Binary search tree (i)

Source: Internet
Author: User

A lookup tree is a data structure that supports a variety of dynamic collection operations, including construction, lookup, insertion, deletion, finding minimum and maximum values, and so on.

Binary search tree is organized according to binary tree structure, which is usually represented by a chain list.
1. Each node represents an object, and the node consists of a data part, a pointer (left,right pointer).
2. If the son node of a node does not exist, then the corresponding son knot is empty.

Characteristics:
1. The left subtree of the root node is not empty, and the values of all nodes of the left subtree are small values of the root node
2. The right subtree of the root node is not empty, the value of all nodes in the right subtree is greater than the value of the root node
3. The left and right sub-tree of the root node is also a binary lookup tree
4. Middle sequence Traversal binary search tree, the resulting middle sequence traversal sequence is an ascending, ordered sequence

1. Find: Starting from the root node
A. Lookup failed: Two fork tree is empty
B. Find success:
1) If the lookup value is the root node value, then the success
2) If the lookup value is less than the root node value, the Zuozi recursive lookup
3) If the lookup value is greater than the root node value, look in the right subtree recursively
2. Delete
A. If the deleted node has no child nodes, the reference to the corresponding location of its parent node is set to null directly
B. If the deleted node has only one child node, simply replace the child node of the node that you want to delete in place of its location
C. If the deleted node has two child nodes, replace it with the middle-order successor that is closest to the deletion node.
3. Insert: Compare the node you want to insert with the root node
A. The node to be inserted is the root node, recursively to the left subtree of the corresponding root node until the location where the left dial hand tree is found empty
B. The node to be inserted is greater than the root node, recursively to the right subtree of the corresponding root node until the right subtree is found to be empty
Example:
1. Node class-node

1 /**2 * Node class3  * @authorIvy4  */5  Public classNode {6     //Node Value7     intdata;8     //left dial hand tree9 Node left;Ten     //Right sub-tree One Node right; A  -      PublicNode (intdata, node left, node right) { -          This. data =data; the          This. left =Left ; -          This. right =Right ; -     } -  +}


2. Insert Algorithm-insertbinarytree

1 /**2 * Node class3  * @authorIvy4  */5  Public classNode {6     //Node Value7     intdata;8     //left dial hand tree9 Node left;Ten     //Right sub-tree One Node right; A  -      PublicNode (intdata, node left, node right) { -          This. data =data; the          This. left =Left ; -          This. right =Right ; -     } -  +}


3. Find Algorithm-findbinarytree

1 /**2 * @Description: Two fork find tree lookup algorithm3  * @authorIvy4  */5  Public classFindbinarytree {6 //root node7     PrivateNode Root;8 //inserting Nodes9      Public voidAddintdata) {TenSystem.out.println ("Insert node:" +data); One         if(NULL==root) { ARoot =NewNode (data,NULL,NULL); -}Else { - Addtree (root,data); the         } -     } -  -     Private voidAddtree (Node root,intdata) { +         if(Root.data >data) { - //into the left sub-tree +             if(NULL==root.left) { ARoot.left =NewNode (data,NULL,NULL); at}Else { -Addtree (root.left, data);//the method of hanging itself, realizing recursion into the left sub-tree -             } -}Else { - //into the right sub-tree -             if(NULL==root.right) { inRoot.right =NewNode (data,NULL,NULL); -}Else { toAddtree (root.right, data);//the method of hanging itself, realizing recursion into the right sub-tree +             } -         } the          *     } $     Panax Notoginseng //Middle sequence Traversal binary search tree -      Public voidShow () { the ShowTree (root); +     } A  the     Private voidshowTree (Node root) { +         if(NULL!=root.left) { - ShowTree (root.left); $         } $System.out.println (Root.data + ""); -         if(NULL!=root.right) { - ShowTree (root.right); the         } -         Wuyi     } the //Find Algorithm -      PublicNode Searchnode (intfindData) { WuNode node =NULL; -Node RootNode =Root; About          while(true) { $             if(NULL==RootNode) { -                  Break; -             } -             if(Rootnode.data = =findData) { Anode =RootNode; +                  Break; the             } -             if(Rootnode.data >findData) { $RootNode =Rootnode.left; the}Else { theRootNode =Rootnode.right; the             } the              -         } in         returnnode; the     } the      About //Test the      Public Static voidMain (string[] args) { theFindbinarytree tree =NewFindbinarytree (); theTree.add (9); +Tree.add (13); -Tree.add (45); theTree.add (2);BayiTree.add (34); theTree.add (45); theTree.add (5); -Tree.add (3); -Tree.add (78); theTree.add (56); the tree.show (); the          the         intFindData = 0; -Scanner input =NewScanner (system.in); theSystem.out.println ("Please enter the node value to find:"); theFindData =input.nextint (); theNode node =Tree.searchnode (findData);94         if(NULL==node) { theSystem.out.println ("Find failed! "); the}Else { theSystem.out.println ("Lookup succeeded, find node value:" +node.data);98         } About     } -     101}



Binary search tree (i)

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.