Import Queue "" " binary search tree: Features: The value is smaller than the value of the left subtree of the root node; value is greater than the right subtree of the root node 1. Create 2. the insertion node 3 first traversal The lookup value node 5. delete node "" " class the TreeNode (Object): " "" node definition tree "" " DEF the __init__ (Self, Val): self.value = Val self.father = None self.left = None self.right = None class BinarySearchTree (Object): DEF the __init__ (Self, the nodeList): "" "Construction of binary search tree . "" " self.root = None for node in nodeList: self.insert(node) def insert(self, node): """插入节点""" father = None cur = self.root # 找到要插入的位置 while cur is not None: if cur.value == node.value: return -1 father = cur if node.value < cur.value: cur = Cur.left the else : CUR = cur.right # insertion node node.father = Father IF Father IS None: # Print ( "of the root node") self.root = Node elif node.value < father.value: # Print ( "insert left sub-tree") father.left = the Node the else : # Print ( "into the right subtree") father.right = the Node DEF breadthFirstSearch (Self): "" "广度优先遍历""" if self.root is None: return None resultList = [] q = queue.Queue() q.put(self.root) while not q.empty(): node = q.get() # print(node.value) resultList.append(node.value) if node.left is not None: q.put(node.left) if node.right is not None: q.put(node.right) return resultList def search(self, value): """根据值查找节点""" cur = self.root while cur is not None: if cur.value == value: return cur if value < cur.value: cur = cur.left if value > cur.value: cur = cur.right return None def deleteNode(self, node): """Delete nodes "" " Father = node.father # deleted node has no left subtree, right subtree instead of the deleted node IF node.left IS None: # node is the root node deleted IF Father IS None: self.root = the Node .right IF node.right iS not None: node.right.father = None # delete a node is not the root elif father.left == the node: # nodes removed is left child father.left = node.right IF node.right IS not None: node.right.father = Father the else : # node is removed right child father.right = node.right IF node.right IS not None: node.right.father = Father return 1 # to delete a node has left subtree, right subtree hanging to the left of the rightmost sub-tree node tmpnodes = node.left the while tmpNode.right iS not None: tmpnodes =tmpNode.right tmpNode.right = node.right IF node.right IS not None: node.right.father = tmpnodes # node is the root node deleted IF Father IS None: self.root = node.left node.left.father = none # nodes removed is left child elif father.left == the node: father.left = node.left node.left.father = Father # node is removed right child the else : father.right = node.left node.left.father = Father # Node = None return 2 IF the __name__ == ' __main__ ' : Data = [24, 34 is,. 5,. 4,. 8, 23 is, 45, 35, 28 ,. 6, 29 ] the nodeList = [the TreeNode (D) for D in Data] # build a binary search tree BST = BinarySearchTree (the nodeList) # breadth first traversal print (bst.breadthFirstSearch ()) # lookup node according to the value print (bst.search (23 )) the Node = bst.search (8 ) # delete nodes bst.deleteNode (the Node) Print (bst.breadthFirstSearch ())