About the basic algorithms of the Binary Search Tree (three ways to implement query operations)

Source: Internet
Author: User

About the basic algorithms of the Binary Search Tree (three ways to implement query operations)

We have discussed the implementation of recursive non-Recursive Algorithms for sequential traversal in the forward and backward directions of a binary tree. Next we will talk about the binary search tree ~

The binary sorting tree is divided into two types: static search (find) and dynamic search (insert, delete) Binary Search Tree: a binary tree can be empty. If it is not empty, it meets the following requirements: 1. all key values of the Left subtree are smaller than those of the root node. 2. All key values of the non-empty right subtree are greater than the key values of the Root Node 3. Left and right subtree are binary search trees !!
2. The above are some basic operations of the Binary Search Tree (also called the binary sorting tree). Here we will first talk about the node definition of the binary tree ·· The auxiliary methods and simple get methods for determining the current node location in the code can be completed within the constant time, And the implementation is also very simple. The following describes the implementation and time complexity of updateHeight (), updateSize (), sever (), setLChild (lc), and getRChild (rc.
1) <喎?http: www.bkjia.com kf ware vc " target="_blank" class="keylink"> Vc3Ryb25nPgo8c3Ryb25nPiAgICAvLyC4/NDCtbHHsL3hteO8sMbk1 + bPyLXEuN + hybrid + 2yLP1yry7r86qIDAsuN + hybrid + CjxzdHJvbmc + hybrid KTsvL7e1u9jBvdXf1tC9z7TztcTSu7j2PC9zdHJvbmc + CjxzdHJvbmc + quit Examples/NDC1 + bPyLXEuN + examples/IveG147XEuN + 2 yKGjx + vxotlio6ztydpa0ru49r3hteo1xljft1_osn6ses7r6osu + HTsM/s Release/release + 2yNbQtPPV37zTIDGjrLb41/release/dPDxObQ0M/yyc + jrNLAtM64/NDCuPfX5s/IveG147XEuN + release/release + 2yMO7 09C3osn6seS7r6Osy + O3qL/users + CjxzdHJvbmc + users/ItcTX08vvyv08L3N0cm9uZz4KPHN0cm9uZz5wdWJs Region + Region Release/ItcS55sSjPC9zdHJvbmc + CjxzdHJvbmc + fTwvc3Ryb25nPgo8c3Ryb25nPnVwZGF0ZVNpemUKICgpo7rNrNH5yOe5 + release ZbzTyc + release +/release + CjxzdHJvbmc + IAogICAgICAgcmV0dXJu Container + CjxzdHJvbmc + container + CjxzdHJvbmc + IAogICBlbHNlPC9zdHJvbmc + CjxzdHJvbmc + container/IuebE Authorization + CjxzdHJvbmc + IH0gPC9zdHJvbmc + CjxzdHJvbmc + authorization + O3qNDo0qrQ3rjEIHYg0 + signature/Signature + signature + bPyLXEuN + Signature BikKID0gpq8gKGxldmVsKHYpKaGjPGJyPgogICAgNKOpPC9zdHJvbmc + CjxzdHJvbmc + callback B25nPgo8c3Ryb25nPiAKICAgfSAvLyC2z7 + release Alibaba/IuN + Alibaba/NDCtbHHsL3hteO8sMbk1 + Alibaba/O6otfTPC9zdHJvbmc + CjxzdHJvbmc + Alibaba Merge + CjxzdHJvbmc + merge AGlsZCgpKSB7PC9zdHJvbmc + CjxzdHJvbmc + collate + u94bXjtcS52M + 1PC9zdHJvbmc + CjxzdHJvbmc + merge Container + CjxzdHJvbmc + container/container + CjxzdHJvbmc + c2V0TENoaWxkKGxjKaGiIGdldFJDaGls ZChyYymjusG9uPbL47eotcS5psTcz + large + yB2INPQ1/large/O6otfTtcS52M + 1oaMKIMbktM6jrCC199PDIGxjLiBzZXZlcigpts +/qsbk0 + large 99PDIHYuCiB1cGRhdGVTaXplICgp0 + rotate/Cw + rotate/rotate "brush: java;"> public class BinSearchTreeTest01 {public Point root; // Access Node public static void visitKey (Point p) {System. out. println (p. getKey () + "");} // constructor public static Po Int Tree () {Point a = new Point ('A', null, null); Point B = new Point ('B', null, ); point c = new Point ('C', null, null); Point d = new Point ('D', B, C); Point e = new Point ('E ', null, null); Point f = new Point ('F', e, null); Point g = new Point ('G', null, F ); point h = new Point ('h', d, g); return H; // root} // calculate the height of a binary tree = max (HL, HR) + 1 public static int height (Point p) {int HL, HR, MaxH; if (p! = Null) {HL = height (p. getLeftChild (); HR = height (p. getRightChild (); MaxH = Math. max (HL, HR); return MaxH + 1;} return 0;} // calculate the leaf node public static void OrderLeaves (Point p) {if (p! = Null) {if (p. getLeftChild () = null & p. getRightChild () = null) visitKey (p); OrderLeaves (p. getLeftChild (); OrderLeaves (p. getRightChild ();} public static void main (String [] args) {System. out. println ("the height of a binary Tree is:" + height (Tree (); System. out. print ("the leaf node of the binary Tree is:"); OrderLeaves (Tree () ;}} class Point {private char key; private Point LeftChild, RightChild; public Point (char key, point LeftChild, Point RightChild) {this. key = key; this. leftChild = LeftChild; this. rightChild = RightChild;} public Point (char key) {this (key, null, null);} public char getKey () {return key;} public void setKey (char key) {this. key = key;} public Point getLeftChild () {return LeftChild;} public void setLeftChild (Point leftChild) {LeftChild = leftChild;} public Point getRightChild () {return RightChild ;} public void setRightChild (Point rightChild) {RightChild = rightChild ;}}3. Now let's start the question: Query, insert, and delete the binary search tree. 1) Find · search starts from the root node. If the fruit tree is empty, NULL is returned. If the search tree is not empty, the root node keyword is compared with X and processed differently. 1) if X is less than the root node key value, you only need to search in the left subtree; 2) If X is greater than the root node key value, search in the right subtree; 3) if the comparison results are the same, after the search is complete, this node is returned.
Import java. util. collections;/*** operation collection of the Binary Search Tree ** @ author Administrator **/class BinSearchTreeTest02 {public Point1 root; // Access Node public static void visitKey (Point1 p) {System. out. println (p. getKey () + "");} // constructor public static Point1 Tree () {Point1 m = new Point1 (4, null, null ); point1 a = new Point1 (6, m, null); Point1 B = new Point1 (5, null, a); Point1 c = new Point1 (14, null, null ); point1 d = new Point1 (10, B, c); Point1 e = new Point1 (24, null, null); Point1 f = new Point1 (25, e, null ); point1 g = new Point1 (20, null, f); Point1 h = new Point1 (15, d, g); return h; // root} // construct the empty tree public static Point1 EmptyTree (int t) {Point1 a = new Point1 (t, null, null); return ;} // ************************************ search operation *** * ***********************/***** method of searching for Binary Search Trees 1 *** @ param t * @ param node * @ return */public static Boolean contains (int t, Point1 node) {if (node = null) return false; // The node is empty and the search fails. String st = Integer. toString (t); // convert the node to be searched to the String type int result = compareTo (st, node); if (result = 1) {return true ;} else {return false ;}} public static int compareTo (String a, Point1 p) {// String B = Integer. toString (p. getKey (); // if (. equals (B) is equivalent to the following line if (. equals (p. getKey () + "") return 1; int I = 0, j = 0; if (p. GetLeftChild ()! = Null) {I = compareTo (a, p. getLeftChild ();} if (p. getRightChild ()! = Null) {j = compareTo (a, p. getRightChild ();} if (I = 1) {return I;} else if (j = 1) {return j;} else {return-1 ;}} /*** method 2 of the binary search tree search operation (recursive algorithm) method ** @ param t * @ param node * @ return */public static Point1 contains2 (int t, point1 node) {if (node = null) return null; // The node is empty. if (t> node fails to be searched. getKey () return contains2 (t, node. getRightChild (); // find the right subtree else if (t <node. getKey () return contains2 (t, n Ode. getLeftChild (); // search for the left subtree elsereturn node;}/*** deformation of search method 2 found in the binary search tree, non-recursive algorithms are more efficient. Change tail recursion to the iteration function ** @ param args */public static Point1 contains3 (int t, Point1 node) {while (node! = Null) {if (t> node. getKey () node = node. getRightChild (); else if (t <node. getKey () node = node. getLeftChild (); elsereturn node;} return null;}/*** the maximum element of the Binary Search Tree. Based on its nature, the greatest element of the binary search tree must be at the rightmost end of the right subtree, the smallest element is the opposite ** @ param args */public static int findMax (Point1 point) {if (point! = Null) {while (point. getRightChild ()! = Null) {point = point. getRightChild () ;}} return point. getKey () ;}public static int findMin (Point1 point) {if (point = null) return 0; // first judge whether the tree is empty else if (point. getLeftChild () = null) return point. getKey (); // determine whether the left subtree is empty elsereturn findMin (point. getLeftChild ();} public static void main (String [] args, Point1 p) {System. out. println ("the maximum node of this binary Tree is:" + findMax (Tree (); System. out. println ("The minimum node of this binary tree is:" + findMi N (Tree (); @ SuppressWarnings ("resource") condition = new condition (System. in); System. out. println ("enter a node to determine whether it is the node of this binary tree"); int a = vertex. nextInt (); if (contains2 (a, Tree ())! = Null) System. out. println (a + "is the node of this binary tree"); else System. out. println (a + "not the node of this binary tree") ;}} class Point1 {private int key; private Point1 LeftChild, RightChild; public Point1 (int key, Point1 LeftChild, Point1 RightChild) {this. key = key; this. leftChild = LeftChild; this. rightChild = RightChild;} public Point1 (int key) {this (key, null, null);} public int getKey () {return key;} public void setKey (char key) {this. key = key;} public Point1 getLeftChild () {return LeftChild;} public void setLeftChild (Point1 leftChild) {LeftChild = leftChild;} public Point1 getRightChild () {return RightChild ;} public void setRightChild (Point1 rightChild) {RightChild = rightChild ;}}

(Three different search ideas are provided, but they are indeed better than the other two. I hope you can use them for some reference and put them in later versions for insertion and deletion)

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.