Tree and two Fork Tree Summary (iii)-BST two fork sort tree

Source: Internet
Author: User

Binary sorting Tree features: The middle sequence traversal is incremental ...

Here are a few common things to review:

1. How to insert a node into BST

idea : Use recursion to do ...
First, to determine whether the head is empty, is empty is the need to plug the place;
Then, determine if the head data is the same as the new node, the same is to repeat node, do not add
Finally, if it is smaller than the current head.data, put it in the tree you inserted into the left node, or plug it into the right node.

 Public StaticNode insertbst (node root, node Keynode) {if(root==NULL) {//If you encounter an empty place, it is the place to insert ...root=Keynode; } Else{//If the node is not empty, you need to find the corresponding location, and the dichotomy looks like            if(Root.data = = Keynode.data) {//find a worthy number, direct return                returnRoot; } Else if(Keynode.data <root.data) {root.left=Insertbst (Root.left, Keynode); } Else{root.right=Insertbst (Root.right, Keynode); }        }        returnRoot; }

2. How to create BST

Ideas :
  As with the interpolation to BST, step into the insert ...
 Public Static Node createbst (int[] arr) {        null;          for (int i = 0; i < arr.length; i++) {            new  Node (Arr[i]));        }         return root;    }

3. Determine if a tree is a binary sort tree

Ideas :
Because the middle sequence traversal of a binary sort tree is an ascending ordered sequence, it is possible to use this feature to traverse a binary tree directly in sequence, if guaranteed
The previous one is smaller than the latter, so it proves that the tree is a binary sort tree ...

 Public Static BooleanJudgebst (Node root) {intPreVal = 0; Node cur=Root; Stack<Node> stack =NewStack<>();  while(true) {             while(cur! =NULL) {stack.push (cur); Cur=Cur.left; }            if(Stack.isempty ()) Break; Cur=Stack.pop (); if(PreVal >cur.data) {return false; } Else{PreVal=Cur.data; } cur=Cur.right; }        return true; }

4. Removing a node from BST

Ideas:
  1. Find key
2. If key is a leaf node, delete it directly
3. If key has only left or right sub-tree, directly across node (key) connected to its left or right sub-tree ...
4. If key has both Zuozi and right subtree, find the rightmost node of its left subtree rightest, replace the key node with rightest ... Then proceed from the second step to determine how to delete the rightest ...
 Public StaticNode Deletenodefrombst (node root,intkey) {        if(Root = =NULL)return NULL; if(Key <root.data) {root.left=Deletenodefrombst (Root.left, key); } Else if(Key >root.data) {root.right=Deletenodefrombst (Root.right, key); } Else {            //Find Key            if(Root.left = =NULL) {                returnRoot.right; } Else if(Root.right = =NULL) {                returnRoot.left; }            //There is a case of the left and right subtree ...Node tmp =Root; //find the rightmost node in the root left dial hand tree, which can only be traversed from TMPRoot =findrightest (Tmp.left); Root.left=deleterightest (Tmp.left); Root.right=Tmp.right; }        returnRoot; }    //find the right node in the left dial hand tree because it is the maximum value of root Zuozi ...    Private Staticnode findrightest (node root) {if(Root = =NULL)return NULL;  while(Root.right! =NULL) {root=Root.right; }        returnRoot; }    //Delete the point just from the discovery (root left dial hand the rightmost rightest in the tree)//because it is the rightmost node, there must be no right subtree.    Private Staticnode deleterightest (node root) {if(Root.right = =NULL)            returnRoot.left; Root.right=deleterightest (root.right); return NULL; }

Tree and two Fork Tree Summary (iii)-BST two fork 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.