二叉树(三)——已知遍历序列构造二叉树(java版)

来源:互联网 发布:心理学书籍推荐 知乎 编辑:程序博客网 时间:2024/05/16 05:46

已知先序和中序构造二叉树

任何n(n≥0)个不同结点的二叉树,都可由他的先序序列和中序序列唯一地确定
当n=0时,二叉树为空,正确。
若二叉树具有n(n>0)个不同结点,其先序序列是a0a1…an-1;中序序列是b0b1…bk-1 bk bk+1…bn-1。
因为在先序遍历过程中,访问根结点后,紧跟着遍历左子树,最后再遍历右子树。所以a0必定是二叉树的根结点,而且a0必然在中序序列中出现。也就是说,在中序序列中必有某个bk(0≤k≤n-1)就是根结点a0。
由于bk是根结点,而在中序遍历过程中,先遍历左子树,再访问根结点,最后再遍历右子树。所以在中序序列中,b0b1…bk-1必是根结点bk的左子树的中序序列,即bk的左子树有k个结点,而bk+1…bn-1必是根结点bk右子树的中序序列,即bk的右子树有n-k-1个结点。
根据此思想,可以给出下列算法:

/*已知先序和中序  构造二叉树*/private TreeNode myBuildTree(String[] inorder, int instart, int inend,            String[] preorder, int prestart, int preend) {        if (instart > inend) {            return null;        }        TreeNode<String> head = new TreeNode<String>(preorder[prestart]);        int position = findPosition(inorder, instart, inend, preorder[prestart]);        head.setLeftChild(myBuildTree(inorder, instart, position - 1,                preorder, prestart + 1, prestart + position - instart));        head.setRightChild(myBuildTree(inorder, position + 1, inend,                preorder, position - inend + preend + 1, preend));        return head;    }    public TreeNode buildTree(String[] preorder, String[] inorder) {        if (inorder.length != preorder.length) {            return null;        }        return myBuildTree(inorder, 0, inorder.length - 1, preorder, 0, preorder.length - 1);    }

已知中序和后序构造二叉树

任何n(n≥0)个不同结点的二叉树,都可由他的中序序列和后序序列唯一地确定
当n=0时,二叉树为空,正确。
已知二叉树具有n(n>0)个不同结点,其中中序序列是b0b1…bn-1;后序序列是a0a1…an-1。因为在后序遍历过程中,先遍历左子树,再遍历右子树,最后访问根结点。所以an-1必定是二叉树的根结点,而且an-1必定在中序序列中出现。也就是说,在中序序列中必有某个结点bk(0≤k≤n-1)就是根结点an-1。
由于bk是根结点,而在中序遍历过程中,先遍历左子树,再访问根结点,最后再遍历右子树。所以在中序序列中,b0…bk-1必是根结点bk左子树的中序序列,即bk的左子树有k个结点。而bk+1…bn-1必是根结点bk右子树的中序序列,即bk的右子树有n-k-1个结点。
根据此思想,可以给出下列算法:

/*已知中序和后序   构造二叉树*/private TreeNode myBuildTree1(String[] inorder, int instart, int inend,            String[] postorder, int poststart, int postend) {        if (instart > inend) {            return null;        }        TreeNode<String> head1 = new TreeNode<String>(postorder[postend]);        int position = findPosition(inorder, instart, inend, postorder[postend]);        head1.setLeftChild(myBuildTree1(inorder, instart, position - 1,                postorder, poststart, poststart + position - instart - 1));        head1.setRightChild(myBuildTree1(inorder, position + 1, inend,                postorder, poststart + position - instart, postend - 1));        return head1;    }public TreeNode buildTree1(String[] inorder, String[] postorder) {        if (inorder.length != postorder.length) {            return null;        }        return myBuildTree1(inorder, 0, inorder.length - 1, postorder, 0, postorder.length - 1);    }

其中,给出由先序和中序构造二叉树的过程:
这里写图片描述
代码执行过程:
这里写图片描述

总结:
在已知序列的情况下构造二叉树的过程中,采取了递归的方法,而这种思想在将二叉树的顺序存储结构转换成二叉链存储结构中,会受到应用。
对于构造二叉树过程,将上述算法简化成以下形式进行表达:
这里写图片描述

这里写图片描述

1 0