GitHub: Code Implementation
This algorithm is implemented using Python3
1. Two Fork search tree definition
?? Binary search tree, aka binary sorting tree (binary sort trees).
?? A binary search tree is a two-fork tree with the following properties:
?? (1) Joz tree is not empty, the value of all nodes on the left subtree is less than or equal to the value of its root node.
?? (2) If the right subtree is not empty, the value of all nodes on the right subtree is greater than or equal to the value of its root node.
?? (3) The left and right sub-trees are also two-fork search trees respectively.
2. Two fork search tree related Operations 2.1 insert operation
?? From the root node, if the value inserted is smaller than the value of the root node, it is inserted into the left subtree of the root node, and if the value is larger than the root node, it is inserted into the right subtree of the root node. The operation can be implemented using recursion.
??????
Program code:
def insert(self, root, val): '''二叉搜索树插入操作''' if==None: = TreeNode(val) elif< root.val: =self.insert(root.left, val) elif> root.val: =self.insert(root.right, val) return root
2.2 Query Operations
?? Search from the root node, whether the value to be found is the same as the value of the root node, if the same is true; otherwise, determine whether the value to be looked for is smaller than the value of the root node, if you go to the root node left subtree to find, otherwise go to the right subtree to find. This operation uses a recursive implementation.
Program code:
def query(self, root, val): '''二叉搜索树查询操作''' if==None: returnFalse if== val: returnTrue elif< root.val: returnself.query(root.left, val) elif> root.val: returnself.query(root.right, val)
2.3 Finding the maximum (small) in a binary search tree
?? (1) Find the minimum value: Starting from the root node, continue down the Zuozi until the last left subtree node is found, according to the definition, the node must be the minimum node in the two-fork search tree.
Program code:
def findMin(self, root): '''查找二叉搜索树中最小值点''' if root.left: returnself.findMin(root.left) else: return root
?? (2) Find maximum value: Starting from the root node, continue down the right subtree until the last right subtree node is found, according to the definition, the node must be the maximum node in the two-fork search tree.
Program code:
def findMax(self, root): '''查找二叉搜索树中最大值点''' if root.right: returnself.findMax(root.right) else: return root
2.4 Delete node actions
?? The deletion of a two-fork search tree node is divided into the following three scenarios:
?? (1) The node to be deleted has no left subtree or right subtree: delete the node directly
??????
?? (2) The node to be deleted is only Zuozi or only right subtree: replace its left subtree or right subtree node with the node to be deleted
??????
?? (3) The node to be deleted has both Zuozi and right subtree: Find the smallest node in the right subtree of the node, use that node instead of the node to be deleted, and then delete the minimum node in the right subtree.
??????
Program code:
defDelnode ( Self, Root, Val):"Delete the binary search tree with a value of Val" ifRoot== None:return ifVal<Root.val:root.left= Self. Delnode (Root.left, Val)elifVal>Root.val:root.right= Self. Delnode (Root.right, Val)# when val = = Root.val, there are three cases: only Zuozi or only the right sub-tree, there is a subtree, that is, no Zuozi and no right sub-tree Else:ifRoot.left andRoot.right:# both Zuozi and right subtree, you need to find the smallest node in the right sub-treeTemp= Self. Findmin (Root.right) root.val=Temp.val# Remove the minimum node from the right subtreeRoot.right= Self. Delnode (Root.right, Temp.val)elifRoot.right== None andRoot.left== None:# Left and right subtrees are emptyRoot= None elifRoot.right== None:# Only left dial hand treesRoot=Root.leftelifRoot.left== None:# only right subtreeRoot=Root.rightreturnRoot
2.5 Printing operations
?? Implement a two-fork search tree in the middle sequence traversal, and print out. The number of sequences printed out by this method will be arranged in ascending order.
Program code:
def printTree(self, root): # 打印二叉搜索树(中序打印,有序数列) if==None: return self.printTree(root.left) print=' ') self.printTree(root.right)
Binary search tree (BST)---python implementation