The document describes functions to implement operations on a binary search tree including insertion, searching, and deletion of nodes. It defines a Node class with data and left/right child pointers. The insert_data function recursively adds a new node, search_opr searches for a key, and inorder prints the tree. Delete_Node removes a node by considering cases for node children and replacing the key value. Main tests the functions by building a tree from input, searching, and deleting a node.
The document describes functions to implement operations on a binary search tree including insertion, searching, and deletion of nodes. It defines a Node class with data and left/right child pointers. The insert_data function recursively adds a new node, search_opr searches for a key, and inorder prints the tree. Delete_Node removes a node by considering cases for node children and replacing the key value. Main tests the functions by building a tree from input, searching, and deleting a node.
The document describes functions to implement operations on a binary search tree including insertion, searching, and deletion of nodes. It defines a Node class with data and left/right child pointers. The insert_data function recursively adds a new node, search_opr searches for a key, and inorder prints the tree. Delete_Node removes a node by considering cases for node children and replacing the key value. Main tests the functions by building a tree from input, searching, and deleting a node.
2) #Structure of a node in the binary search tree 3) class Node: 4) def __init__(self,data): 5) self.left=None 6) self.data=data 7) self.right=None 8) 9) def insert_data(root,data): 10) if root==None: 11) return Node(data) 12) else: 13) if root.data>data: 14) root.left=insert_data(root.left,data) 15) else: 16) root.right=insert_data(root.right,data) 17) return root 18) 19)def search_opr(root,key): 20) if root.data==key or root==None: 21) return root 22) elif root.data>key: 23) return search_opr(root.left,key) 24) elif root.data<=key: 25) return search_opr(root.right,key) 26) 27)def inorder(root): 28) if root!=None: 29) inorder(root.left) 30) print(root.data) 31) inorder(root.right) 32) 33)def delete_Node(root, key): root key delete node 34) # if root doesn't exist, just return it 35) if not root: 36) return root 37) # Find the node in the left subtree if key value is less than root value 38) if root.val > key: 39) root.left = delete_Node(root.left, key) 40) # Find the node in right subtree if key value is greater than root value, 41) elif root.val < key: 42) root.right= delete_Node(root.right, key) Dpne by: T. Vivekananda chary 121910312049
43) # Delete the node if root.value == key
44) else: 45) # If there is no right children delete the node and new root would be root.left 46) if not root.right: 47) return root.left 48) # If there is no left children delete the node and new root would be root.right 49) if not root.left: 50) return root.right 51) # If both left and right children exist in the node replace its value with 52) # the minmimum value in the right subtree. Now delete that minimum node 53) # in the right subtree 54) temp_val = root.right 55) mini_val = temp_val.val 56) while temp_val.left: 57) temp_val = temp_val.left 58) mini_val = temp_val.val 59) # Delete the minimum node in right subtree 60) root.right = delete_Node(root.right,root.val) 61) return root 62) 63)if __name__=="__main__": 64) 65) a=list(map(int,input("Enter elements to insert:").split(" "))) 66) r=int(input("Enter root Node:")) 67) root_node=Node(r) 68) 69) for i in range(1,len(a)): 70) root_node=insert_data(root_node,a[i]) 71) 72) inorder(root_node) 73) 74) key=int(input("Enter element to search:")) 75) val=search_opr(root_node,key) 76) if val!=None: 77) print(key,"is present in the tree") 78) else: 79) print(key,"not present in the tree") 80) 81) 82) a=delete_Node(root_node,int(input("Enter element to delete:"))) 83) print("After deleting specified node:") 84) inorder(root_node) Dpne by: T. Vivekananda chary 121910312049