Binary Tree BST ---- java Implementation, binary tree bst ---- java

Source: Internet
Author: User

Binary Tree BST ---- java Implementation, binary tree bst ---- java

Binary Search Tree BST ---- java Implementation

1. Introduction to Binary Search Tree

The binary search tree is also known as the Binary Search Tree and the binary sorting tree. Properties:

 

In the Binary Search Tree:
(01) If the left subtree of any node is not empty, the value of all nodes on the left subtree is smaller than the value of its root node;
(02) if the right subtree of any node is not empty, the value of all nodes on the right subtree is greater than the value of its root node;
(03) the left and right subtree of any node are also binary lookup trees.
(04) no node with the same key value (no duplicate nodes ).

2. Binary Tree node search

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

The traversal of the binary search tree is the same as that of the binary tree. For details about recursive and non-recursive methods, see recursive and non-recursive traversal of the binary tree (for example).

4. maximum and minimum values

A. The minimum value in BST is the leftmost child.

// Calculate the minimum value of BST public TreeNode getMin (TreeNode root) {if (root = null) return null; while (root. left! = Null) root = root. left; return root ;}

B. The maximum value in BST is the rightmost child.

// Calculate 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. Precursor and successor nodes


Ps: images come from the network

A. In BST, a node's precursor node = is smaller than the maximum value among all nodes of the node

Precursor easy situation: 5 find the precursor 4

Precursor complexity: 11. Search for precursor 10

// Find the front node of a node in BST. That is, find the node whose data value is smaller than the maximum node of the node. Public TreeNode preNode (TreeNode x) {if (x = null) return null; // if x exists, then, "x's precursor node" is "the maximum node of the subtree with its left child as the root ". If (x. left! = Null) return getMax (x. left); // If x has no left child. Then there are two possibilities for x: // (01) x is "a right child", then "x's precursor node" is "its parent node ". // (02) x is "a left child", the parent node is 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; // The parent node is set to x p = p. parent; // set the parent node of the parent node to the new parent node} return p ;}
B. The successor node of a node in BST is greater than or equal to the minimum value of all nodes of the node.

Easy follow-up: 5 find next 6

Complex Scenario: 9 find successor 10

 

// Find the successor node of a node in BST. That is, find the smallest node with a data value greater than the node. Public TreeNode postNode (TreeNode x) {if (x = null) return null; // if x has the right child, then, "the successor node of x" is "the smallest node of the subtree with its right child as the root ". If (x. left! = Null) return getMin (x. right); // If x does not have the right child. Then, x has two possibilities: // (01) x is "a left child", and "x's successor node" is "its parent node ". // (02) x is "a right child", the parent node is the parent node 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; // The parent node is set to x p = p. parent; // set the parent node of the parent node to the new parent node} return p ;}

6. Search

Find a node with the value of val. If it is smaller than the root node, search in the left subtree, and vice versa.

// Find the node whose value is val -- 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 the node whose value is val -- Non-recursive version -- public TreeNode search (TreeNode root, int val) {if (root = null) return root; while (root! = Null) {if (val <root. value) root = root. left; else if (val> root. value) root = root. right; elsereturn root;} return root ;}

7. insert

A. If the current Binary Search Tree is empty, the inserted element is the root node.

B. If the inserted element value is smaller than the root node value, insert the element into the left subtree.

C. If the inserted element value is not smaller than the root node value, insert the element into the right subtree. First, locate the insert location, either left or right, until an empty node is found, that is, the insert location. If a node with the same value is found, insertion fails.

// 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; // you need to record the parent node while (root! = Null) // locate the inserted position {p = root; // record the parent node if (x. value <root. value) root = root. left; elseroot = root. right;} x. parent = p; // After locating the blank space of the appropriate page node, insert the appropriate position if (x. value <p. value) p. left = x; else if (x. value> p. value) p. right = x; return root ;}

8. Delete

Delete the Binary Search Tree in three cases:
1. p is a leaf node. Delete the node directly, and then modify the pointer of its parent node (note that it is a root node or not a root node),.
2. p is a single node (that is, only the left or right subtree ). Connect the child tree of p to the Father's Day of p and delete p. (Note that the root node is different from the root node.) B.
3. If there are two children, the current node switches to the largest element in the left subtree, and then deletes the current node. The biggest element of the Left subtree must be the leaf node. After the switch, the current node is the leaf node. for deletion, see the absence of children. Another method is to swap the current node with the smallest element in the right subtree and then delete the current node. C.


Ps: images come from the network


// BST delete node public void delete (TreeNode root, TreeNode x) {if (root = null) return; TreeNode p = null; while (root! = Null) // locate the node to be deleted {if (x. value <root. value) {p = root; // record parent node root = root. left;} else if (x. value> root. value) {p = root; // record parent node root = root. right;} else // found {if (root. left = null & root. right = null) // ① The leaf node to be deleted is {if (p = null) // The root node to be deleted is 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 only has the left child {if (p = null) // The root node to be deleted is root = root. left; else {if (p. left = root) // the child to be deleted 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 only has the right child {if (p = null) // The root node to be deleted is root = root. right; else {if (p. left = root) // the child to be deleted is a left child p. left = root. right; else if (p. right = root) p. right = root. right ;}} else // ③ if the node to be deleted has left and right children, you can obtain the minimum value of the right subtree of the node to be deleted, {// the minimum value is exchanged with the value of the node to be deleted. The Node TreeNode rMin = root at the minimum value is deleted. right; // find the successor node of the node to be deleted, that is, the minimum value of the right child of the node to be deleted (No Child Left is found for the successor node !!!) TreeNode rMinP = null; // you need to record the parent node while (rMin! = Null) {rMinP = rMin; rMin = rMin. left;} int rootVtemp = root. value; // value exchange root. value = rMin. value; rMin. value = rootVtemp; // Delete the node at the rMin position. The value at this position is the value of the node to be deleted if (rMinP. left = rMin) rMinP. left = rMin. right; else if (rMinP. right = rMin) rMinP. right = rMin. right ;}} break; // After finding and deleting it, it jumps out of the while LOOP }}



Traversal of Binary Trees in urgent java programs and Binary Search Tree

Let's get a treeset. Do you want to learn java?
 
What is the difference between a binary search tree and a binary search tree? Tutorial

What is the difference between a binary search tree and a binary search tree?
In terms of average time performance, the query on the binary sorting tree is similar to that on the binary query tree.
In terms of table order maintenance, the binary sorting tree does not need to move nodes. You only need to modify the pointer to complete the insert and delete operations, and its average execution time is O (lgn), which is more effective. Binary Search involves an ordered table as a vector. If a node is inserted or deleted, O (n) is used to maintain the order of the table ). When an ordered table is a static query table, vector should be used as its storage structure, and binary search should be used for the query operation. If the ordered table is dynamically searched, the binary sorting tree should be selected as its storage structure.

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.