二叉树的遍历
遍历:按照某一种次序依次访问各个结点,每个结点恰好访问一次。
二叉树前序遍历
1.递归实现前序遍历
<pre>
public static void main(String[] args) {
//设置结点之间的关系
BinNode root = new BinNode(0);
BinNode node1 = new BinNode(1);
BinNode node2 = new BinNode(2);
root.left = node1; root.right = node2;
node1.parent = root; node2.parent = root;
BinNode node3 = new BinNode(3);
BinNode node4 = new BinNode(4);
node1.left = node3; node1.right = node4;
node3.parent = node1; node4.parent = node1;
BinNode node5 = new BinNode(5);
BinNode node6 = new BinNode(6);
node2.left = node5; node2.right = node6;
node5.parent = node2; node6.parent = node2;
BinNode node7 = new BinNode(7);
BinNode node8 = new BinNode(8);
node3.left = node7; node3.right = node8;
node7.parent = node3; node8.parent = node3;
preorder(new BinTree(root));
}
</pre>
<pre>
public static void preorder(BinTree tree) {
BinNode x = tree.root;
if (x == null) {
return;
}
System.out.println(x);
preorder(new BinTree(x.left));
preorder(new BinTree(x.right));
}
</pre>
2.迭代1实现前序遍历:主要通过栈来实现
<pre>
public static void preorder(BinNode root) {
BinNodeStackArray s = new BinNodeStackArray(100);
if(root != null) {
s.push(root);
}
while(!s.isEmpty()) {
BinNode x = s.pop();
System.out.println(x);
if (x.right != null) {
s.push(x.right);
}
if (x.left != null) {
s.push(x.left);
}
}
}
</pre>
3.迭代2实现前序遍历:以上消除尾递归的方法不易推广,因此另寻它法。主要思路如下图所示:
<pre>
public static void preorder(BinNode root) {
BinNodeStackArray s = new BinNodeStackArray(100);
BinNode x = root;
while(true) {
vistAlongLeftTree(x, s);
if (s.isEmpty()) {
break;
}
x = s.pop();
}
}
</pre>
<pre>
public static void vistAlongLeftTree(BinNode root, BinNodeStackArray s) {
BinNode x = root;
while(x != null) {
System.out.println(x);
if (root.right != null) {
s.push(x.right);
}
x = x.left;
}
}
</pre>
二叉树中序遍历
1.递归实现
<pre>
public static void infixorder(BinNode root) {
if (root == null) {
return;
}
infixorder(root.left);
System.out.println(root);
infixorder(root.right);
}
</pre>
2.迭代实现:主要思路如下。
<pre>
public static void deepstAlongLeftTree(BinNode root, BinNodeStackArray s) {
BinNode x = root;
while(x != null) {
s.push(x);
x = x.left;
}
}
</pre>
<pre>
public static void infixorder(BinNode root) {
BinNode x = root;
BinNodeStackArray s = new BinNodeStackArray(100);
while(true) {
deepstAlongLeftTree(x,s);
if (s.isEmpty()) {
break;
}
x = s.pop();
System.out.println(x);
x = x.right;
}
}
</pre>
二叉树后序遍历
1.递归实现:
<pre>
public static void postorder(BinNode root) {
if (root == null) {
return;
}
postorder(root.left);
postorder(root.right);
System.out.println(root);
}
</pre>
2.迭代实现:主要思路如下图:
<pre>
public static void highestLeafVisibleFromTheLeft(BinNode root, BinNodeStackArray s) {
BinNode x = root;
while(x != null) {
if (x.hasLeftChild()) {
if (x.hasRightChild()) {
s.push(x.right);
s.push(x.left);
x = x.left;
}
}
else if (x.hasRightChild()) {
s.push(x.right);
x = x.right;
}else {
break;
}
}
}
</pre>
<pre>
public static void highestLeafVisibleFromTheLeft(BinNode root, BinNodeStackArray s) {
BinNode x = root;
while(x != null) {
if (x.hasLeftChild()) {
if (x.hasRightChild()) {
s.push(x.right);
s.push(x.left);
x = x.left;
}
}
else if (x.hasRightChild()) {
s.push(x.right);
x = x.right;
}else {
break;
}
}
}
</pre>