1.二叉树定义

二叉树就是度不超过2的树(每个结点最多有两个子结点)

满二叉树:
一个二叉树,如果每一个层的结点树都达到最大值,则这个二叉树就是满二叉树。
image.png
完全二叉树:
叶节点只能出现在最下层和次下层,并且最下面一层的结点都集中在该层最左边的若干位置的二叉树
image.png

代码实现:
put()
image.png
delete()
删除方法delete实现思想:
1.找到被删除结点;
2.找到被删除结点右子树中的最小结点minNode
3.删除右子树中的最小结点
4.让被删除结点的左子树称为最小结点minNode的左子树,让被删除结点的右子树称为最小结点minNode的右子树
5.让被删除结点的父节点指向最小结点minNode

  1. package 树;
  2. public class BinaryTree<Key extends Comparable<Key>, Value> {
  3. //记录根结点
  4. private Node root;
  5. //记录树中元素的个数
  6. private int N;
  7. //获取树中元素的个数
  8. public int size() {
  9. return N;
  10. }
  11. //向树中添加元素key-value
  12. public void put(Key key, Value value) {
  13. root = put(root, key, value);
  14. }
  15. //向指定的树x中添加key-value,并返回添加元素后新的树
  16. private Node put(Node x, Key key, Value value) {
  17. if (x == null) {
  18. //个数+1
  19. N++;
  20. return new Node(key, value, null, null);
  21. }
  22. int cmp = key.compareTo(x.key);
  23. if (cmp > 0) {
  24. //新结点的key大于当前结点的key,继续找当前结点的右子结点
  25. x.right = put(x.right, key, value);
  26. } else if (cmp < 0) {
  27. //新结点的key小于当前结点的key,继续找当前结点的左子结点
  28. x.left = put(x.left, key, value);
  29. } else {
  30. //新结点的key等于当前结点的key,把当前结点的value进行替换
  31. x.value = value;
  32. }
  33. return x;
  34. }
  35. //查询树中指定key对应的value
  36. public Value get(Key key) {
  37. return get(root, key);
  38. }
  39. //从指定的树x中,查找key对应的值
  40. public Value get(Node x, Key key) {
  41. if (x == null) {
  42. return null;
  43. }
  44. int cmp = key.compareTo(x.key);
  45. if (cmp > 0) {
  46. //如果要查询的key大于当前结点的key,则继续找当前结点的右子结点;
  47. return get(x.right, key);
  48. } else if (cmp < 0) {
  49. //如果要查询的key小于当前结点的key,则继续找当前结点的左子结点;
  50. return get(x.left, key);
  51. } else {
  52. //如果要查询的key等于当前结点的key,则树中返回当前结点的value。
  53. return x.value;
  54. }
  55. }
  56. //删除树中key对应的value
  57. public void delete(Key key) {
  58. root = delete(root, key);
  59. }
  60. //删除指定树x中的key对应的value,并返回删除后的新树
  61. public Node delete(Node x, Key key) {
  62. if (x == null) {
  63. return null;
  64. }
  65. int cmp = key.compareTo(x.key);
  66. if (cmp > 0) {
  67. //新结点的key大于当前结点的key,继续找当前结点的右子结点
  68. x.right = delete(x.right, key);
  69. } else if (cmp < 0) {
  70. //新结点的key小于当前结点的key,继续找当前结点的左子结点
  71. x.left = delete(x.left, key);
  72. } else {
  73. //新结点的key等于当前结点的key,当前x就是要删除的结点
  74. //1.如果当前结点的右子树不存在,则直接返回当前结点的左子结点
  75. if (x.right == null) {
  76. return x.left;
  77. }
  78. //2.如果当前结点的左子树不存在,则直接返回当前结点的右子结点
  79. if (x.left == null) {
  80. return x.right;
  81. }
  82. //3.当前结点的左右子树都存在
  83. //3.1找到右子树中最小的结点
  84. Node minNode = x.right;
  85. while (minNode.left != null) {
  86. minNode = minNode.left;
  87. }
  88. //3.2删除右子树中最小的结点
  89. Node n = x.right;
  90. while (n.left != null) {
  91. if (n.left.left == null) {
  92. n.left = null;
  93. } else {
  94. n = n.left;
  95. }
  96. }
  97. //3.3让被删除结点的左子树称为最小结点minNode的左子树,让被删除结点的右子树称为最小结点minNode的右子树
  98. minNode.left = x.left;
  99. minNode.right = x.right;
  100. //3.4让被删除结点的父节点指向最小结点minNode
  101. x = minNode;
  102. //个数-1
  103. N--;
  104. }
  105. return x;
  106. }
  107. private class Node {
  108. //存储键
  109. public Key key;
  110. //存储值
  111. private Value value;
  112. //记录左子结点
  113. public Node left;
  114. //记录右子结点
  115. public Node right;
  116. public Node(Key key, Value value, Node left, Node right) {
  117. this.key = key;
  118. this.value = value;
  119. this.left = left;
  120. this.right = right;
  121. }
  122. }
  123. }

