一、树结构概述
根节点:1号节点
双亲节点:10号节点和11号节点的双亲节点是5号节点
子节点:7、8、9号节点是4号节点的子节点
路径:1号到11号的路径:1、2、5、11
节点的度:子节点的个数,1号的度为3
节点的权:节点存储的值
叶子节点:没有子节点的节点,10、11、6、3、7、8、9都是叶子节点
子树:5、10、11组成的树是整个树的子树
层:1是1层,2、3、4是2层,5、6、7、8、9是3层,10、11是4层
树的高度:最高层数,即为4
森林:是m(m≥0)棵互不相交的树的集合。任何一棵树,删除了根结点就变成了森林。
二叉树
1.二叉树概述
任何一个节点的子节点数量不超过2的树:
二叉树的子节点分左节点和右节点,不能随意颠倒:
两幅图中都是不一样的两棵树
2.满二叉树
所有叶子节点都在最后一层的二叉树,而且节点的总数为:2^n-1(n为层数)
3.完全二叉树
所有叶子节点都在最后一层或倒数第二层,且最后一层的叶子节点在左边连续,倒数第二层的叶子节点在右边连续的二叉树
上图是完全二叉树
上图不是完全二叉树
三、(链式存储)二叉树的创建、遍历、查找、删除
二叉树的结构
先序遍历
先父节点,再左节点,再右节点
中序遍历
先左节点,再父节点,再右节点
后序遍历
先左节点,再右节点,再父节点
package com.demo1;
public class BinaryTree {
TreeNode root;// 根节点
public void setRoot(TreeNode root) {
this.root = root;
}
public TreeNode 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 TreeNode frontSearch(int i) {
return root.frontSearch(i);
}
public void delete(int i) {
if (root.value == i) {
root = null;
} else {
root.delete(i);
}
}
}
package com.demo1;
public class TreeNode {
// 节点的权
int value;
// 左节点
TreeNode leftNode;
// 右节点
TreeNode rightNode;
public TreeNode(int value) {
this.value = value;
}
public void setLeftNode(TreeNode lNode) {
this.leftNode = lNode;
}
public void setRightNode(TreeNode rNode) {
this.rightNode = rNode;
}
// 前序遍历
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 TreeNode frontSearch(int i) {
TreeNode target = null;
if (this.value == i) {
return this;
} else {
if (leftNode != null) {
target = leftNode.frontSearch(i);
}
if (target != null) {
return target;
}
if (rightNode != null) {
target = rightNode.frontSearch(i);
}
}
return target;
}
// 删除子树
public void delete(int i) {
TreeNode 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);
}
}
}
package com.demo1;
public class TestBinaryTree {
public static void main(String[] args) {
// 创建一棵树
BinaryTree binTree = new BinaryTree();
// 创建一个根节点
TreeNode root = new TreeNode(1);
// 把根节点赋给树
binTree.setRoot(root);
// 创建左节点
TreeNode rootL1 = new TreeNode(2);
// 把新创建的节点设置为根节点的子节点
root.setLeftNode(rootL1);
// 创建右节点
TreeNode rootR1 = new TreeNode(3);
// 把新创建的节点设置为根节点的子节点
root.setRightNode(rootR1);
// 为第二层的左节点创建子节点
rootL1.setLeftNode(new TreeNode(4));
rootL1.setRightNode(new TreeNode(5));
// 为第二层的右节点创建子节点
rootR1.setLeftNode(new TreeNode(6));
rootR1.setRightNode(new TreeNode(7));
// 前序遍历树
binTree.frontShow();// 1 2 4 5 3 6 7
System.out.println();
// 中序遍历
binTree.midShow();// 4 2 5 1 6 3 7
System.out.println();
// 后序遍历
binTree.afterShow();// 4 5 2 6 7 3 1
System.out.println();
// 前序查找
TreeNode result = binTree.frontSearch(5);
System.out.println(result == rootL1.rightNode);// true
// 删除一个节点(包括子树)
binTree.delete(5);
binTree.frontShow();// 1 2 4 3 6 7)
System.out.println();
binTree.delete(2);
binTree.frontShow();// 1 3 6 7
}
}