Analysis and implementation of binary search (sort) tree

Source: Internet
Author: User
Tags addsearch

Binary sort tree, also known as a two-fork search tree , is also known as a two- prong tree.

Figure from Baike

two forks the sorting tree is either an empty tree or a two-tree with the following properties:(1) The Joz tree is not empty, then the value of all nodes on the left subtree is less than 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 sorting trees respectively;(4) There are no nodes with key values equal.

steps:
The keyword value of the Joghen node is equal to the search keyword, which succeeds.
Otherwise, if the key value of the root node is small, the left subtree is recursively checked.
Jordahugen The key value of the node and recursively checks the right subtree.
If the subtree is empty, the lookup is unsuccessful.
Average situation analysis (in case of two successful lookups):
in general, set P (N,i) as the average lookup length for the number of nodes in its left subtree. The number of nodes is n = 6 and i = 3; Then P (n,i) = P (6, 3) = [1+ (P (3) + 1) * 3 + (P (2) + 1) * 2]/6= [1+ (5/3 + 1) * 3 + (3/2 + 1) * 2]/6
Note: here P (3), P (2) is the average lookup length of a two-fork classification tree with 3 nodes and 2 nodes. In general, P (i) is the average lookup length of a two-fork classification tree with I nodes.
P (3) = (1+2+2)/3 = 5/3
p (2) = (1+2)/2 = 3/2∴p (n,i) = [1+ (P (i) + 1) * i + (P (n-i-1) + 1) * (n-i-1)]/n
∴p (n) = P (n,i)/n <= 2 (1+i/n) Lnn
because 2 (1+i/n) Lnn≈1.38logn p (n) =o (LOGN)

The binary sort tree is a dynamic tree table relative to the sub-optimal two fork tree. The feature is that the structure of the tree is usually not generated at once, but is inserted during the lookup process when the node in the tree does not have a keyword equal to the given value. The newly inserted nodes must be a newly added leaf node , and a left child or right child node that finds the last node visited on the path when the lookup is unsuccessful. Insert Algorithm
first, the search algorithm is performed to find the Father node of the inserted node. The left and right son of his father's knot is judged by the insertion point. Insert nodes as leaf nodes . if the two fork tree is empty. First, the root node is generated separately. Note: The newly inserted node is always the leaf knot point. Delete a node.
in the binary sort tree Delete a node, divided into three kinds of situation discussion:
    1. If the Saozi right subtree of p node is not empty. After deleting p, in order to keep the relative position of other elements unchanged, it can be adjusted in order by the middle order traversal, there are two ways: one is to make the left/right of P (according to P is the Zuozi of F or right subtree) subtree, S is the right-most node of P Zuozi, while the right subtree of P is the right subtree of S,

Package Com.wxisme.binarysearchtree;import java.util.scanner;/** * binary find (sort) tree implementation * @time 2015/8/16 12:02 * @author wxisme * @param <T> */public class Binarysearchtree<t extends comparable> {Private class TreeNode {private T Data;priv Ate TreeNode left;private TreeNode right;private TreeNode parent;public TreeNode () {}public TreeNode (T data) {This.data = Data;this.left = Null;this.right = Null;this.parent = null;}} Private TreeNode root;private int nodecount;private int depth;public binarysearchtree () {}public binarysearchtree (T data {root = new TreeNode (data);} /** * Add element * @param data */public void Add (T data) {TreeNode node = new TreeNode (data); if (root = null) {root = node;} else {Addsearch (node, root);} Nodecount + +;} public void Addsearch (TreeNode node, TreeNode root) {if (Node.data.compareTo (root.data) <=0) {if (Root.left = = null) { Root.left = Node;node.parent = Root;return;} else {addsearch (node, root.left);}} else {if (root.right = = null) {root.right = Node;node.parent = RooT;return;} else {addsearch (node, root.right);}}} /** * Find node * @param data * @return */public TreeNode Search (T data) {TreeNode it = root;if (it = = null) {SYSTEM.OUT.PRINTLN (The tree is empty!) "); return null;} else {while (it! = null) {if (Data.compareto (it.data) <0) {it = It.left;} else if (Data.compareto (it.data) >0) {It =it.right;} else {return it;}}} return null;} /** * Delete element * @param data */public void Remove (T data) {TreeNode node = search (data); if (node = = null) {SYSTEM.OUT.PRINTLN (" This node does not exist! "); return;} Both left and right subtrees are empty if (node.left==null && node.right==null) {if (Root = = node) {root = null;} else {if (node = = node.parent.left) {node.parent.left = null;} else {node.parent.right = null;} node.parent = null;}} Left dial hand tree is not empty, right subtree is empty else if (node.left!=null && node.right==null) {if (Root = = node) {root = Node.left;root.parent = null ;} else {if (node = = node.parent.left) {node.parent.left = Node.left;node.left.parent = Node.parent;node.left = null; node.parent = null;} else {node.parent.right = Node.left;node.left.parent = node.parent;node.left = Null;node.parent = null;}}} Right subtree is not empty, left subtree is empty else if (node.left==null && node.right!=null) {if (Root = = node) {root = Node.right;root.parent = nul l;} else {if (node = = node.parent.left) {node.parent.left = Node.right;node.right.parent = Node.parent;node.right = Null;node . parent = null;} else {node.parent.right = Node.right;node.right.parent = Node.parent;node.right = Null;node.parent = null;}}} The left and right subtrees are not empty or else {//Record the largest node TreeNode Leftmaxnode = Node.left;while (leftmaxnode.right! = null) {Leftmaxnode = Leftmaxnode.right;} Move the node with the largest value in the left subtree to the location of the node you want to delete. LeftMaxNode.parent.right = Null;leftmaxnode.parent = node.parent;if (node = = node.parent.left) {Node.parent.left = Leftmaxnode;} else {node.parent.right = Leftmaxnode;} Leftmaxnode.left = Node.left;leftmaxnode.right = node.right;//Releases the reference to the node to be deleted node.parent = Node.left = Node.right = null;} Nodecount--;} /** * Number of nodes * @return */public int size () {return nodecount;} /** * Destroy tree */public void Destroy () {root = Null;nodecount = 0;}/** * Middle sequence traversal tree */public void traversal () {System.out.print ("middle order Traversal:"); inordertraversal (root); System.out.println ();} public void Inordertraversal (TreeNode root) {if (root = null) {return;} Inordertraversal (Root.left); System.out.print (Root.data); Inordertraversal (root.right);} /** * Test * @param args */public static void main (string[] args) {binarysearchtree<integer> tree = new BINARYSEARCHTR Ee<integer> (); Scanner scan = new Scanner (system.in); for (int i=0; i<10; i++) {Tree.add (Scan.nextint ());} Tree.traversal (); System.out.print ("Delete node:"); Tree.remove (Scan.nextint ()); Tree.traversal (); System.out.println ("Number of nodes:" + tree.size ()); System.out.println ("Destroy Tree"); Tree.destroy (); System.out.println ("Number of nodes:" + tree.size ()); }}

Test results:

5 9 6 3 2 8 7 4 1 0
Middle Sequence Traversal: 0123456789
Delete node: 7
Middle Sequence Traversal: 012345689
Number of nodes: 9
Destroy Tree
Number of nodes: 0

Analysis and implementation of binary search (sort) tree

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.