package binarytree;
import java.util.*;
public class BinaryTree {
public Node root = new Node();
private static int maxsize = 100;
public BinaryTree() {
root = null;
}
public void Insert(int value) {
Node tmp = root;
Node pNode = root;
int flag = 0;
if (null == root) {
root = new Node(value);
return;
}
while (tmp != null)
if (value > tmp.value) {
pNode = tmp;
tmp = tmp.right;
flag = 0;
} else if (value < tmp.value) {
pNode = tmp;
tmp = tmp.left;
flag = 1;
} else
return;
tmp = new Node(value);
if (0 == flag)
pNode.right = tmp;
else
pNode.left = tmp;
}
public void MidShow(Node node) {
if (null == node)
return;
MidShow(node.left);
System.out.print(node.value + "\t");
MidShow(node.right);
}
public static void MidShow2(Node t) {
Stack<Node> s = new Stack<Node>();
while (t != null || !s.empty()) {
while (t != null) {
s.push(t);
t = t.left;
}
if (!s.empty()) {
t = s.pop();
System.out.print(t.value + "\t");
t = t.right;
}
}
}
public static void FrontShow2(Node t) {
Stack<Node> s = new Stack<Node>();
while (t != null || !s.empty()) {
while (t != null) {
System.out.print(t.value + "\t");
s.push(t);
t = t.left;
}
if (!s.empty()) {
t = s.pop();
t = t.right;
}
}
}
public static void BackShow2(Node t) {
Stack<Node> s = new Stack<Node>();
Stack<Integer> s2 = new Stack<Integer>();
Integer i = new Integer(1);
while (t != null || !s.empty()) {
while (t != null) {
s.push(t);
s2.push(new Integer(0));
t = t.left;
}
while (!s.empty() && s2.peek().equals(i)) {
s2.pop();
System.out.print(s.pop().value + "\t");
}
if (!s.empty()) {
s2.pop();
s2.push(new Integer(1));
t = s.peek();
t = t.right;
}
}
}
public void FrontShow(Node node) {
if (null == node)
return;
System.out.print(node.value + "\t");
FrontShow(node.left);
FrontShow(node.right);
}
public void BackShow(Node node) {
if (null == node)
return;
BackShow(node.left);
BackShow(node.right);
System.out.print(node.value + "\t");
}
public boolean MyFind(Node node, int value) {
if (null == node)
return false;
else if (node.value > value)
return MyFind(node.left, value);
else if (node.value < value)
return MyFind(node.right, value);
else
return true;
}
public static void main(String[] args) {
BinaryTree mytree = new BinaryTree();
Random r = new Random();
int tmp;
for (int i = 0; i < 5; i++) {
tmp = r.nextInt(maxsize);
System.out.print(tmp + "->");
mytree.Insert(tmp);
}
System.out.println();
System.out.println("Front Show!");
mytree.FrontShow(mytree.root);
System.out.println("\nFront Show UnReverse!");
FrontShow2(mytree.root);
System.out.println("\nMid Show!");
mytree.MidShow(mytree.root);
System.out.println("\nMid Show UnReverse!");
MidShow2(mytree.root);
System.out.println("\nBack Show!");
mytree.BackShow(mytree.root);
System.out.println("\nBack Show UnReverse!");
BackShow2(mytree.root);
System.out
.println("\nPlease input the data to be found in the tree :");
Scanner in = new Scanner(System.in);
int inputnum = in.nextInt();
in.close();
System.out.println(mytree.MyFind(mytree.root, inputnum));
}
}
package binarytree;
public class Node {
public int value;
public Node left;
public Node right;
public Node(int value)
{
this.value = value;
left = right = null;
}
public Node()
{
}
}