首页 > 代码库 > 二叉树遍历

二叉树遍历

  1 #include<stdio.h>
  2 #include<iostream>
  3 
  4 using namespace std;
  5 struct TreeNode
  6 {
  7     int data;
  8     TreeNode *Left, *Right;
  9 };
 10 
 11 TreeNode *root;
 12 
 13 //中序遍历
 14 void inorder(TreeNode* CurrentNode)
 15 {
 16     if (CurrentNode)
 17     {
 18         inorder(CurrentNode->Left);
 19         cout<<Current->data;
 20         inorder(CurrentNode->Right);
 21     }
 22 }
 23 
 24 //前序遍历
 25 void preorder(TreeNode* CurrentNode)
 26 {
 27     if (CurrentNode)
 28     {
 29         cout<<CurrentNode->data;
 30         precoder(CurrentNode->Left);
 31         preorder(CurrentNode->Right);
 32     }
 33 }
 34 
 35 //后序遍历
 36 void postorder(TreeNode* CurrentNode)
 37 {
 38     if (CurrentNode)
 39     {
 40         postorder(CurrentNode->Left);
 41         postorder(CurrentNode->Right);
 42         cout<<CurrentNode->data;
 43     }
 44 }
 45 
 46 //中序游标 (非递归中序遍历)
 47 void NonrecInorder(TreeNode* root)
 48 {
 49     stack<TreeNode*> s;
 50     TreeNode *CurrentNode = root;
 51     while (1)
 52     {
 53         while (CurrentNode)
 54         {
 55             s.push(CurrentNode);
 56             CurrentNode = CurrentNode->Left;
 57         }
 58         if (!s.empty())
 59         {
 60             CurrentNode = s.top();
 61             s.pop();
 62             cout<<CurrentNode->data<<endl;
 63             CurrentNode = CurrentNode->Right;
 64         }
 65         else break;
 66     }
 67 }
 68 
 69 //前序游标(非递归前序遍历)
 70 void preorderIterator(TreeNode* root)
 71 {
 72     stack<TreeNode*> s;
 73     TreeNode* CurrentNode = root;
 74     while (1)
 75     {
 76         while(CurrentNode)
 77         {
 78             cout<<CurrentNode->data<<endl;
 79             s.push(CurrentNode);
 80             CurrentNode = CurrentNode->Left;
 81         }
 82         if (!s.empty())
 83         {
 84             CurrentNode = s.top();
 85             s.pop();
 86             CurrentNode = CurrentNode->Right;
 87         }
 88         else break;
 89     }
 90 }
 91 
 92 //后序游标(非递归后序遍历)
 93 struct StackItem
 94 {
 95     TreeNode *p;
 96     bool RVisited;
 97 };
 98 
 99 void postorderIterator()
100 {
101     stack<StackItem> s;
102     TreeNode *CurrentNode = root;
103     StackItem item;
104 
105     while(1)
106     {
107         while(CurrentNode)
108         {
109             item.p = CurrentNode;
110             item.RVisited = false;
111             s.push(item);
112             CurrenNode = CurrentNode->Left;
113         }
114         if (!s.empty())
115         {
116             item = s.top();
117             s.pop();
118             CurrentNode = item.p;
119             if (item.RVisited == false)
120             {
121                 item.RVisited = true;
122                 s.push(item);
123                 CurrentNode = CurrentNode->Right;
124             }
125             else
126             {
127                 cout<<CurrentNode->data<<endl;
128                 CurrentNode = 0;
129             }
130         }
131         else break;
132     }
133 }
134 
135 //按层次访问
136 void LevelOrder(TreeNode* root)
137 {
138     queue<TreeNode*> q;
139     TreeNode *t, *CurrentNode = root;
140     while (CurrentNode)
141     {
142         cout<<CurrentNode->data;
143         if (CurrentNode->Left)
144             q.push(CurrentNode->Left);
145         if (CurrentNode->Right)
146             q.push(CurrentNode->Right);
147         t = q.front();
148         q.pop();
149         if (t)
150             CurrentNode = t;
151         else
152             CurrentNode = 0;
153     }
154 }

 

二叉树遍历