1.1 树的相关概念
- 树的定义:树是由n(n>=1)个有限结点组成一个具有层次关系的集合。把它叫做树,是因为它看起来像是一个倒挂的树,也就是它根朝上,而叶朝下。
- 树的特点:
- 每个结点有0或多个子结点
- 没有父节点的结点称为根结点
- 每一个非根结点只有一个父结点
- 每个结点及其后代结点整体上可以看做是一棵树,称为当前结点的父结点的一个子树。
- 结点的度:一个结点含有的子树的个数称为该结点的度。
- 叶结点:度不为0的结点称为分支结点,也可以叫做非终端结点
- 结点的层次:从根结点开始,根结点的层次为1,根的直接后继结点层次为2,以此类推。
- 结点的层序编号:将树种的结点,按照从上层到下层,同层从左到右依次排序成一个线性序列,把他们编成连续的自然数。
- 树的度:树中所有结点的度的最大值。
- 树的高度(深度):树中结点的最大层次。
- 森林:m(m>=0)个互不相交的树的集合,将一颗非空树的根结点删去,树就变成了一个森林;给森林增加一个统一的根结点,森林就变成一棵树。
- 孩子结点:一个结点的直接后继结点称为该结点的孩子结点。
- 双亲结点:一个结点的直接前驱结点称为该结点的双亲结点。
- 兄弟结点:同一双亲结点的孩子结点间互称兄弟结点。
1.2 二叉树
1.2.1 二叉树的基本定义
- 二叉树就是度不超过2的树(每个结点最多有两个子结点)
- 满二叉树:一个二叉树,如果每一个层的结点树都达到最大值,则这个二叉树就是满二叉树。
- 完全二叉树:叶节点只能出现在最下层和次下层,并且最下面一层的结点都集中在该层最左边的若干位置的二叉树。
- 二叉树API设计
1.2.2 二叉查找树
1.2.3 二叉树的遍历
- 前序遍历:先访问根节点,然后再访问左子树,最后访问右子树。
- 中序遍历:先访问左子树,再访问根节点,最后访问右子树。
- 后序遍历:先访问左子树,再访问右子树,最后访问根节点。
- 层序遍历:从根节点开始,从上往下,从左往右访问所有节点。(EBGADFHC)
1.2.4 二叉树相关代码
package com.tiger.study.DataStructure.Tree;
import com.sun.corba.se.impl.resolver.SplitLocalResolverImpl;
import com.tiger.study.DataStructure.Linear.Queue;
import org.jetbrains.annotations.NotNull;
public class BinaryTree<Key extends Comparable<Key>, Value> {
private Node root;
private int N;
public int size() {
return N;
}
public void put(Key key, Value value) {
root = put(root, key, value);
}
public Node put(Node x, Key key, Value value) {
if (x == null) {
N++;
return new Node(key, value, null, null);
}
int cmp = key.compareTo(x.key);
if (cmp > 0) {
x.right = put(x.right, key, value);
} else if (cmp < 0) {
x.left = put(x.left, key, value);
} else {
x.value = value;
}
return x;
}
public Value get(Key key) {
return get(root, key);
}
public Value get(Node x, Key key) {
if (x == null) {
return null;
}
int cmp = key.compareTo(x.key);
if (cmp > 0) {
return get(x.right, key);
} else if (cmp < 0) {
return get(x.left, key);
} else {
return x.value;
}
}
public void delete(Key key) {
root = delete(root, key);
}
public Node delete(Node x, Key key) {
if (x == null) {
return null;
}
int cmp = key.compareTo(x.key);
if (cmp > 0) {
x.right = delete(x.right, key);
} else if (cmp < 0) {
x.left = delete(x.left, key);
} else {
N--;
if (x.right == null) {
return x.left;
}
if (x.left == null) {
return x.right;
}
Node minNode = x;
while (minNode.left != null) {
minNode = minNode.left;
}
Node n = x;
while (n.left != null) {
if (n.left.left == null) {
n.left = null;
} else {
n = n.left;
}
}
minNode.left = x.left;
minNode.right = x.right;
x = minNode;
}
return x;
}
public Key min() {
return min(root).key;
}
public Node min(Node x) {
if (x.left == null) {
return x;
} else {
return min(x.left);
}
}
public Key max() {
return max(root).key;
}
public Node max(Node x) {
if (x.right != null) {
return max(x.right);
} else {
return x;
}
}
public Queue<Key> preErgodic() {
Queue<Key> keys = new Queue<>();
preErgodic(root, keys);
return keys;
}
private void preErgodic(Node x, Queue<Key> keys) {
if (x == null) {
return;
}
keys.enqueue(x.key);
if (x.left != null) {
preErgodic(x.left, keys);
}
if (x.right != null) {
preErgodic(x.right, keys);
}
}
public Queue<Key> midErgodic() {
Queue<Key> keys = new Queue<>();
midErgodic(root, keys);
return keys;
}
private void midErgodic(Node x, Queue<Key> keys) {
if (x == null) {
return;
}
if (x.left != null) {
midErgodic(x.left, keys);
}
keys.enqueue(x.key);
if (x.right != null) {
midErgodic(x.right, keys);
}
}
public Queue<Key> afterErgodic() {
Queue<Key> keys = new Queue<>();
afterErgodic(root, keys);
return keys;
}
private void afterErgodic(Node x, Queue<Key> keys) {
if (x == null) {
return;
}
if (x.left != null) {
afterErgodic(x.left, keys);
}
if (x.right != null) {
afterErgodic(x.right, keys);
}
keys.enqueue(x.key);
}
public Queue<Key> layerErgodic() {
Queue<Node> nodes = new Queue<Node>();
Queue<Key> keys = new Queue<>();
nodes.enqueue(root);
while (!nodes.isEmpty()) {
Node dqNode = nodes.dequeue();
if (dqNode.left != null) {
nodes.enqueue(dqNode.left);
}
if (dqNode.right != null) {
nodes.enqueue(dqNode.right);
}
keys.enqueue(dqNode.key);
}
return keys;
}
public int maxDepth() {
return maxDepth(root);
}
private int maxDepth(Node x) {
if (x == null) {
return 0;
}
int leftChildTreeDepth = 0;
if (x.left != null) {
leftChildTreeDepth = maxDepth(x.left);
}
int rightChildTreeDepth = 0;
if (x.right != null) {
rightChildTreeDepth = maxDepth(x.right);
}
return Math.max(leftChildTreeDepth, rightChildTreeDepth) + 1;
}
private class Node {
Node left;
Node right;
Key key;
Value value;
public Node(Key key, Value value, Node left, Node right) {
this.key = key;
this.value = value;
this.left = left;
this.right = right;
}
}
}
1.3 折纸问题
- 需求
- 分析
- 代码实现
package com.tiger.study.DataStructure.Test.Tree;
import com.tiger.study.DataStructure.Linear.Queue;
import com.tiger.study.DataStructure.Tree.BinaryTree;
public class PageFolding {
public static void main(String[] args) {
int times = 2;
Node tree = createTree(times);
printErgodic(tree);
}
public static Node<String> createTree(int n) {
Node<String> root = null;
for (int i = 0; i < n; i++) {
if (i == 0) {
root = new Node<>("down", null, null);
continue;
}
Queue<Node> queue = new Queue<>();
queue.enqueue(root);
while (!queue.isEmpty()) {
Node tmp = queue.dequeue();
if (tmp.left != null) {
queue.enqueue(tmp.left);
}
if (tmp.right != null) {
queue.enqueue(tmp.right);
}
if (tmp.left == null && tmp.right == null) {
tmp.left = new Node<>("down", null, null);
tmp.right = new Node<>("up", null, null);
}
}
}
return root;
}
public static void printErgodic(Node<String> n) {
if (n == null) {
return;
}
if (n.left != null) {
printErgodic(n.left);
}
System.out.printf(n.item + " ");
if (n.right != null) {
printErgodic(n.right);
}
}
private static class Node<T> {
public T item;
public Node left;
public Node right;
public Node(T item, Node left, Node right) {
this.item = item;
this.left = left;
this.right = right;
}
}
}