Binary search Tree Python implementation

Source: Internet
Author: User

1. Definition of a two-fork lookup tree:

When the left dial hand tree is not empty. When the node value of the Zuozi is less than the root, and the right subtree is not empty, the node value of the right subtree is greater than the root node. The left and right sub-trees are two-fork search trees

2. The leftmost node of the two fork find tree is the minimum value to find the minimum value. Simply traverse the nodes of the left subtree until it is empty. In the same vein, the rightmost node ends with the maximum value. To find the maximum value, simply traverse the node of the right subtree until it is empty. Binary search tree Insert find and delete are implemented recursively, delete a node, first find the node s, assuming that the node is not empty children, this is not really delete this node s, but in its right sub-tree to find the successor node, the value of the subsequent node to pay S, You can then delete this successor node.

Assuming that the left child or right child of node S is empty, the node s can be deleted directly.

3. Python implementation of a two-fork lookup tree:

Class Treenode:def __init__ (self,val): Self.val=val;self.left=none;self.right=none;def Insert (Root,val): if Root is   None:root=treenode (val); Else:if Val<root.val:root.left=insert (Root.left,val);  #递归地插入元素elif Val>root.val:root.right=insert (Root.right,val); return root;def query (Root,val): If Root is None:return, if Root.val is Val:return 1;if root.val <val:return query (root.  Right,val); #递归地查询else: Return query (root.left,val);d ef findmin (root): If Root.left:return findmin (root.left); Else:return root; def delnum (Root,val): if Root is None:return; if Val<root.val:return delnum (root.left,val); Elif Val>root.val: Return Delnum (Root.right,val); else: # Remove the case if the left and right children are empty if (Root.left and Root.ri             ght): Tmp=finmin (Root.right);    #找到后继结点root. Val=tmp.val;root.right=delnum (Root.right,val); #实际删除的是这个后继结点else: If Root.left is None:root=root.right;elif root.right is None:root=root.left;return root; #測试代码root = TreeNode (3); Root=insert (root,2); rOot=insert (root,1); Root=insert (root,4), #print query (root,3);p rint query (root,1), Root=delnum (root,1);p rint query ( root,1);

Binary search Tree Python implementation

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.