Binary search tree BST----Java implementation

Source: Internet
Author: User

Binary search tree BST----Java implementation

1. A simple introduction to the two-fork search tree

Binary lookup tree aka binary search tree and two-fork sort tree. Properties such as the following:

In the binary lookup tree:
(01) If the left subtree of the random node is not empty, the value of all nodes on the left subtree is less than the value of its root node.
(02) The right subtree of the random node is not empty, then the value of all nodes on the right subtree is greater than the value of its root node;
(03) The left and right sub-trees of random nodes are also two-fork search trees respectively.


(04) No nodes with key values equal (no duplicate nodes).

2. Two fork find tree node class

class Treenode{int value; TreeNode parent; TreeNode left; TreeNode right;public TreeNode (int value, TreeNode parent, TreeNode left, TreeNode right) {this.value = Value;this.parent = Parent;this.left = Left;this.right = right;} }

3. Traverse

Traversal of binary search tree with binary tree traversal, recursive and non-recursive methods see recursive traversal and non-recursive traversal of the two-tree (with specific examples)

4. Maximum and minimum values

A. Theminimum value in BST is the leftmost child.

Minimum value for BST public TreeNode  getmin (TreeNode root) {if (root==null) return Null;while (root.left!=null) root= Root.left; return root;}

The maximum value in B.bst is the right child.

Find the maximum value of BST public TreeNode  Getmax (TreeNode root) {if (root==null) return Null;while (root.right!=null) root= Root.right;return Root;}

5. Precursors and successor nodes


PS: Pictures come to the web

The predecessor node = = of a node in A.bst is less than the maximum value in all nodes of the node

Precursor Easy scenario: 5 Seeker 4

Precursor complex scenario: 11 Seeker 10

