BST (binary search tree) Java Implementation analysis

Source: Internet
Author: User

1. Two fork search tree definition: All the left subtree of a tree is smaller than the root, all the right subtree is larger than the root, become a binary search tree.

2. The BST tree implements 9 important methods, namely keyword lookup, insert, delete, delete node follow-up node lookup, pre-sequence traversal, middle sequence traversal, post-order traversal, get maximum node, get minimum node.

3. The following is the Java code implementation:

Define the node class, with the element value, node name, left child node, right child node, 4 member variables.
Class Node {
int element;
String name;
Node Leftchild;
Node Rightchild;

public Node (int element, String name) {
This.element = element;
THIS.name = name;
}
}
public class Binsearchtreedemo {
public Node root;
Find nodes
Method of thinking: Starting from the root, if smaller than the root, look for the left child, if larger than the root, find the right child, if equal, then return the node.
Public Node find (int key) {
if (root==null) {
System.out.println ("The Tree is empty ...");
return null;
}
Node Current=root;
while (Current.element!=key) {
if (Current.element>key) {
Current=current.leftchild;
}else{
Current=current.rightchild;
}
if (current==null) {
return null;
}
}
return current;
}
Inserting nodes
Method of thinking: from the root node to traverse, if smaller than the root, continue to find the left child, if larger than the root, then continue to find the right child, has been found to a node left child or right child is null, then the node is inserted in the subsequent node.
Note: The inserted element must be a leaf node in the tree
public boolean Insert (node node) {
if (root==null) {
Root=node;
return true;
}
Node Current=root;
if (Find (node.element)!=null) {
SYSTEM.OUT.PRINTLN ("Duplicate nodes are not allowed ...");
return false;
}
while (Current!=null) {
if (current.element>node.element) {
if (current.leftchild==null) {
Current.leftchild=node;
return true;
}
Current=current.leftchild;
}else{
if (current.rightchild==null) {
Current.rightchild=node;
return true;
}
Current=current.rightchild;
}
}
return false;
}
Pre-sequence traversal (around the root)
public void Preorder_iterator (node node) {
System.out.println (node.element+ "");
if (node.leftchild!=null) {
Preorder_iterator (Node.leftchild);
}
if (node.rightchild!=null) {
Preorder_iterator (Node.rightchild);
}
}
Middle sequence traversal (left root right)
public void Inorder_iterator (node node) {
if (node.leftchild!=null) {
Inorder_iterator (Node.leftchild);
}
System.out.println (node.element+ "");
if (node.rightchild!=null) {
Inorder_iterator (Node.rightchild);
}
}
Post-sequential traversal (left and right root)
public void Postorder_iterator (node node) {
if (node.leftchild!=null) {
Postorder_iterator (Node.leftchild);
}
if (node.rightchild!=null) {
Postorder_iterator (Node.rightchild);
}
System.out.println (node.element+ "");
}
Get the smallest node in the tree
Public node Getminnode (node node) {
if (Find (node.element) ==null) {
SYSTEM.OUT.PRINTLN ("This node does not exist ...");
return null;
}
Node Current=node.leftchild;
while (Current!=null) {
Current=current.leftchild;
}
return current;
}
Get the largest node in the tree
Public node Getmaxnode (node node) {
if (Find (node.element) ==null) {
SYSTEM.OUT.PRINTLN ("This node does not exist ...");
return null;
}
Node Current=node.rightchild;
while (Current!=null) {
Current=current.rightchild;
}
return current;
}
Delete a node
Method of thinking:
1. First find the deletion node and the node corresponding to the Father node, respectively, with the target variable and the targetparent variable is saved, and use a Boolean variable to save the deleted node is the father's left child or right child, the left child is true, the right child is
False
2. Next node deletion, deletion needs to be divided into three cases, the first is to delete the leaf node, the case is if the father's left child is deleted, the father's leftchild is directly set to Target's left child, if the right child is deleted, then// The rightchild of the father is directly set to Target's left child, which is null; the second case is to remove the node with a child node, which can be left child or right child, need to be processed separately if the deleted node itself is Zuozi//, and with left child, Then point the left child of Targetparent to Target's left child, and if it has a right child, point the left child of the targetparent to the right child of target, and if the deleted node itself is a right subtree with left child//, Point the targetparent right child to Target's left child, or point to the right child. The third case is the deletion of a node with two children, which is slightly more complicated, and we individually explain
3. Analysis of node deletion with two children: The method is accompanied by a Getfollwingnode method, which is designed to capture the subsequent nodes of the deleted node, and the idea is as follows.
①. Locate the right subtree node of the delete node, call the Getfollowingnode method to find the leftmost node in the right subtree of the deleted node, that is, the smallest node, and return the smallest node, at which point the structure of the right subtree is adjusted, and the//parent node of the node is returned to the right subtree of the returned node ( If it exists, the left subtree must not exist.
②. The parent node of the deleted node points to the returned node (note the left and right conditions), then points the left pointer of the returned node to the left child of the deleted node, and the right pointer to the child to delete the node, ending.
public boolean Delete (int key) {
if (root==null) {
System.out.println ("The Tree is empty ...");
return false;
}
Node Targetparent=root;
Node Target=root;
Boolean isleftchild=false;
Locate the corresponding Delete node target and its parent node targetparent
while (Target.element!=key) {
if (key<target.element) {
Targetparent=target;
Target=target.leftchild;
Isleftchild=true;
}else{
Targetparent=target;
Target=target.rightchild;
Isleftchild=false;
}
if (target==null) {
SYSTEM.OUT.PRINTLN ("The target node does not exist ...");
return false;
}
}
If the deleted node is a leaf node
if (target.leftchild==null&&target.rightchild==null) {
if (root.element==target.element) {
Root=null;
return true;
}
if (isleftchild) {
Targetparent.leftchild=target.leftchild;
}else{
Targetparent.rightchild=target.leftchild;
}
}
If the deleted node has only one child node
If the node is Zuozi
else if (target.leftchild!=null&&target.rightchild==null) {
if (root.element==target.element) {
Root=target.leftchild;
return true;
}
if (isleftchild) {
Targetparent.leftchild=target.leftchild;
}else{
Targetparent.leftchild=target.rightchild;
}
}
If the node is the right child tree
else if (target.leftchild==null&&target.rightchild!=null) {
if (root.element==target.element) {
Root=target.rightchild;
return true;
}
if (isleftchild) {
Targetparent.rightchild=target.leftchild;
}else{
Targetparent.rightchild=target.rightchild;
}
}
else{
Node Followingnode = This.getfollowingnode (target);
if (target.element = = root.element)
root = Followingnode;
else if (isleftchild)
Targetparent.leftchild = Followingnode;
Else
Targetparent.rightchild = Followingnode;
Followingnode.leftchild = Target.leftchild;
Followingnode.rightchild = Target.rightchild;
}

return true;
}
Get Delete node next node
Public node Getfollowingnode (node Node2del) {
Node nodeparent = Node2del;
This method is called only if the deleted node has left and right child nodes
There is no problem in calling Rightchild directly here.
Node node = node2del.rightchild;
while (node.leftchild! = null) {
nodeparent = node;
node = Node.leftchild;
}
if (node.element! = node2Del.rightChild.element)
Nodeparent.leftchild = Node.rightchild;
Else
Nodeparent.rightchild = Node.rightchild;
return node;
}
}

BST (binary search tree) Java Implementation analysis

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.