数据结构&算法Java版(四) 二叉树

欢迎关注我的B站账号:卍卍子非鱼卍卍

二叉树

二叉树的构造

现有格式化字符串str = "A(B(D(,G)),C(E,F))"

其中每一个字母代表一个节点的值,字母后面的括号"()"代表它的子树,其中逗号","前为左子树,逗号","后为右子树。

上述字符串str构造出的二叉树如下:
在这里插入图片描述

算法思路

1 构造

1.定义栈结构Stack<BTNode<Character>> stack用于临时存储树节点

2.定义节点类型BTNode<Character> p用于保存当前处理的节点

3.定义布尔值isLeft判断当前处理的是哪个子树,true为左子树,false为右子树

4.以字符ch的形式遍历str

4.1 当ch为字母的时候,将ch作为val值新建节点,保存在p中,判断根节点root == null,若为true,说明此时二叉树为空,直接将root置为p,否则取栈顶元素stack.peek(),并判断布尔值isLeft,若为true,说明此时应该处理左子树,将栈顶元素的左子树置为p,即stack.peek().left = p,否则将其右子树置为p

4.2 当ch为左括号(时,说明即将处理p的子树,将p入栈,并将isLeft置为true

4.3 当ch为右括号)时,说明当前节点的子树已处理完毕,栈顶元素出栈

4.4 当ch为逗号,时,说明左子树处理完毕,即将处理右子树,将isLeft置为false

5.遍历完毕,结束

2 输出

1.定义字符串str存储输出结果

2.判断root是否为null

2.1 root不为null:

2.1.1 将root.val加入str

2.1.2判断root的左右子树是否至少有一个不为null,若有一个不为null,说明节点存在子树,此时str中加入(,并递归处理左右子树,在处理右子树前注意:判断右子树是否为null,若不为nullstr中应加入,。左右子树处理完后,str中加入)

2.2 rootnull,直接返回str

代码

二叉树节点BTNode

class BTNode<T> {
    T val;
    BTNode<T> left;
    BTNode<T> right;

    public BTNode() {
        this(null);
    }

    public BTNode(T val) {
        this.val = val;
        this.left = null;
        this.right = null;
    }
}

二叉树基本运算

class MyBTree {
    BTNode<Character> root;

    public MyBTree() {
        root = null;
    }

    // 返回根节点
    public BTNode<Character> getRoot() {
        return this.root;
    }

    // 根据规范字符串建立二叉树
    public void CreatTree(String str) {
        Stack<BTNode<Character>> stack = new Stack<BTNode<Character>>();
        BTNode<Character> p = null;
        boolean isLeft = true;
        for (char ch : str.toCharArray()) {
            switch (ch) {
                case '(':
                    stack.push(p);
                    isLeft = true;
                    break;
                case ')':
                    stack.pop();
                    break;
                case ',':
                    isLeft = false;
                    break;
                default:
                    p = new BTNode<Character>(ch);
                    if (root == null) {
                        root = p;
                    } else {
                        if (isLeft) {
                            stack.peek().left = p;
                        } else {
                            stack.peek().right = p;
                        }
                    }
            }
        }
    }

    // 根据给定val值找到对应节点并返回
    public BTNode<Character> getNode(BTNode<Character> root, char val) {
        if (root == null) {
            return null;
        }
        if (root.val == val) {
            return root;
        } else {
            BTNode<Character> p = getNode(root.left, val);
            if (p != null) {
                return p;
            } else {
                return getNode(root.right, val);
            }
        }
    }

    // 获取二叉树最大深度
    public int getDepth(BTNode<Character> root) {
        return root == null ? 0 : Math.max(getDepth(root.left) + 1, getDepth(root.right) + 1);
    }

    // 格式化输出二叉树
    public String toString(BTNode<Character> root) {
        String str = "";
        if (root != null) {
            str += root.val;
            if (root.left != null || root.right != null) {
                str += "(";
                str += toString(root.left);
                if (root.right != null) {
                    str += ",";
                }
                str += toString(root.right);
                str += ")";
            }
        }
        return str;
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值