public class Node {
//数据
private int value;
//左子节点
private Node leftChild;
//右子点的
private Node rightChild;
public Node() {
}
public Node(int value){
this.value=value;
}
public int getValue() {
return value;
}
public void setValue(int value) {
this.value = value;
}
public Node getLeftChild() {
return leftChild;
}
public void setLeftChild(Node leftChild) {
this.leftChild = leftChild;
}
public Node getRightChild() {
return rightChild;
}
public void setRightChild(Node rightChild) {
this.rightChild = rightChild;
}
@Override
public String toString() {
return "Node{" +
"value=" + value +
'}';
}
}
import com.sun.corba.se.spi.ior.IdentifiableFactory;
import com.sun.org.apache.bcel.internal.generic.IF_ACMPEQ;
import java.util.ArrayList;
import java.util.List;
/*
* 二叉树
* */
public class BinaryTree {
//根节点属性
private Node root;
public BinaryTree(){
}
public BinaryTree(int value){
Node node = new Node(value);
this.root=node;
}
public Node getRoot() {
return root;
}
public void setRoot(Node root) {
this.root = root;
}
public Boolean add(int value) {
//根据value创建节点对象
Node node = new Node(value);
// System.out.println(node);
//判断二叉树是否有根节点
if (root == null) {
//将node设置为根节点
root = node;
return true;
} else {//有根节点
Node current = root;
while (true) {
if (value < current.getValue()) {
//来到这里,说明node节点应该出现在根节点的左边
//判断cuurent节点间是否有左节点对象
if (current.getLeftChild() == null) {
//设置node节点为current节点的左节点
current.setLeftChild(node);
//结束循环
return true;
}
//说明current有左节点
current = current.getLeftChild();
} else if (value > current.getValue()) {
//来到这里,说名node节点应该出现在current节点的右边
//判断current是否有右节点对象
if (current.getRightChild() == null) {
//设置node节点为current节点的又节点
current.setRightChild(node);
return true;
}
//说明current节点有右节点
current = current.getRightChild();
} else {
return false;
}
}
}
}
/*根据节点内容获得节点对象
@param value节点内容
@return 节点对象,如果没有找到,则返回null
* */
public Node get(int value){
//定义一个节点对象变量,用来记录当前节点对象
Node currentNode=root;
while (true){
//比较value和current的Node节点的对象的value是否相同
if (value==currentNode.getValue()){
return currentNode;
}else if (value>currentNode.getValue()){
//说明这里查询的节点对象在cuurent节点的右边
currentNode=currentNode.getRightChild();
}else if (value<currentNode.getValue()){
//说明这里查询的节点对象在cuurent节点的左边
currentNode= currentNode.getLeftChild();
}
//判断curentNode是否为null
if (currentNode==null){
return null;
}
}
}
/*将指定节点下的所有子节点的内容按顺序输出
* */
public void order(Node node){
if (node==null){
return;
}
//先遍历node的左节点
order(node.getLeftChild());
//输出node对象的内容
System.out.println(node.getValue());
//遍历node的右节点
order(node.getRightChild());
}
public void order2(Node node,List<Integer> list){
if (node==null){
return;
}
//先遍历node的左节点
order2(node.getLeftChild(),list);
//输出node对象的内容
// System.out.println(node.getValue());
list.add(node.getValue());
//遍历node的右节点
order2(node.getRightChild(),list);
}
@Override
public String toString() {
List<Integer> list=new ArrayList<>();
order2(root,list);
String string = list.toString();
return string;
}
}
public class BinaryTreeDemo {
public static void main(String[] args) {
int[] a={3,1,0,2,7,5,8,9};
BinaryTree binaryTree = new BinaryTree();
for (int i = 0; i < a.length; i++) {
int value=a[i];
binaryTree.add(value);
}
// System.out.println(binaryTree.get(5));
// System.out.println(binaryTree.get(4));
binaryTree.order(binaryTree.getRoot());
// System.out.println(binaryTree);
}
}