Node:
/*
* Binary tree node
*/
public class Node {
Data item
public Long data;
Data item
Public String SData;
Left Dial Hand node
Public Node Leftchild;
Right child node
Public Node Rightchild;
/**
* Construction Method
* @param data
*/
Public Node (Long data,string sData) {
This.data = data;
This.sdata = SData;
}
}
Two-fork Tree:
/*
* Two-fork Tree class
*/
public class Tree {
Root node
public Node root;
/**
* Insert Node
* @param value
*/
public void Insert (long value,string svalue) {
Encapsulating nodes
Node NewNode = new node (value,svalue);
Referencing the current node
Node current = root;
Reference parent Node
Node parent;
If root is null, which is the first insertion
if (root = = null) {
root = NewNode;
Return
} else {
while (true) {
Parent node points to current node
parent = current;
If the node data that you are pointing to is larger than the insert, go left
if (Current.data > value) {
current = Current.leftchild;
if (current = = null) {
Parent.leftchild = NewNode;
Return
}
} else {
current = Current.rightchild;
if (current = = null) {
Parent.rightchild = NewNode;
Return
}
}
}
}
}
/**
* Find Nodes
* @param value
*/
Public Node find (Long value) {
Reference the current node, starting at the root node
Node current = root;
Loop, as long as the lookup value is not equal to the current node's data item
while (current.data! = value) {
Compare the lookup value and the size of the current node
if (Current.data > value) {
current = Current.leftchild;
} else {
current = Current.rightchild;
}
If you do not find the
if (current = = null) {
return null;
}
}
return current;
}
/**
* Delete Node
* @param value
*/
Public Boolean Delete (Long value) {
Reference the current node, starting at the root node
Node current = root;
Apply the parent node of the current node
Node parent = root;
is the left node
Boolean isleftchild = true;
while (current.data! = value) {
parent = current;
Compare the lookup value and the size of the current node
if (Current.data > value) {
current = Current.leftchild;
Isleftchild = true;
} else {
current = Current.rightchild;
Isleftchild = false;
}
If you do not find the
if (current = = null) {
return false;
}
}
Delete the leaf node, that is, the node has no child nodes
if (Current.leftchild = = NULL && Current.rightchild = = null) {
if (current = = root) {
root = null;
} else if (Isleftchild) {
Parent.leftchild = null;
} else {
Parent.rightchild = null;
}
} else if (Current.rightchild = = null) {
if (current = = root) {
root = Current.leftchild;
}else if (isleftchild) {
Parent.leftchild = Current.leftchild;
} else {
Parent.rightchild = Current.leftchild;
}
} else if (Current.leftchild = = null) {
if (current = = root) {
root = Current.rightchild;
} else if (Isleftchild) {
Parent.leftchild = Current.rightchild;
} else {
Parent.rightchild = Current.rightchild;
}
} else {
Node successor = getsuccessor (current);
if (current = = root) {
root = successor;
} else if (Isleftchild) {
Parent.leftchild = successor;
} else{
Parent.rightchild = successor;
}
Successor.leftchild = Current.leftchild;
}
return true;
}
Public node Getsuccessor (node Delnode) {
Node successor = Delnode;
Node successorparent = Delnode;
Node current = Delnode.rightchild;
while (current! = null) {
Successorparent = successor;
successor = current;
current = Current.leftchild;
}
if (successor! = Delnode.rightchild) {
Successorparent.leftchild = Successor.rightchild;
Successor.rightchild = Delnode.rightchild;
}
return successor;
}
/**
* Pre-sequence traversal
*/
public void Frontorder (Node localnode) {
if (Localnode! = null) {
Accessing the root node
System.out.println (Localnode.data + "," + localnode.sdata);
Pre-sequence Traversal Zuozi
Frontorder (Localnode.leftchild);
Pre-sequence traversal right subtree
Frontorder (Localnode.rightchild);
}
}
/**
* Middle Sequence traversal
*/
public void Inorder (Node localnode) {
if (Localnode! = null) {
Middle Sequence Traversal Zuozi
Inorder (Localnode.leftchild);
Accessing the root node
System.out.println (Localnode.data + "," + localnode.sdata);
Middle of the right sub-tree traversal
Inorder (Localnode.rightchild);
}
}
/**
* Post-sequential traversal
*/
public void Afterorder (Node localnode) {
if (Localnode! = null) {
Post-Traversal Zuozi
Afterorder (Localnode.leftchild);
To traverse the right sub-tree
Afterorder (Localnode.rightchild);
Accessing the root node
System.out.println (Localnode.data + "," + localnode.sdata);
}
}
}
Test:
public class Testtree {
public static void Main (string[] args) {
Tree tree = new Tree ();
Tree.insert ("James");
Tree.insert ("YAO");
Tree.insert ("Kobi");
Tree.insert (3, "Mac");
Tree.insert (4, "Zhangsan");
Tree.insert ("Lisi");
Tree.delete (90);
Tree.inorder (Tree.root);
}
}
This article is from the "8159085" blog, please be sure to keep this source http://8169085.blog.51cto.com/8159085/1696870
Lapping data structures and algorithms-13 Delete a binary tree node