Package: bstq
BSTqBSTq is a comprehensive Python library dedicated to Binary Search Trees (BSTs). Designed to work with BSTs, this library offers a wide array of functions and utilities, rather than basic functions to work with binary search trees effortlessly.This library has lots of functions which can be used to minimize your efforts for Binary Search Trees .Installpip install bstqUsagefrom bstq import *Creating Binary Search TreeThe following class is used to create a BinarySearchTree and instanciate a tree object with root value  of 10 .classBinarySearchTree:def__init__(self,data,left=None,right=None):self.data=dataself.left=leftself.right=rightroot=BinarySearchTree(10)Create Binary Search Tree from Sorted list.You have to provide a sorted list which you will paas in thecreateBSTFromSortedList, the function will return a root of the Binary Search Tree created from the list .l=[11,14,36,64,69,71,83,83]# sorted list.root=createBSTFromSortedList(l)print(root)Output:SortedList:[11,14,36,64,69,71,83,83]64__/\__1471/\/\11366983\83Create Binary Search Tree With given Height .You can usecreateBSTWithHeightfunction to create BST with given height.bst = createBSTWithHeight(4)
print(bst)Output :
              8
       ______/ \_____
      4              12
   __/ \_         __/  \__
  2      6      10        14
 / \    / \    /  \      /  \
1   3  5   7  9    11  13    15InOrder , PreOrder , PostOrder , LevelOrder Traversals .There are two methods for traversals , one is class based which is specific to class and another is global method for bst.in_order=bst.inOrder()# class methodin_order=inOrder(bst)# global methodprint(f"In Order :{in_order}")pre_order=bst.preOrder()# class methodpre_order=preOrder(bst)# global methodprint(f"Pre Order :{pre_order}")post_order=bst.postOrder()# class methodpost_order=postOrder(bst)# global methodprint(f"Post Order :{post_order}")level_order=bst.levelOrder()# class methodlevel_order=levelOrder(bst)# global methodprint(f"Level Order :{level_order}")Output :
In Order :  [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15]
Pre Order :  [8, 4, 2, 1, 3, 6, 5, 7, 12, 10, 9, 11, 14, 13, 15]  
Post Order :  [1, 3, 2, 5, 7, 6, 4, 9, 11, 10, 13, 15, 14, 12, 8] 
Level Order :  [8, 4, 12, 2, 6, 10, 14, 1, 3, 5, 7, 9, 11, 13, 15]List Level Order TraversalThis function gives the list  of all the levels of bst .level_order_list = bst.levelOrderList()
level_order_list = levelOrderList(bst)
print(level_order_list)Output :
[[8], [4, 12], [2, 6, 10, 14], [1, 3, 5, 7, 9, 11, 13, 15]]Search in bstnode=searchInBST(bst,6)node=bst.searchInBST(6)print(node)Ouput:6/\57Height of bsth=bst.height()h=height(bst)Output:4Printing Binary TreeYou will get the most prettiest display of Binary Search Tree .printBTree(bst) # global function
print(bst)      # you can also use print function directly .

Output :
              8
       ______/ \_____
      4              12
   __/ \_         __/  \__
  2      6      10        14
 / \    / \    /  \      /  \
1   3  5   7  9    11  13    15Printing the Binary Search Tree Horizontally Flipped .You cannot use the print function , you have to useprintBTree()with inverted parameter to be TrueprintBTree(bst,inverted=True)Output:13579111315\/\/\/\/261014Â¯Â¯\/Â¯Â¯Â¯\/Â¯Â¯412Â¯Â¯Â¯Â¯Â¯Â¯\/Â¯Â¯Â¯Â¯Â¯8Invert BSTInvertes the binary search tree to its mirror image .inverted_tree=invertBST(bst)print(inverted_tree)8______/\______124__/\____/\_141062/\/\/\/\15131197531Top View of BSTtop_view=topViewOfBST(bst)print(top_view)Output:[1,2,4,8,12,14,15]Delete Node in BSTGives and new node with delete bst and also deletes from the actual one.new_bst=deleteNodeInBST(bst,6)print(new_bst)Output:8_____/\_____412/\__/\__251014/\   \/\/\1379111315Distance of Node From Rootdistance=findDistanceFromRoot(bst,7)print(distance)Output:3Binary Search Tree to Doubly Linked Listdoubly_linked_list=bstToDLL(bst)print(doubly_linked_list)Output:1->2->3->4->5->6->7->8->9->10->11->12->13->14->15Linked List EnumerationThe functionenumerateLinkedList(doubly_linked_list)is used to enumerate through the linked list . It gives three items in the tuple of list that is (index , node , node_data)new_bst=createBSTWithHeight(2)# created new bst with height of 2 .doubly_linked_list=bstToDLL(new_bst)forindex,node,datainenumerateLinkedList(doubly_linked_list):print(f"index :{index}, node :{node}, node data :{data}")Output:index:0,node:LinkedListNode(data=1,prev=.,next=2),nodedata:1index:1,node:LinkedListNode(data=2,prev=1,next=3),nodedata:2index:2,node:LinkedListNode(data=3,prev=2,next=.),nodedata:3Serialization and Deserialization Of Binary Search Tree.The library hasBSTSerializerclass which is used for Binary Tree Serialization and DeSerialization .There are two functions in the classserialize()which returns a string of serialied binary serach tree anddeSerialize()which returns the root of the deSerialized Binary Search Tree .serializer=BSTSerializer()serialized_string=serializer.serialize(bst)print(f" Serialized BST String  :{serialized_string}")# serialized stringdeserialized_bst=serializer.deSerialize(serialized_string)# root of the binary tree.print(f"Deserialized Binary Search Tree ")print(deserialized_bst)Output:SerializedBSTString:8.4.2.1.N.N.3.N.N.6.5.N.N.7.N.N.12.10.9.N.N.11.N.N.14.13.N.N.15.NDeserializedBinarySearchTree8______/\_____412__/\___/\__261014/\/\/\/\13579111315There are more functions in the library to work with .isBST(root): The function checks whether the given tree is BST or not .bstSum: The function gives the sum of all the values of the Binary Search Tree .diameterOfBST: The function gives the biggest path of the tree or the diameter of the tree .kThSmallestElement: The function gives the kth smallest element in the bst .kThLargetElement: The function gives the kth largest element in the bst.findMinInBST: The function gives the smallest node in BST.findMaxInBST: The function gives the largest node in BST.is_bst=isBST(bst)print(f"Is Bst :{is_bst}")bst_sum=bstSum(bst)print(bst)print(f"Sum of nodes of the Binary Search Tree :{bst_sum}")diameter_of_bst=diameterOfBST(bst)print(f"Diameter of Binary Search Tree:{diameter_of_bst}")kth_smallest=kThSmallestNode(bst,6)print(f"k th smallest node in Binary Search Tree :\n{kth_smallest}")kth_largest=kThLargestNode(bst,4)print(f"k th largest node in Binary Search Tree:\n{kth_largest}")min_element=findMinInBST(bst)print(f"Minimum value in BST :\n{min_element}")max_element=findMaxInBST(bst)print(f"Maximum value in BST :\n{max_element}")Output:IsBst:TrueSumofnodesoftheBinarySearchTree:496DiameterofBinarySearchTree:8kthsmallestnodeinBinarySearchTree:6/\57kthlargestnodeinBinarySearchTree:28__/\__2630/\/\25272931MinimumvalueinBST:1MaximumvalueinBST:15