package four_tree.binarySearchTree;
/**
* Author:jinpma
* Date :2019/12/22
*/
public class BinarySearchTree
{
public static void main(String[] args)
{
int array[] = {7,3,10,12,5,1,9,2};
BinaryTreeDemo bt = new BinaryTreeDemo();
for (int i = 0; i < array.length; i++)
{
bt.add(new Node(array[i]));
}
bt.inOrder();
}
static class BinaryTreeDemo
{
public Node root;
public void setter(Node root)
{
this.root = root;
}
public void inOrder()
{
if (this.root == null)
{
System.out.println("空树");
}else
{
this.root.inOrder();
}
}
public void add(Node node)
{
if (root == null)
{
setter(node);
}else
{
root.add(node);
}
}
}
static class Node
{
public Node left;
public Node right;
public int value;
public Node(int value)
{
this.value = value;
}
@Override
public String toString()
{
return "Node{" +
"value=" + value +
'}';
}
public void add(Node node)
{
if (node.value < this.value)
{
if (this.left == null)
{
this.left = node;
}else
{
this.left.add(node);
}
}else
{
if (this.right == null)
{
this.right = node;
}else
{
this.right.add(node);
}
}
}
public void inOrder()
{
if (this.left != null)
{
this.left.inOrder();
}
System.out.println(this);
if(this.right != null)
{
this.right.inOrder();
}
}
}
}
来源:CSDN
作者:tmax52HZ
链接:https://blog.csdn.net/tmax52HZ/article/details/103751817