1. 程式人生 > 實用技巧 >8.4 樹的遍歷

8.4 樹的遍歷

1.遞迴先序遍歷:

1 void PreOrder(BiTree T)
2 {
3     if(T!=NULL)
4     {
5         visit(T);
6         PreOrder(T->lchild);
7         PreOrder(T->rchild);
8     }    
9 }

2.非遞迴先序遍歷:

 1  void PreOrder2(BiTree T)
 2  {
 3       InitStack(S);
 4       BiTree p=T;
 5       while(p||!IsEmpty(S))
 6       {
7 if(p) 8 { 9 visit(p->data); 10 push(S,p); 11 p=p->lchild; 12 } 13 else 14 { 15 pop(S,p); 16 p=p->rchild; 17 } 18 } 19 }

3.遞迴中序遍歷:

1 void InOrder(BiTree T)
2 {
3 if(T!=NULL) 4 { 5 InOrder(T->lchild); 6 Visit(T); 7 InOrder(T->rchild); 8 } 9 }

4.非遞迴中序遍歷:

 1  void InOrder2(BiTree T)
 2  {
 3      InitStack(S);
 4      BiTree p=T;
 5      while(p||!IsEmpty(S))
 6      {
 7          if(p)
 8          {
 9              Push(S,p);
10 p=p->lchild; 11 } 12 else 13 { 14 Pop(S,p); 15 visit(p); 16 p=p->rchild; 17 } 18 } 19 }

5.遞迴後序遍歷:

1 void PostOrder(BiTree T)
2 {
3     if(T!=NULL)
4     {
5         PostOrder(T->lchild);
6         PostOrder(T->rchild);
7         visit(T);
8     }
9 }

6.非遞迴後序遍歷:

 1  void PostOrder2(BiTree T)
 2  {
 3      InitStack(S);
 4      BiTree p=T;
 5      r=NULL;
 6      while(p||!IsEmpty(S))
 7      {
 8          if(p)
 9          {
10              push(S,p);
11              p=p->lchild;
12          }
13          else
14          {
15              GetTop(S,p);
16              if(p->rchild&&p->rchild!=r)
17              {
18                  p=p->rchild;
19                  push(S,p);
20                  p=p->lchild;
21              }
22              else
23              {
24                  pop(S,p);
25                  visit(p->data);
26                  r=p;
27                  p=NULL;
28              }
29          }
30      }
31  }

7.層次遍歷:

 1  void LevelOrder(BiTree T)
 2  {
 3      InitQueue(Q);
 4      BiTree p;
 5      EnQueue(Q,T);
 6      while(!IsEmpty(Q))
 7      {
 8          DeQueue(Q,p);
 9          visit(p);
10          if(p->lchild!=NULL)
11              EnQueue(Q,p->lchild);
12          if(p->rchild!=NULL)
13              EnQueue(Q,p->rchild);
14      }
15  }

8.另一種寫法風格:

 1  public static void preOrderUnRecur(Node head) {
 2      System.out.print("pre-order: ");
 3      if (head != null) {
 4          Stack<Node> stack = new Stack<Node>();
 5          stack.add(head);
 6          while (!stack.isEmpty()) {
 7              head = stack.pop();
 8              System.out.print(head.value + " ");
 9              if (head.right != null) {
10                  stack.push(head.right);
11              }
12              if (head.left != null) {
13                  stack.push(head.left);
14              }
15          }
16      }
17      System.out.println();
18  }
19  
20  public static void inOrderUnRecur(Node head) {
21      System.out.print("in-order: ");
22      if (head != null) {
23          Stack<Node> stack = new Stack<Node>();
24          while (!stack.isEmpty() || head != null) {
25              if (head != null) {
26                  stack.push(head);
27                  head = head.left;
28              } else {
29                  head = stack.pop();
30                  System.out.print(head.value + " ");
31                  head = head.right;
32              }
33          }
34      }
35      System.out.println();
36  }
37  
38  public static void posOrderUnRecur1(Node head) {
39      System.out.print("pos-order: ");
40      if (head != null) {
41          Stack<Node> s1 = new Stack<Node>();
42          Stack<Node> s2 = new Stack<Node>();
43          s1.push(head);
44          while (!s1.isEmpty()) {
45              head = s1.pop();
46              s2.push(head);
47              if (head.left != null) {
48                  s1.push(head.left);
49              }
50              if (head.right != null) {
51                  s1.push(head.right);
52              }
53          }
54          while (!s2.isEmpty()) {
55              System.out.print(s2.pop().value + " ");
56          }
57      }
58      System.out.println();
59  }
60  
61  public static void posOrderUnRecur2(Node h) {
62      System.out.print("pos-order: ");
63      if (h != null) {
64          Stack<Node> stack = new Stack<Node>();
65          stack.push(h);
66          Node c = null;
67          while (!stack.isEmpty()) {
68              c = stack.peek();
69              if (c.left != null && h != c.left && h != c.right) {
70                  stack.push(c.left);
71              } else if (c.right != null && h != c.right) {
72                  stack.push(c.right);
73              } else {
74                  System.out.print(stack.pop().value + " ");
75                  h = c;
76              }
77          }
78      }  
79      System.out.println();
80  }