Binary searchtree (BST)
Corresponding to Chapter 12th of Introduction to algorithms. Compared with a common Binary Tree, BST meets the unique condition: the Key of any node> the key of the left child, and the key of the right child.
1. node class:
Public class binarysearchtreesnode <t> {private int key; private t satellitedata; private binarysearchtreesnode <t> parent, leftchild, rightchild; Public binarysearchtreesnode (INT key, t satellitedata) {// The constructor stub automatically generated by todo this. key = key; this. satellitedata = satellitedata; parent = leftchild = rightchild = NULL;} public t getsatellitedata () {return satellitedata;} public void setsatellitedata (T satellitedata) {This. satellitedata = satellitedata;} public binarysearchtreesnode <t> getparent () {return parent;} public void setparent (binarysearchtreesnode <t> parent) {This. parent = parent;} public binarysearchtreesnode <t> getleftchild () {return leftchild;} public void setleftchild (binarysearchtreesnode <t> leftchild) {This. leftchild = leftchild;} public binarysearchtreesnode <t> getrightchild () {return rightchild;} public void setrightchild (binarysearchtreesnode <t> rightchild) {This. rightchild = rightchild;} public int getkey () {return key ;}}
2. Establishment of BST
/*** @ Author administrator **/public class binarysearchtree <t> {private binarysearchtreesnode <t> root; Public binarysearchtree () {// todo automatically generated constructor stub root = NULL;} public binarysearchtreesnode <t> getroot () {return root;} public void setroot (binarysearchtreesnode <t> root) {This. root = root;} public binarysearchtree (binarysearchtreesnode <t> binarysearchtreesnode) {// automatically generated todo constructor stub root = binarysearchtreesnode ;}}
Ii. BST Traversal
You can use the typical binary tree traversal method.
1. Sequential Traversal
/* Introduction to algorithms: 288-page pseudocode, In-order traversal, */Public void inordertreewalk (binarysearchtreesnode <t> binarysearchtreesnode) {If (binarysearchtreesnode! = NULL) {inordertreewalk (binarysearchtreesnode. getleftchild (); system. out. println (binarysearchtreesnode. getkey () + ":" + binarysearchtreesnode. getsatellitedata (); inordertreewalk (binarysearchtreesnode. getrightchild () ;}} public void inordertreewalk () {inordertreewalk (Root );}
3. Other BST operations 1. Search
Introduction to algorithms p290 recursive and cyclic versions
2. maximum and minimum key nodes
Introduction to algorithms p291
public static BinarySearchTreesNode<String> getMinimum(BinarySearchTree<String> binarySearchTree) {BinarySearchTreesNode<String> node=binarySearchTree.getRoot();while (node.getLeftChild()!=null) {node=node.getLeftChild();}return node;}
3. successor and precursor
Successor: the successor of node X refers to the node with the smallest key greater than the X. Key. Two cases:
1) The right subtree of X is not empty: The smallest key node of the right subtree that is followed by X.
2) If the right subtree of X is null and X is the left node, the return value is the parent node of X;
3) if the right subtree of X is null and X is the right node: if all the parent nodes of X have nodes, null is returned. As long as the parent node of X has a left node, return the parent node of the parent class node.
Pseudocode:
Tree-successor (X)
If X. Right≠ Nil
ReturnTree-minimum (X. Right)
Y = x. Parent
While Y==Nil andX=Y. Right
X=Y
Y=Y. Parent
Return Y
**************************************** ***************************************
Introduction to algorithms p293 after-school question 12.2-3 requires writing the pseudocode of the precursor. Similarly, the precursor of node X refers to the node with the largest key smaller than X. Key. Follow the above pseudocode:
Tree-predecessor (X)
If X. Left≠ Nil
ReturnTree-maximum (X. Left)
Y = x. Parent
While Y==Nil andX=Y. Left
X=Y
Y=Y. Parent
Return Y
4. insert
The inserted node must be a leaf node, so you can traverse to the final position that meets the conditions.
/** Introduction to algorithms: pseudo code on page 1, binarysearchtreesnode <t> * x traverses from the root, as long as binarysearchtreesnode. getkey () <X. getkey () is used to traverse the left, and vice versa. * temp is the insert position at the end of the traversal. x = NULL */Public void insert (binarysearchtreesnode <t> binarysearchtreesnode) {binarysearchtreesnode <t> temp = NULL; binarysearchtreesnode <t> X = root; while (X! = NULL) {temp = x; If (binarysearchtreesnode. getkey () <X. getkey () {x = x. getleftchild ();} else {x = x. getrightchild () ;}}if (temp = NULL) {root = binarysearchtreesnode;} else if (binarysearchtreesnode. getkey () <temp. getkey () {temp. setleftchild (binarysearchtreesnode);} else {temp. setrightchild (binarysearchtreesnode);} // system. out. println ("insert complete! ");}
5. Delete
Complicated: p296, Introduction to Algorithms
**************************************** ************************************
Iv. Converting BST to a two-way linked list
Reference: http://blog.csdn.net/ljianhui/article/details/22338405
JAVA Implementation: to be continued
**************************************** ************************************
[One interview question]: How to efficiently find the median of BST.
Reference: http://blog.csdn.net/hhygcy/article/details/4654305
Answer: BST is converted to a two-way linked list. A two-way linked list is sorted. The speed of two pointers is twice that of the other. The speed of the two pointers moves to the end, and the speed of the two pointers is the median.
[Introduction to algorithms-24] Binary Tree Topic 2: Binary Search Tree (BST)