目 录
P28-4.1树结构概述
顺序存储:开始位置、结束位置,插入数据、删除数据=====》在数据量大的情况下,非常耗费时间!根据下标查找元素。
链表存储:从第1个节点,开始往后找。。。【耗时!!!】
树结构:查找性能、插入性能 较好!!!
双亲结点:雌雄同体!!!子节点的上一个节点。--------->子节点
路径:从某个节点到达某个节点,途中经过的结点。
节点的度:节点有几个子节点(子树)。
节点的权:给节点赋予的值(节点中存储的数值)。
叶子节点:没有子节点的节点。
树的高度:最大层数。【4】
森林:多个树。
P29-4.2二叉树的概述
二叉树:任何一个节点的子节点数量不超过2!
二叉树的子节点 分 左节点 和 右节点。
满二叉树:所有叶子节点都在最后一层,而且节点的总数为 2^n-1。【n是树的高度!】
满二叉树,是 完全二叉树!
完全二叉树:所有叶子节点都在最后一层或倒数第二层,且最后一层的叶子节点在左边连续,倒数第二层的叶子节点在右边连续。
从上到下,从左到右 依次 数能数完 就是 完全二叉树!
P30-4.3创建二叉树
双链表 是 前驱 和 后继,树 是 左右孩子结点,不一样!
二叉树的5种形式:
P31-4.4遍历二叉树
遍历详解:https://blog.csdn.net/weixin_44949135/article/details/105291651
P32-4.5二叉树中节点的查找
P33-4.6删除二叉树的子树
删除 节点(子树):
- 无子节点(子节点一并删除!);
- 有子节点。
1、BinaryTree.java
package demo5;
public class BinaryTree {
Node root;
// 设置根节点
public void setRoot(Node root) {
this.root = root;
}
// 获取根节点
public Node getRoot() {
return root;
}
public void frontShow() {
if (root != null) {
root.frontShow();
}
}
public void midShow() {
if (root != null) {
root.midShow();
}
}
public void afterShow() {
if (root != null) {
root.afterShow();
}
}
public Node frontSearch(int i) {
return root.frontSearch(i);
}
public void delete(int i) {
if (root.value == i) {
root = null;
} else {
root.delete(i);
}
}
}
2、Node.java
package demo5;
public class Node {
// 节点的权
int value;
// 左儿子
Node leftNode;
// 右儿子
Node rightNode;
public Node(int value) {
this.value = value;
}
// 设置左儿子
public void setLeftNode(Node leftNode) {
this.leftNode = leftNode;
}
// 设置右儿子
public void setRightNode(Node rightNode) {
this.rightNode = rightNode;
}
// 前序遍历
public void frontShow() {
// 先遍历当前节点的内容
System.out.print(value + "、");
// 左节点
if (leftNode != null) {
leftNode.frontShow();
}
// 右节点
if (rightNode != null) {
rightNode.frontShow();
}
}
// 中序遍历
public void midShow() {
// 左子节点
if (leftNode != null) {
leftNode.midShow();
}
// 当前节点
System.out.print(value + "、");
// 右子节点
if (rightNode != null) {
rightNode.midShow();
}
}
// 后序遍历
public void afterShow() {
// 左子节点
if (leftNode != null) {
leftNode.afterShow();
}
// 右子节点
if (rightNode != null) {
rightNode.afterShow();
}
// 当前节点
System.out.print(value + "、");
}
// 前序查找
public Node frontSearch(int i) {
Node target = null;
// 对比当前节点的值
if (this.value == i) {
return this;
// 当前节点的值不是要查找的节点
} else {
// 查找左儿子
if (leftNode != null) {
// 有可能可以查到,也可以查不到,查不到的话,target还是一个null
target = leftNode.frontSearch(i);
}
// 如果不为空,说明在左儿子中已经找到
if (target != null) {
return target;
}
// 查找右儿子
if (rightNode != null) {
target = rightNode.frontSearch(i);
}
}
return target;
}
// 删除一个子树
public void delete(int i) {
Node parent = this;
// 判断左儿子
if (parent.leftNode != null && parent.leftNode.value == i) {
parent.leftNode = null;
return;
}
// 判断右儿子
if (parent.rightNode != null && parent.rightNode.value == i) {
parent.rightNode = null;
return;
}
// 递归检查并删除左儿子
parent = leftNode;
if (parent != null) {
parent.delete(i);
}
// 递归检查并删除右儿子
parent = rightNode;
if (parent != null) {
parent.delete(i);
}
}
}
3、TestBinaryTree.java
package demo5;
public class TestBinaryTree {
public static void main(String[] args) {
// 创建一颗树
BinaryTree binTree = new BinaryTree();
// 创建一个根节点
Node root = new Node(1);
// 把根节点赋给树
binTree.setRoot(root);
// 创建一个左节点
Node rootL = new Node(2);
// 把新创建的节点设置为根节点的子节点
root.setLeftNode(rootL);
// 创建一个右节点
Node rootR = new Node(3);
// 把新创建的节点设置为根节点的子节点
root.setRightNode(rootR);
// 为第二层的左节点创建两个子节点
rootL.setLeftNode(new Node(4));
rootL.setRightNode(new Node(5));
// 为第二层的右节点创建两个子节点
rootR.setLeftNode(new Node(6));
rootR.setRightNode(new Node(7));
// 前序遍历树
System.out.println("========前序遍历树========");
binTree.frontShow();
System.out.println("\n");
// 中序遍历
System.out.println("========中序遍历树========");
binTree.midShow();
System.out.println("\n");
// 后序遍历
System.out.println("========后序遍历树========");
binTree.afterShow();
System.out.println("\n\n");
// 前序查找
Node result = binTree.frontSearch(3);
System.out.println(result);
System.out.println(result == rootR);
System.out.println("\n\n===============");
// 删除一个子树
binTree.delete(4);
binTree.frontShow();
}
}
希望对您有所帮助~