2.二叉查找树查找最值

2.1.查找最小的键

//找出整个树中最小的键
    public Key min(){
        return min(root).key;
    }
    //找出指定树x中最小的键所在的结点
    private Node min(Node x){
        if (x.left!=null){
            return min(x.left);
        }else{
            return x;
        }
    }

2.2.查找最大的键

 //找出整个树中最大的键
    public Key max(){
        return max(root).key;
    }
    //找出指定树x中最大键所在的结点
    public Node max(Node x){
        if (x.right!=null){
            return max(x.right);
        }else{
            return x;
        }
    }

3.遍历

image.png
我们把树简单的画作上图中的样子,由一个根节点、一个左子树、一个右子树组成,那么按照根节点什么时候被访 问,我们可以把二叉树的遍历分为以下三种方式:
1.前序遍历; 先访问根结点,然后再访问左子树,最后访问右子树
2.中序遍历; 先访问左子树,中间访问根节点,最后访问右子树
3.后序遍历; 先访问左子树,再访问右子树,最后访问根节点
image.png

3.1.前序遍历

先访问根结点,然后再访问左子树,最后访问右子树

代码实现:

public Queue<Key> preErgodic(){
        Queue<Key> keys = new Queue<>();
        preErgodic(root,keys);
        return keys;
    }
    //使用前序遍历,把指定树x中的所有键放入到keys队列中
    private void preErgodic(Node x,Queue<Key> keys){
        if (x==null){
            return;
        }
        //1.把当前结点的key放入到队列中;
        keys.enqueue(x.key);
        //2.找到当前结点的左子树,如果不为空,递归遍历左子树
        if (x.left!=null){
            preErgodic(x.left,keys);
        }
        //3.找到当前结点的右子树,如果不为空,递归遍历右子树
        if (x.right!=null){
            preErgodic(x.right,keys);
        }
    }

3.2.中序遍历

先访问左子树,中间访问根节点,最后访问右子树

代码实现:

//使用中序遍历,获取整个树中的所有键
    public Queue<Key> midErgodic(){
        Queue<Key> keys = new Queue<>();
        midErgodic(root,keys);
        return keys;
    }
    //使用中序遍历,把指定树x中的所有键放入到keys队列中
    private void midErgodic(Node x,Queue<Key> keys){
        if (x==null){
            return;
        }
        //1.找到当前结点的左子树,如果不为空,递归遍历左子树
        if (x.left!=null){
            midErgodic(x.left,keys);
        }
        //2.把当前结点的key放入到队列中;
        keys.enqueue(x.key);
        //3.找到当前结点的右子树,如果不为空,递归遍历右子树
        if (x.right!=null){
            midErgodic(x.right,keys);
        }
    }

3.3.后序遍历

先访问左子树,再访问右子树,最后访问根节点

代码实现:

//使用后序遍历,获取整个树中的所有键
    public Queue<Key> afterErgodic(){
        Queue<Key> keys = new Queue<>();
        afterErgodic(root,keys);
        return keys;
    }
    //使用后序遍历,把指定树x中的所有键放入到keys队列中
    private void afterErgodic(Node x,Queue<Key> keys){
        if (x==null){
            return;
        }
        //1.找到当前结点的左子树,如果不为空,递归遍历左子树
        if (x.left!=null){
            afterErgodic(x.left,keys);
        }
        //2.找到当前结点的右子树,如果不为空,递归遍历右子树
        if (x.right!=null){
            afterErgodic(x.right,keys);
        }
        //3.把当前结点的key放入到队列中;
        keys.enqueue(x.key);
    }

3.4.层序遍历

所谓的层序遍历,就是从根节点(第一层)开始,依次向下,获取每一层所有结点的值,有二叉树如下:
image.png

实现步骤
1.创建队列,存储每一层的结点;
2.使用循环从队列中弹出一个结点:
2.1获取当前结点的key;
2.2如果当前结点的左子结点不为空,则把左子结点放入到队列中
2.3如果当前结点的右子结点不为空,则把右子结点放入到队列中
image.png

代码实现:

//使用层序遍历得到树中所有的键
    public Queue<Key> layerErgodic() {
        Queue<Key> keys = new Queue<>();
        Queue<Node> nodes = new Queue<>();
        nodes.enqueue(root);
        while (!nodes.isEmpty()) {
            Node x = nodes.dequeue();
            keys.enqueue(x.key);
            if (x.left != null) {
                nodes.enqueue(x.left);
            }
            if (x.right != null) {
                nodes.enqueue(x.right);
            }
        }
        return keys;
    }

4.最大深度问题

