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 void Delte (Long value) {
}
}
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");
System.out.println (Tree.root.data);
System.out.println (Tree.root.rightChild.data);
System.out.println (Tree.root.rightChild.leftChild.data);
System.out.println (Tree.root.leftChild.data);
Node node = tree.find (3);
System.out.println (Node.data + "," + node.sdata);
}
}
This article is from the "8159085" blog, please be sure to keep this source http://8169085.blog.51cto.com/8159085/1696554
Lapping data structures and algorithms-basic operation of 112-fork Tree