Binary search Tree

Source: Internet
Author: User

Nature: Left Dial hand node < parent node < right child node
Definition:
publicclass TreeNode {    publicint keyValue;    //关键字值    public TreeNode leftNode;//左节点    public TreeNode rightNode;//右节点    publicTreeNode(){}    publicTreeNode(int Key){        this.keyValue = Key;    }}
1. Find
  PublicTreeNodeSearch(intKey) {TreeNode node = root;//First define a node to point to the root, in the following loop        //As long as the node value is not equal to the node value being looked up into the loop returns null if not found         while(Node.keyvalue! = Key) {if(Key < Node.keyvalue) {//If the value to find is less than the node value then point to the left nodenode = Node.leftnode; }Else{//or point to right nodenode = Node.rightnode; }if(node = =NULL) {//returns NULL if the node is empty                return NULL; }        }returnNode }
2. Insert

 Public void Insert(intKey) {TreeNode node =NewTreeNode (Key);///Add nodes first to find the location to add, so remember to add the node's parent node        //Leave the parent node pointing to the node to be added        if(Root = =NULL) {//If the root node is empty, the root point points to the new noderoot = node; }Else{TreeNode CurrentNode = root;//define the current node and point to the root nodeTreeNode parentnode; while(true) {//Find the location where the node is addedParentNode = CurrentNode;//parent node of the current node                if(Key < Currentnode.keyvalue) {CurrentNode = Currentnode.leftnode;if(CurrentNode = =NULL) {//When an empty node is found, the left node of the parent node points to the new nodeParentnode.leftnode = node;return; }                }Else{CurrentNode = Currentnode.rightnode;if(CurrentNode = =NULL) {//When an empty node is found, the right node of the parent node points to the new nodeParentnode.rightnode = node;return; }                }            }        }    }
3. Delete

The node deletion of a binary tree is divided into three categories: delete nodes that have no child nodes, delete nodes with only one child node, and delete nodes with two child nodes. The first and second are simple and the third is more complex.

Before you start, find the child node you want to delete, which is used to find

TreeNode current = root; TreeNodeParent= root; Boolean isleftnode =true;//Mark is the left or right child node of the parent node         while(Current.keyvalue! = Key) {Parent= current;//parent node of the current node            if(Key < Current.keyvalue) {Isleftnode =true;            current = Current.leftnode; }Else{Isleftnode =false;            current = Current.rightnode; }        }if(Current = =NULL) {System.out.println ("did not find the node to delete!" ");return false; }

Delete a node that has no child nodes : Deleting a node that has no child nodes simply points the parent node of the deleted node to null.

if  (Current.leftnode = = null  && current.rightnode = = null ) {//the node to be removed has no child nodes  if  (current = = root) {//root node Delete entire tree  root = null ; } else  if  (Isleftnode) {                //if it is the left node, make the node point to null             parent . Leftnode = null ; } else  {//if it is the right node, the right node points to null  parent . Rightnode = null ; }        }

Delete a node that has a child node: Delete the node that has a child node, simply point the parent node of the deleted node to the child node of the deleted node

      if(Current.leftnode = =NULL) {//The node to be deleted has only the right node            if(current = = root)            {root = Current.rightnode; }Else if(Isleftnode) {Parent. Leftnode = Current.rightnode; }Else{Parent. Rightnode = Current.rightnode; }        }Else if(Current.rightnode = =NULL) {//The node to be deleted has only the left node            if(current = = root)            {root = Current.leftnode; }Else if(Isleftnode) {Parent. Leftnode = Current.leftnode; }Else{Parent. Rightnode = Current.leftnode; }        }

Delete node with two child nodes : Delete the node with two child nodes, who will replace the deleted node's location? Is the left node, or the right node, instead of the child nodes of this child node should be how to arrange? A whole series of questions came out ... The easy way is to find a node instead of the deleted node, which should be viewed from the definition of a two-fork search tree. Because the binary search tree is orderly, we are looking for the node in this tree, and this node is larger than the left node is deleted, smaller than the right node. Let's take a look at this. All nodes of the subtree with the right node of the node being deleted are larger than the node being deleted, which is defined by the two-fork search tree, but to find the smallest one in this set instead of the deleted node, keep looking to the left in the subtrees tree. This node is smaller than the right node of the node being deleted, and larger than the left node, which is called the successor node of the deleted node, using this node instead of the deleted node.

private TreeNode findSuccessor(TreeNode delNode){        parent = delNode;        TreeNode successor = delNode;        TreeNode current = delNode.rightNode;        whilenull){            parent = successor;            successor = current;            current = current.leftNode;        }        //这段代码的作用是当后继节点不是被删除节点的右节点并且后继结点有右节点的情况下的时候做的操作        if(successor != delNode.rightNode){            parent.leftNode = successor.rightNode;            successor.rightNode = delNode.rightNode;        }        return successor;    }

Find the next node the rest of the matter is simply to replace the deleted node with the successor node, the process should be aware of the various pointers pointing.

TreeNode successor = findSuccessor(current);            if(current == root){                root = successor;            }elseif(isLeftNode){                parent.leftNode = successor;            }else{                parent.rightNode = successor;            }            successor.current.leftNode;
All the code of the binary search tree
 Public classTree { PublicTreeNode Root;//root node    //Find node     PublicTreeNodeSearch(intKey) {TreeNode node = root;//First define a node to point to the root, in the following loop        //As long as the node value is not equal to the node value being looked up into the loop returns null if not found         while(Node.keyvalue! = Key) {if(Key < Node.keyvalue) {//If the value to find is less than the node value then point to the left nodenode = Node.leftnode; }Else{//or point to right nodenode = Node.rightnode; }if(node = =NULL) {//returns NULL if the node is empty                return NULL; }        }returnNode }//Add node     Public void Insert(intKey) {TreeNode node =NewTreeNode (Key);///Add nodes first to find the location to add, so remember to add the node's parent node        //Leave the parent node pointing to the node to be added        if(Root = =NULL) {//If the root node is empty, the root point points to the new noderoot = node; }Else{TreeNode CurrentNode = root;//define the current node and point to the root nodeTreeNode parentnode; while(true) {//Find the location where the node is addedParentNode = CurrentNode;if(Key < Currentnode.keyvalue) {CurrentNode = Currentnode.leftnode;if(CurrentNode = =NULL) {//When an empty node is found, the left node of the parent node points to the new nodeParentnode.leftnode = node;return; }                }Else{CurrentNode = Currentnode.rightnode;if(CurrentNode = =NULL) {//When an empty node is found, the right node of the parent node points to the new nodeParentnode.rightnode = node;return; }                }            }        }    }//Traversal tree     Public void Display(TreeNode node) {if(Node! =NULL) {display (Node.leftnode); System. out. println (Node.keyvalue +",");        Display (Node.rightnode); }    }//Maximum value     Public int Max() {TreeNode node = root; TreeNode parent =NULL; while(Node! =NULL) {parent = node;        node = Node.rightnode; }returnParent.keyvalue; }//min value     Public int min() {TreeNode node = root; TreeNode parent =NULL; while(Node! =NULL) {parent = node;        node = Node.leftnode; }returnParent.keyvalue; }//Delete nodes three ways to delete nodes    ///1, delete the node without child nodes, direct the node's parent node's left or right node to point to the empty    ///2, delete the node that has a child node, direct the node's parent node to the remaining node of the deleted node    ///3, delete the child nodes with three nodes, find the successor node to delete the node, replace the deleted node with this node     PublicBooleanDelete(intKey) {//Find the node first, and record the parent node reference of the nodeTreeNode current = root;        TreeNode parent = root; Boolean isleftnode =true; while(Current.keyvalue! = Key) {parent = current;if(Key < Current.keyvalue) {Isleftnode =true;            current = Current.leftnode; }Else{Isleftnode =false;            current = Current.rightnode; }        }if(Current = =NULL) {System. out. println ("did not find the node to delete!" ");return false; }Delete nodes in three different situations        if(Current.leftnode = =NULL&& Current.rightnode = =NULL) {//The node to be removed has no child nodes            if(current = = root) {//root node deletes entire treeRoot =NULL; }Else if(Isleftnode) {//If it is left node, do node point to nullParent.leftnode =NULL; }Else{//If it is the right node, the right node points to an emptyParent.rightnode =NULL; }        }Else if(Current.leftnode = =NULL) {//The node to be deleted has only the right node            if(current = = root)            {root = Current.rightnode; }Else if(Isleftnode)            {parent.leftnode = Current.rightnode; }Else{parent.rightnode = Current.rightnode; }        }Else if(Current.rightnode = =NULL) {//The node to be deleted has only the left node            if(current = = root)            {root = Current.leftnode; }Else if(Isleftnode)            {parent.leftnode = Current.leftnode; }Else{parent.rightnode = Current.leftnode; }        }Else{//The node to be deleted has two nodesTreeNode successor = findsuccessor (current);if(current = = root)            {root = successor; }Else if(Isleftnode)            {Parent.leftnode = successor; }Else{Parent.rightnode = successor;        } Successor.leftnode = Current.leftnode; }return true; }//Find subsequent nodes    PrivateTreeNodeFindsuccessor(TreeNode Delnode)        {TreeNode parent = Delnode;        TreeNode successor = Delnode; TreeNode current = Delnode.rightnode; while(Current! =NULL) {parent = successor;            successor = current;        current = Current.leftnode; }if(Successor! = Delnode.rightnode)            {parent.leftnode = Successor.rightnode;        Successor.rightnode = Delnode.rightnode; }returnsuccessor; }}

Binary search 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.