给定一棵树,请计算树的最大深度(树的根节点到最远叶子结点的最长路径上的结点数)

实现步骤
1.如果根结点为空,则最大深度为0;
2.计算左子树的最大深度;
3.计算右子树的最大深度;
4.当前树的最大深度=左子树的最大深度和右子树的最大深度中的较大者+1

代码实现:

//计算整个树的最大深度
    public int maxDepth() {
        return maxDepth(root);
    }
    //计算指定树x的最大深度
    private int maxDepth(Node x) {
        //1.如果根结点为空,则最大深度为0;
        if (x == null) {
            return 0;
        }
        int max = 0;
        int maxL = 0;
        int maxR = 0;
        //2.计算左子树的最大深度;
        if (x.left != null) {
            maxL = maxDepth(x.left);
        }
        //3.计算右子树的最大深度;
        if (x.right != null) {
            maxR = maxDepth(x.right);
        }
        //4.当前树的最大深度=左子树的最大深度和右子树的最大深度中的较大者+1
        max = maxL > maxR ? maxL + 1 : maxR + 1;
        return max;
    }

5.折纸问题

需求
请把一段纸条竖着放在桌子上,然后从纸条的下边向上方对折1次,压出折痕后展开。此时 折痕是凹下去的,即折 痕突起的方向指向纸条的背面。如果从纸条的下边向上方连续对折2 次,压出折痕后展开,此时有三条折痕,从上 到下依次是下折痕、下折痕和上折痕。

给定一 个输入参数N,代表纸条都从下边向上方连续对折N次,请从上到下打印所有折痕的方向 例如:N=1时,打 印: down;N=2时,打印: down down up
image.png
分析:
我们把对折后的纸张翻过来,让粉色朝下,这时把第一次对折产生的折痕看做是根结点,那第二次对折产生的下折 痕就是该结点的左子结点,而第二次对折产生的上折痕就是该结点的右子结点,这样我们就可以使用树型数据结构 来描述对折后产生的折痕。

这棵树有这样的特点:
1.根结点为下折痕;
2.每一个结点的左子结点为下折痕;
3.每一个结点的右子结点为上折痕;

实现步骤
1.定义结点类
2.构建深度为N的折痕树;
3.使用中序遍历,打印出树中所有结点的内容;

构建深度为N的折痕树:
1.第一次对折,只有一条折痕,创建根结点;
2.如果不是第一次对折,则使用队列保存根结点;
3.循环遍历队列:
3.1从队列中拿出一个结点;
3.2如果这个结点的左子结点不为空,则把这个左子结点添加到队列中;
3.3如果这个结点的右子结点不为空,则把这个右子结点添加到队列中;
3.4判断当前结点的左子结点和右子结点都不为空,如果是,则需要为当前结点创建一个值为 down的左子结点,一 个值为up的右子结点。

代码实现:

package 树;

import 线性表.Queue;

public class PaperFolding {
    public static void main(String[] args) {
//构建折痕树
        Node tree = createTree(3);
//遍历折痕树,并打印
        printTree(tree);
    }

    //3.使用中序遍历,打印出树中所有结点的内容;
    private static void printTree(Node tree) {
        if (tree == null) {
            return;
        }
        printTree(tree.left);
        System.out.print(tree.item + ",");
        printTree(tree.right);
    }

    //2.构建深度为N的折痕树;
    private static Node createTree(int N) {
        Node root = null;
        for (int i = 0; i < N; i++) {
            if (i == 0) {
                //1.第一次对折,只有一条折痕,创建根结点;
                root = new Node("down", null, null);
            } else {
                //2.如果不是第一次对折,则使用队列保存根结点;
                Queue<Node> queue = new Queue<>();
                queue.enqueue(root);
                //3.循环遍历队列:
                while (!queue.isEmpty()) {
                    //3.1从队列中拿出一个结点;
                    Node tmp = queue.dequeue();
                    //3.2如果这个结点的左子结点不为空,则把这个左子结点添加到队列中;
                    if (tmp.left != null) {
                        queue.enqueue(tmp.left);
                    }
                    //3.3如果这个结点的右子结点不为空,则把这个右子结点添加到队列中;
                    if (tmp.right != null) {
                        queue.enqueue(tmp.right);
                    }
                    //3.4判断当前结点的左子结点和右子结点都不为空,如果是,则需要为当前结点创建一个值为down的左子结点,一个值为up的右子结点。
                    if (tmp.left == null && tmp.right == null) {
                        tmp.left = new Node("down", null, null);
                        tmp.right = new Node("up", null, null);
                    }
                }
            }
        }
        return root;
    }

    //1.定义结点类
    private static class Node {
        //存储结点元素
        String item;
        //左子结点
        Node left;
        //右子结点
        Node right;

        public Node(String item, Node left, Node right) {
            this.item = item;
            this.left = left;
            this.right = right;
        }
    }
}