Binary search tree (Java)

Source: Internet
Author: User

/***************** Search Binary Tree *********************///"Introduction to Algorithms" p161/* the desired height of a two-search tree with n different keywords is h = O (LGN); The time complexity for all lookups, such as the following, is O (h) *//****** definition search binary tree *****///for either node X, the node that satisfies its left subtree key is not larger than the node on the right subtree of x.key//key is not less than x.keyprivate Class Searchtree{int key; Searchtree parent; Searchtree left; Searchtree right;} /********************* Query *********************///Recursive implementation public Searchtree Search1 (searchtree tnode, int target) {if ( Tnode = = NULL | | target = = Tnode.key) {return tnode;} if (Target > Tnode.key) {return search (tnode.right,target);//recursive query right subtree}else{return search (tnode.left,target);}} Iterative implementation (much higher efficiency) public searchtree Search (searchtree tnode,int target) {while (tnode! = NULL && target! = Tnode.key {if (Target > Tnode.key) {tnode = Tnode.right;} Else{tnode = Tnode.left;}} return tnode;} /****************** query Maximum minimum keyword ***************///min keyword public searchtree mintreenode (Searchtree tnode) {while ( Tnode.left = null) {tnode = Tnode.left;} return tnode;} Minimum keyword public searchtree maxtreenode (Searchtree tnode) {while (TNOde.right = null) {tnode = Tnode.right;} return tnode;} /***************** finding successors and precursors ******************//* to find the successor of a node *//* note there are two situations: 1. If the node has a right node, the successor is the leftmost node of its right node 2. If the node has no Right subtree, then the ancestor node should be back-to-back for consideration 1) If the node is the left child node of its parent node X.P, then X.key is less than X.p.key 2) if the node is the right child node of the parent node, the successor that finds greater than its key is still not satisfied;  To a node z, so that x record in its z. */public searchtree treesuccessor (Searchtree xnode) {if (xnode.right! = null) {return mintreenode (xnode.right);// That is, find the minimum keyword for the right subtree}searchtree ynode = Xnode.parent;while (Ynode! = null && XNode = = ynode.right)// Nil before looping to the root node, or the node is the left child node of the parent node {xnode = Ynode; Ynode = ynode.parent;}  Return ynode;//Note The returned value}/* find the precursor of a node *//* analogy to the question of finding a precursor, 1. When the node has a left dial hand tree, the precursor is the maximum keyword 2 for its left subtree. If the node has only a right subtree, the ancestor node is returned to consider Until the node in the right subtree of the node x z is found, Z is the precursor of x */public searchtree treepredecessor (Searchtree xnode) {if (xnode.left! = null) {return Maxtreenode (xnode.left);} Searchtree Ynode = Xnode.parent;while (Ynode! = null && XNode = = ynode.left) {xnode = Ynode; Ynode = Ynode.left;} return ynode;} /*****************Insert Node **********************//* Note The feature of the search binary tree, the inserted node must eventually become a two-tree leaf node by constantly comparing X.key with node Y key, determine whether node x should be in Y Zuozi or right subtree At the same time processing two fork tree to pay attention to the particularity of the root node at all times, consider */public void Treeinsert (Searchtree troot, Searchtree xnode) {Searchtree Ynode = Null;while ( Troot! = null) {Ynode = troot;//Avoid exiting the loop when Troot is null and cannot be accessed if (Xnode.key > Troot.key) {troot = Troot.right;} Else{troot = Troot.left;}} Xnode.parent = ynode;//Note the root node condition if (Ynode = = null)//That is troot=null, does not enter the loop {troot = XNode;} To determine whether XNode is a left or right node else if (Xnode.key < Ynode.key) {ynode.left = XNode;} Else{ynode.right = XNode;}} /********************* Deleting a node *********************//* Delete to consider three cases: (temporarily not consider the case of xnode not in the tree) 1.XNode is a leaf node, no child, then delete it, and the parent node corresponding to the child node is set to null 2.XNode has a child node ynode, whether it is the child, will ynode replace XNode can be 3.XNo                    De has two children nodes, to be divided into the following two cases 1) XNode Right child node Ynode no left subtree 2) Ynode has Zuozi: (x) (x)                                   /\/() (y) () (y)/\              /        NIL () (m) direct ynode to replace XNode (n) Note to find the successor Mnode of XNode, the leftmost node of the right subtree, let Mnode replace XNode, and notice that the right subtree of the Mnode replaces the mnode*///alternative auxiliary function, note that the substitution of this is only the location of the Vnode Exchange to Unode, Unode itself has not changed, successor has not inherited private void transplant (Searchtree Troot, Searchtree Unode, Searchtree vnode) {//Consider the case of the root node if (unode.parent = = null) {troot = Vnode;} else if (Unode = = UNode.parent.left) {UNode.parent.left = Vnode;} Else{unode.parent.right = Vnode;} if (Vnode! = NULL)//Note This condition determines that NULL is unable to set various states of {vnode.parent = unode.parent;}} /********************** Delete function ************************/public void Treedelete (Searchtree troot, SearchTree XNode) {// Situation one if (Xnode.left = = NULL && Xnode.right = = null) {XNode = null;//omit to determine if xnode is the root node}//condition two else if (xnode.left! = null) { Transplant (troot,xnode,xnode.left);} else if (xnode.right! = null) {transplant (troot,xnode,xnode.right);} Situation three else{searchtree ynode = Xnode.right;Case 3.1 if (ynode.left = = null) {transplant (Troot,xnode,ynode);//After swapping note set child ynode.left = Xnode.left; YNode.left.parent = Ynode;} Situation 3.2else{searchtree Mnode = Mintreenode (Ynode); Transplant (troot,mnode,mnode.right);//replace mnode//with Nnode note mnode changes mnode.right = Xnode.right; MNode.right.parent = mnode;//to be aware of the parenttransplant (troot,xnode,mnode) Setting XNode the child's node; Mnode.left = Xnode.left;                 MNode.left.parent = Mnode;}}}

Binary search tree (Java)

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.