package com.mccrea.structure.tree;
/**
* @author mccrea
* @version 1.0
* @description: 二叉树的demo
* @date 2020/9/26 15:56
*/
public class BinaryTreeDemo {
public static void main(String[] args) {
BinaryTree binaryTree = new BinaryTree();
binaryTree.addNode(3);
binaryTree.addNode(6);
binaryTree.addNode(5);
binaryTree.addNode(2);
binaryTree.addNode(9);
binaryTree.addNode(-1);
binaryTree.addNode(10);
binaryTree.addNode(4);
binaryTree.listData(binaryTree.getRoot());
System.out.println(binaryTree.contains(-1, binaryTree.getRoot()));
}
}
/**
* @description: 二叉树的节点
* @author mccrea
* @date 2020/9/26 15:57
* @version 1.0
*/
class Node {
/**
* @description: 节点数据
* @author mccrea
* @date 2020/9/26 15:58
* @version 1.0
*/
public Integer data;
/**
* @description: 左子节点
* @author mccrea
* @date 2020/9/26 15:58
* @version 1.0
*/
public Node left;
/**
* @description: 右子节点
* @author mccrea
* @date 2020/9/26 15:58
* @version 1.0
*/
public Node right;
/**
* @description: 父节点
* @author mccrea
* @date 2020/9/26 15:59
* @version 1.0
*/
public Node parent;
/**
* @description: 构造函数
* @param:
* @param data 传入的数据
* @return:
* @author mccrea
* @date: 2020/9/26 16:00
*/
public Node(Integer data) {
this.data = data;
}
}
/**
* @description: 二叉树
* @author mccrea
* @date 2020/9/26 16:01
* @version 1.0
*/
class BinaryTree {
/**
* @description: 根节点
* @author mccrea
* @date 2020/9/26 16:01
* @version 1.0
*/
private Node root;
/**
* @description: 二叉树增加节点
* @param:
* @param data 存入的数据
* @return: void
* @author mccrea
* @date: 2020/9/26 16:23
*/
public void addNode(Integer data) {
if (null == data) {
throw new NullPointerException("存入的数据不能为空");
}
// 新增加的节点
Node newNode = new Node(data);
// 根节点为空,说明该二叉树还没有存入数据,直接把存入的数据作为根节点
if (null == root) {
root = newNode;
// 根节点有值,需要给存入的数据找到位置
} else {
// 遍历指针,从根节点开始
Node currentNode = root;
while (newNode != currentNode) {
// 当前节点数据大于等于新节点数据,新节点应该存入左边
if (currentNode.data >= newNode.data) {
// 当前节点左子节点数据不为空,则当前节点进行变换为其左子节点
if (null != currentNode.left) {
currentNode = currentNode.left;
// 当前节点左子节点为空,则新节点存入其中,结束方法
} else {
currentNode.left = newNode;
newNode.parent = currentNode;
return;
}
// 当前节点数据小于新节点数据,新节点应该存入右边
} else {
// 当前节点右子节点不为空,则当前节点进行变换为其右子节点
if (null != currentNode.right) {
currentNode = currentNode.right;
// 当前节点右子节点不为空,则新节点存入其中,结束方法
} else {
currentNode.right = newNode;
newNode.parent = currentNode;
return;
}
}
}
}
}
/**
* @description: 得到二叉树中所有数据,中序遍历
* @param: * @param
* @return: java.lang.Integer[]
* @author mccrea
* @date: 2020/9/26 20:58
*/
public void listData (Node node) {
// 根节点为空,没有数据
if (node == null) {
return;
}
// 左子节点不为空,先处理左子节点
if (node.left != null) {
listData(node.left);
}
// 处理根节点
System.out.println(node.data);
// 右子节点不为空,先处理右子节点
if (node.right != null) {
listData(node.right);
}
}
/**
* @description: 获取根节点
* @param:
* @return: com.mccrea.structure.tree.Node
* @author mccrea
* @date: 2020/9/26 21:09
*/
public Node getRoot() {
return root;
}
/**
* @description: 查询二叉树是否包含指定数据
* @param: number 待查询的数
* @param node 二叉树
* @return: boolean 二叉树是否包含指定数据
* @author mccrea
* @date: 2020/9/26 21:31
*/
public boolean contains(Integer number, Node node) {
// 根节点为空,返回false
if (node == null) {
return false;
}
// 待查询数不能为空
if (number == null) {
throw new NullPointerException("待查询数不能为空");
}
// 查到待查询数
if (number.equals(node.data)) {
return true;
// 根节点数大于待查询数,查询左子节点
} else if (node.data > number) {
return contains(number, node.left);
// 根节点数小于待查询数,查询右子节点
} else {
return contains(number, node.right);
}
}
}