Finds the predecessor node of a node in BST. That is, the lookup data value is less than the maximum node for that junction. Public TreeNode Prenode (TreeNode x) {if (X==null) return null;//assumes that X has a left child, then "X's Precursor node" is the largest node of the subtree with its left child as its root.

if (x.left!=null) return Getmax (x.left); Suppose X has no left child.

Then X has the following two possibilities: //(a) x is "a right child", then "X's predecessor Node" is "its parent node". //() x is "One left child", the parent node of an ancestor node of X, and the ancestor node is the right son of its parent node TreeNode p=x.parent; while (p!=null&&p.left==x) { x=p;//parent node is placed as a new x p=p.parent; Parent node's parent node is set to the new parent node } return p;

a node successor in B.BST = = is greater than the minimum value in all nodes of the node

Successor Easy scenario: 5 Seek successor 6

Complex scenario: 9 seek successor 10

Finds the successor node of a node in BST. That is, the lookup data value is greater than the minimum node of the junction.

Public TreeNode Postnode (TreeNode x) {if (x==null) return null;//assuming X exists right child, then "X's successor" is "the smallest node of a subtree with its right child root". if (x.left!=null) return getmin (x.right); Suppose X has no right child. Then X has the following two possibilities: // (a) x is "a left child", then "X's successor" is "its parent node". //(a) x is a "right child", the predecessor node is the parent of an ancestor node of X, and the ancestor node is the left son of its parent node TreeNode p=x.parent; while (p!=null&&p.right==x) { x=p;//parent node is placed as a new x p=p.parent; Parent node's parent node is set to the new parent node } return p;


6. Find

Look for a node with a Val value, assuming that the small root node looks in the left subtree, and vice versa in the right subtree.

Find a node with a Val value-  -recursive version--public TreeNode searchrec (TreeNode root, int val) {if (root==null) return root;if (val< Root.value) return Searchrec (Root.left,val), else if (Val>root.value) return Searchrec (root.right,val); Elsereturn Root;} Find a node with a value of Val  

7. Insert

A. If the current two-fork lookup tree is empty, the inserted element is the root node

B. If the inserted element value is less than the root node value. The element is inserted into the record

c. If the inserted element value is not smaller than the root node value, the element is inserted into the right subtree. First find the insertion position, either to the left, or to the right, until the empty node is found, that is, the insertion position, assuming that the same value of the node was found, the insertion failed.


BST insert node-  -recursive version--public TreeNode insertrec (TreeNode root,treenode x) {if (root==null) root=x;else if (x.value< Root.value) Root.left=insertrec (Root.left,  x), else if (x.value>root.value) Root.right=insertrec (Root.right,  x); return root;} BST insert node--  non-recursive version--public TreeNode Insert (TreeNode Root,treenode x) {if (root==null) root=x; TreeNode p=null;//need to record parent node while (root!=null)//Position inserted location {p=root;//Record parent node if (x.value<root.value) root=root.left; Elseroot=root.right;} x.parent=p;//navigates to the appropriate page node after the empty space. Insert the appropriate position according to the size of the parent node if (x.value<p.value) P.left=x;else if (x.value>p.value) p.right=x;return root;}

8. Delete

Binary search tree deletion, in three different situations to deal with:
1.P is a leaf node. Delete the node directly, and then change the pointer to its parent node (note that the root node and not the root node), a.
2.P is a single node (i.e. only Zuozi or right subtree).

Let the child tree of P be connected to the parent node of P and delete p. (Note that the points are the root node and not the root node); b.


3. in the case of two children, the current node is exchanged with the largest element in the left dial hand tree. Then delete the current node. The largest element of the left dial hand tree must be the leaf node, after Exchange. The current node is the leaf node. Delete the case where no child is in the examination. Another way is to exchange the current node with the smallest element in the right subtree, and then delete the current node . C.



PS: Pictures come to the web


BST delete node public void Delete (TreeNode Root,treenode x) {if (root==null) return; TreeNode p=null;while (root!=null)//Locate the node that needs to be deleted {if (X.value<root.value) {p=root;//Record parent node root=root.left;} else if (x.value>root.value) {p=root;//Records parent node root=root.right;} else//found {if (root.left==null&&root.right==null)//① to be deleted is the leaf node {if (P==null)//To be deleted is the root node root=null;else{if ( P.left==root) P.left=null;else if (p.right==root) p.right=null;}} else if (root.left!=null&&root.right==null)//② the node to be deleted has only the left child {if (p==null)//The root node root=root.left;else{if ( P.left==root)//To be deleted itself is a left child p.left=root.left; else if (p.right==root) P.right=root.left;}} else if (root.left==null&&root.right!=null)//② the node to be deleted has only the right child {if (p==null)//The root node root=root.right;else{if (  P.left==root)//To be deleted itself is a left child p.left=root.right;  else if (p.right==root) p.right=root.right;    }}else//③ the node to be deleted there is a left child and a right child method: Gets the minimum value of the right subtree of the node to be deleted. {//The minimum value is exchanged with the node to be deleted. Delete the node at the minimum position TreeNode Rmin=root.right; The successor of the node to be deleted, that is, the minimum value of the right child of the node to be deleted (the succeeding node found must have no left child!!). ) TreeNode rminp=null;//The parent node while (rmin!=null) {rminp=rmin;rmin=rmin.left;} int rootvtemp=root.value;//value Exchange root.value= is required to delete the successor node location. Rmin.value; Rmin.value=rootvtemp; Delete the node at the RMin location, at which point the value of this position is already the value of the IF (rminp.left==rmin) rminp.left=rmin.right; else if (rminp.right==rmin) rminp.right=rmin.right;}} break;//found and then deleted and then jumped out while loop}}

9. Two tree search tree frequently meet questions

A. Inferring whether an array is a sequential traversal of a binary search tree

Package Com.sheepmu;public class Offer24 {public static void main (string[] args) {int[] a={5,7,6,9,11,10,8};int len= A.length; System.out.println (Isproofbst (A,len));} public static Boolean Isproofbst (int[] A,int len) {if (a==null| | len<=0) return False;int root=a[len-1];//The last of the post-order traversal is the root node int i=0;while (a[i]<root)//Find Zo Shu number I++;int j=i;// First see if there is an illegal number in the right tree, that is, a number smaller than the root node while (j<len-1) {if (a[j]<root) return false;j++;} If the numbers of the left and right subtree are legal, that is, the tree is smaller than the root value, and the tree is larger than the root node; At this point, only recursive inference to whether the left and the sub-tree is a two-fork search tree of the post-order traversal//to the left and right of the sub-tree array, it is obvious Left=true;if (i>0)//must infer whether there is a left tree {int[] aleft=new int[i];for (int x=0;x<i;x++) aleft[x]=a[x];  Left=isproofbst (aleft,i);} Boolean right=true;if (i<len-1)//must infer whether there is a right tree {int[] aright=new int[len-i-1];//for (int y=i;y<len-1;y++)//careless AH!!!!

{//aright[y]=a[y];//}for (int y=0;y<len-i-1;y++) aright[y]=a[i+y]; Right=isproofbst (aright,len-i-1);} return left&&right;}}

B. Converting a binary search tree into a sorted doubly linked list



Binary search tree BST----Java implementation

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.