## Operation on Binary Search Tree ### Binary Search Tree Operations ``` 1. Insert Element 2. Traverse Tree i.Inorder Traversal ii.Preorder Traversal iii.Postorder Traversal 3. Search Element 4. Remove Element ``` ### 1. Insert Element - Use Recursive pattern to insert new Item in Binary Search Tree Follow steps to insert new items in the tree. - Craete a Normal Node class to creates Nodes for building of Binary search tree ``` class Node: def __init__(self,value=None): self.value=value self.left=None self.right=None self.count = 1 ``` - Create class BST and create Insert and Insert Method Respectively ``` class BST: def Insert(self,root,value): #create Node using value node=Node(value) #Perform Insertion Operation here in Recursive manner if the root is None Then Return node if the root node is not None then perform the following operation if root.value==value then just increase counter by 1 elif valueroot.value then assign root.right=self.Insert(root.right,value) #finally return root node return root ``` and call above function method from a wraper Method ``` def InsertWrap(self,data): root=Node(data[0]) for value in data[1:]: root=self.Insert(root,value) #Return the root node return node ``` - now create Object of BST ``` t=BST() data=[9,6,16,18,1,4,7] t.InsertWrap(data) ``` Full Methods : ```python #Recusrsive Function to Insert Data into Tree def Insert(self,root,value): node=Node(value) if root is None: return node else: if root.value==value: root.count+=1 elif valueroot.value: root.right=self.Insert(root.right,value) return root #Wraper Function to Call Insert Method (Call this method first using class object) def Insertwrap(self,data): root=Node(data[0]) print("Root Node assigned ",data[0]) for value in data[1:]: root=self.Insert(root,value) print("Inserted Node",value) return root ``` ## 2. Traverse Tree ### i.Inorder Traversal Simple Steps for Inorder Traversal 1. visit the left node if exists 2. print root node Value 3. visit right node if exists 4. go to step 1 if the root node is exists Example(Recusrsive): ```python def InOrder(self,root,array): if root: self.InOrder(root.left,array) array.append(root.value) self.InOrder(root.right,array) return array ``` ### ii.Preorder Traversal Simple Steps for Preorder Traversal 1. print root node Value 2. visit left node if exists 3. visit right node if exists 4. go to step 1 if the root node is exists Example(Recursive): ```python def PreOrder(self,root,array): if root: array.append(root.value) self.PreOrder(root.left,array) self.PreOrder(root.right,array) return array ``` ### iii.Postorder Traversal 1. visit the right node if exists 2. visit left node if exists 3. print root node Value 4. go to step 1 if the root node is exists Example(Recursive): ```python def PostOrder(self,root,array): if root: self.PostOrder(root.left,array) self.PostOrder(root.right,array) array.append(root.value) return array ```` ## 3. Search Element Use the following steps to search an element in Binary Search Tree consider ``` root=Your_binary_search_tree value=search_element ``` 1. if root.value==value then simple return True 2. else compare value with root.value 3. if valueroot.value and root.right is present then return Find(root.right,value) root.right is not present then return False Example: ```python def Find(self,root,value): if root.value==value: return True else: if valueroot.value: root.right=self.Insert(root.right,value) return root def Insertwrap(self,data): root=Node(data[0]) print("Root Node assigned ",data[0]) for value in data[1:]: root=self.Insert(root,value) print("Inserted Node",value) return root def Find(self,root,value): if root.value==value: return True else: if value