一、构建二叉树
我们构建一个如下图所示的二叉树
我们使用下面的数据结构来描述这个二叉树
public class Node {
private String name = "";
public Node leftChild;
public Node rightChild;
public Node(String name) {
this.name = name;
}
public Node() {
}
public void setName(String name) {
this.name = name;
}
二、二叉树的遍历
前序遍历
/**
* 前序遍历
*/
public String readPre() {
StringBuilder result = new StringBuilder();
result.append(name); //前序遍历
if (leftChild != null) {
result.append(leftChild.readPre());
}
if (rightChild != null) {
result.append(rightChild.readPre());
}
return result.toString();
}
中序遍历
/**
* 中序遍历
*/
public String readMid() {
StringBuilder result = new StringBuilder();
if (leftChild != null) {
result.append(leftChild.readMid());
}
result.append(name); //中序遍历
if (rightChild != null) {
result.append(rightChild.readMid());
}
return result.toString();
}
后序遍历
/**
* 后序遍历
*/
public String readEnd() {
StringBuilder result = new StringBuilder();
if (leftChild != null) {
result.append(leftChild.readEnd());
}
if (rightChild != null) {
result.append(rightChild.readEnd());
}
result.append(name); //后序遍历
return result.toString();
}
从上面可以看出,前序、后序、中序遍历的算法基本上差不多,其主要区别是在对根节点的访问顺序不同,然后利用递归的方式进行实现。
层序遍历:
/**
* 层序遍历
*/
public String readLevel() {
Queue<Node> queue = new LinkedList<>();
StringBuilder result = new StringBuilder();
queue.offer(this);
while (!queue.isEmpty()) {
Node curNode = queue.poll();
result.append(curNode.name);
if (curNode.leftChild != null) {
queue.offer(curNode.leftChild);
}
if (curNode.rightChild != null) {
queue.offer(curNode.rightChild);
}
}
return result.toString();
}
跟其他遍历不同,层序遍历是需要利用队列来实现的。首先将根节点放到队列中,然后遍历循环,依次将左孩子和右孩子放置到队列中。
三、还原二叉树
在第二章节中,我们获得的前序、中序、后序、层序的结果依次如下:
String pre = "ABDGHCEIF"; //前序遍历 String mid = "GDHBAEICF"; //中序遍历 String end = "GHDBIEFCA"; //后序遍历 String level = "ABCDEFGHI"; //层序遍历
那能否通过上面的字符串还原出二叉树的形状呢?这个需要分情况进行讨论
前序+中序:
思路:通过前序获得根节点的位置,利用根节点将中序序列分为左子树和右子树,然后不断的递归划分即可。
代码:
/**
* 根据前序和中序排序表获取树
*/
private static Node buildTreeByPreMid(char[] pre, int preBegin, int preEnd, char[] mid, int midBegin, int midEnd) {
Node root = new Node();
root.setName(pre[preBegin] + "");
int midRootLoc = 0;
for (int i = midBegin; i <= midEnd; i++) {
if (mid[i] == pre[preBegin]) {
midRootLoc = i;
break;
}
}
//递归得到左子树
if (preBegin + (midRootLoc - midBegin) >= preBegin + 1 && (midRootLoc - 1) >= midBegin) {
Node leftChild = buildTreeByPreMid(pre, preBegin + 1, preBegin + (midRootLoc - midBegin),
mid, midBegin, midRootLoc - 1);
root.leftChild = leftChild;
}
//递归得到右子树
if (preEnd >= (preEnd - (midEnd - midRootLoc) + 1) && (midEnd >= midRootLoc + 1)) {
Node rightChild = buildTreeByPreMid(pre, preEnd - (midEnd - midRootLoc) + 1, preEnd,
mid, midRootLoc + 1, midEnd);
root.rightChild = rightChild;
}
return root;
}
后序+中序
思路:通过后续获得根节点的位置,然后在中序中获取左子树和右子树,然后递归划分即可。
代码:
/**
* 根据后序和中序遍历还原树
*/
private static Node buildTreeByMidEnd(char[] mid, int midBegin, int midEnd, char[] end, int endBegin, int endEnd) {
Node root = new Node();
root.setName(end[endEnd] + "");
int midRootLoc = 0;
for (int i = midEnd; i >= midBegin; i--) {
if (mid[i] == end[endEnd]) {
midRootLoc = i;
break;
}
}
//还原左子树
if (midRootLoc - 1 >= midBegin && (endBegin + (midRootLoc - midBegin) - 1 >= endBegin)) {
Node leftChild = buildTreeByMidEnd(mid, midBegin, midRootLoc - 1, end, endBegin, endBegin + (midRootLoc - midBegin) - 1);
root.leftChild = leftChild;
}
//还原右子树
if (midEnd >= midRootLoc + 1 && (endEnd - 1 >= endEnd - (midEnd - midRootLoc))) {
Node rightChild = buildTreeByMidEnd(mid, midRootLoc + 1, midEnd, end, endEnd - (midEnd - midRootLoc), endEnd - 1);
root.rightChild = rightChild;
}
return root;
}
层序+中序
思路:根据层序遍历获取根节点的位置,然后将中序划分为左子树和右子树,然后根据划分出的左子树和右子树分别在层序遍历中获取其对应的层序顺序,然后递归调用划分即可。
代码如下:
/**
* 根据层序遍历和中序遍历得到结果
* @return
*/
private static Node buildTreeByMidLevel(char[] mid, char[] level, int midBegin, int midEnd) {
Node root = new Node(level[0] + "");
int midLoc = -1;
for (int i = midBegin; i <= midEnd; i++) {
if (mid[i] == level[0]) {
midLoc = i;
break;
}
}
if (level.length >= 2) {
if (isLeft(mid, level[0], level[1])) {
Node left = buildTreeByMidLevel(mid, getLevelStr(mid, midBegin, midLoc - 1, level), midBegin, midLoc - 1);
root.leftChild = left;
if (level.length >= 3 && !isLeft(mid, level[0], level[2])) {
Node right = buildTreeByMidLevel(mid, getLevelStr(mid, midLoc + 1, midEnd, level), midLoc + 1, midEnd);
root.rightChild = right;
}
} else {
Node right = buildTreeByMidLevel(mid, getLevelStr(mid, midLoc + 1, midEnd, level), midLoc + 1, midEnd);
root.rightChild = right;
}
}
return root;
}
/**
* 将中序序列中midBegin与MidEnd的字符依次从level中提取出来,保持level中的字符顺序不变
*/
private static char[] getLevelStr(char[] mid, int midBegin, int midEnd, char[] level) {
char[] result = new char[midEnd - midBegin + 1];
int curLoc = 0;
for (int i = 0; i < level.length; i++) {
if (contains(mid, level[i], midBegin, midEnd)) {
result[curLoc++] = level[i];
}
}
return result;
}
/**
* 如果str字符串的begin和end位置之间(包括begin和end)含有字符target,则返回true。
*/
private static boolean contains(char[] str, char target, int begin, int end) {
for (int i = begin; i <= end; i++) {
if (str[i] == target) {
return true;
}
}
return false;
}
其他的遍历组合均不能还原出二叉树的形状,因为无法确认其左右孩子。例如,前序为AB,后序为AB,则无法确认出,B节点是A节点的左孩子还是右孩子,因此无法还原。
完整代码已经上传至github
https://github.com/YanYoJun/Tree