@Archger
2016-07-10T15:06:58.000000Z
字数 6091
阅读 790
ACM
二叉树
二叉树的遍历分为三类:前序遍历、中序遍历和后序遍历。
先访问根节点,再遍历左子树,最后遍历右子树;并且在遍历左右子树时,仍需先遍历左子树,然后访问根节点,最后遍历右子树。上图的前序遍历如下。
先遍历左子树、然后访问根节点,最后遍历右子树;并且在遍历左右子树的时候。仍然是先遍历左子树,然后访问根节点,最后遍历右子树。前图的中序遍历如下。
先遍历左子树,然后遍历右子树,最后访问根节点;同样,在遍历左右子树的时候同样要先遍历左子树,然后遍历右子树,最后访问根节点。前图后序遍历结果如下。
用BFS每一层的向下遍历。
#include<iostream>
#include<string>
#include<string.h>
#include<algorithm>
#include<iomanip>
#include<cstdio>
#include<queue>
#include<stack>
using namespace std;
struct node {
int val;
node *lch, *rch;
};
//插入数值
node *insert(node *p, int x)
{
if (p == NULL)
{
node *q = new node;
q->val = x;
q->lch = q->rch = NULL;
return q;
}
else
{
if (x < p->val) p->lch = insert(p->lch, x);
else p->rch = insert(p->rch, x);
return p;
}
}
//查找数值
bool find(node *p, int x)
{
if (p == NULL) return false;
else if (x == p->val) return true;
else if (x < p->val) return find(p->lch, x);
else return find(p->rch, x);
}
//删除数值
node *remove(node *p, int x)
{
if (p == NULL) return NULL;
else if (x < p->val) p->lch = remove(p->lch, x);
else if (x > p->val) p->rch = remove(p->rch, x);
else if (p->lch == NULL)
{
node *q = p->rch;
delete p;
return q;
}
else if (p->lch->rch == NULL)
{
node *q = p->lch;
q->rch = p->rch;
delete p;
return q;
}
else
{
node *q;
for (q = p->rch; q->rch->rch != NULL; q = q->rch);
node *r = q->rch;
q->rch = r->lch;
r->lch = p->lch;
r->rch = p->rch;
delete p;
return r;
}
return p; //没有找到p点
}
int main()
{
node *head = NULL;
int n;
cout << "1: insert 2: find 3: remove\n";
while (cin >> n)
{
int x;
if (n == 1)
{
cin >> x;
head = insert(head, x);
}
else if (n == 2)
{
cin >> x;
if (find(head, x))
{
cout << "Find it!\n";
}
else
{
cout << "Not find!\n";
}
}
else
{
cin >> x;
head = remove(head, x);
}
}
}
void PreOrder2(node *p)
{
stack<node*> stack;
while (p || stack.size())
{
if (p)
{
stack.push(p);
cout << p->val<<" ";
p = p->lch;
}
else
{
p = stack.top();
stack.pop();
p = p->rch;
}
}
}
void InOrder2(node *p)
{
stack<node*> stack;
while (p || stack.size())
{
if (p)
{
stack.push(p);
p = p->lch;
}
else
{
p = stack.top();
stack.pop();
cout << p->val << " ";
p = p->rch;
}
}
}
void PostOrder2(node *p)
{
stack<node*> stack;
while (p || stack.size())
{
while (p)
{
p->left = 1;
p = p->lch;
stack.push(p);
}
while ((stack.top()->left) && (stack.top()->right) && stack.size())
{
p = stack.top();
stack.pop();
cout << p->val << " ";
}
if (stack.size())
{
p->right = 1;
p = p->rch;
}
}
}
void view(node *p)
{
if (p)
{
cout << p->val << " ";
}
}
void PreOrder(node *p)
{
if (p)
{
view(p);
PreOrder(p->lch);
PreOrder(p->rch);
}
}
void InOrder(node *p)
{
if (p)
{
InOrder(p->lch);
view(p);
InOrder(p->rch);
}
}
void PostOrder(node *p)
{
if (p)
{
PostOrder(p->lch);
PostOrder(p->rch);
view(p);
}
}
void LevelOrder(node *p)
{
queue<node*> queue;
queue.push(p);
while (queue.size())
{
p = queue.front();
queue.pop();
cout << p->val << " ";
if (p->lch) queue.push(p->lch);
if (p->rch) queue.push(p->rch);
}
}
#include<iostream>
#include<string>
#include<string.h>
#include<algorithm>
#include<iomanip>
#include<cstdio>
#include<queue>
#include<stack>
using namespace std;
struct node {
int val;
node *lch, *rch;
bool left, right;
};
//插入数值
node *insert(node *p, int x)
{
if (p == NULL)
{
node *q = new node;
q->val = x;
q->lch = q->rch = NULL;
return q;
}
else
{
if (x < p->val) p->lch = insert(p->lch, x);
else p->rch = insert(p->rch, x);
return p;
}
}
//查找数值
bool find(node *p, int x)
{
if (p == NULL) return false;
else if (x == p->val) return true;
else if (x < p->val) return find(p->lch, x);
else return find(p->rch, x);
}
//删除数值
node *remove(node *p, int x)
{
if (p == NULL) return NULL;
else if (x < p->val) p->lch = remove(p->lch, x);
else if (x > p->val) p->rch = remove(p->rch, x);
else if (p->lch == NULL)
{
node *q = p->rch;
delete p;
return q;
}
else if (p->lch->rch == NULL)
{
node *q = p->lch;
q->rch = p->rch;
delete p;
return q;
}
else
{
node *q;
for (q = p->rch; q->rch->rch != NULL; q = q->rch);
node *r = q->rch;
q->rch = r->lch;
r->lch = p->lch;
r->rch = p->rch;
delete p;
return r;
}
return p; //没有找到p点
}
void view(node *p)
{
if (p)
{
cout << p->val << " ";
}
}
void PreOrder(node *p)
{
if (p)
{
view(p);
PreOrder(p->lch);
PreOrder(p->rch);
}
}
void InOrder(node *p)
{
if (p)
{
InOrder(p->lch);
view(p);
InOrder(p->rch);
}
}
void PostOrder(node *p)
{
if (p)
{
PostOrder(p->lch);
PostOrder(p->rch);
view(p);
}
}
void PreOrder2(node *p)
{
stack<node*> stack;
while (p || stack.size())
{
if (p)
{
stack.push(p);
cout << p->val<<" ";
p = p->lch;
}
else
{
p = stack.top();
stack.pop();
p = p->rch;
}
}
}
void InOrder2(node *p)
{
stack<node*> stack;
while (p || stack.size())
{
if (p)
{
stack.push(p);
p = p->lch;
}
else
{
p = stack.top();
stack.pop();
cout << p->val << " ";
p = p->rch;
}
}
}
void PostOrder2(node *p)
{
stack<node*> stack;
while (p || stack.size())
{
while (p)
{
p->left = 1;
p = p->lch;
stack.push(p);
}
while ((stack.top()->left) && (stack.top()->right) && stack.size())
{
p = stack.top();
stack.pop();
cout << p->val << " ";
}
if (stack.size())
{
p->right = 1;
p = p->rch;
}
}
}
void LevelOrder(node *p)
{
queue<node*> queue;
queue.push(p);
while (queue.size())
{
p = queue.front();
queue.pop();
cout << p->val << " ";
if (p->lch) queue.push(p->lch);
if (p->rch) queue.push(p->rch);
}
}
int main()
{
node *head = NULL;
int n;
cout << "1: insert 2: find 3: remove\n";
cout << "4: PreOrder 5: InOrder 6: PostOrder\n";
cout << "7: PreOrder2 8: InOrder2 9: PostOrder2\n";
cout << "10: LevelOrder\n";
while (cin >> n)
{
int x;
if (n == 1)
{
cin >> x;
head = insert(head, x);
}
else if (n == 2)
{
cin >> x;
if (find(head, x))
{
cout << "Find it!\n";
}
else
{
cout << "Not find!\n";
}
}
else if (n == 3)
{
cin >> x;
head = remove(head, x);
}
else if (n == 4)
{
PreOrder(head);
cout << endl;
}
else if (n == 5)
{
InOrder(head);
cout << endl;
}
else if (n == 6)
{
PostOrder(head);
cout << endl;
}
else if (n == 7)
{
PreOrder2(head);
cout << endl;
}
else if (n == 8)
{
InOrder2(head);
cout << endl;
}
else if (n == 9)
{
PostOrder(head);
cout << endl;
}
else if (n == 10)
{
LevelOrder(head);
cout << endl;
}
}
}
CSDN中关于二叉树遍历详解
PAT 1020. Tree Traversals (25)
题解:
#include<iostream>
#include<string>
#include<string.h>
#include<algorithm>
#include<iomanip>
#include<cstdio>
#include<queue>
#include<stack>
using namespace std;
struct node
{
long long val;
node *lch, *rch;
};
node *head = NULL;
long long n, post[31], in[31], num;
bool book[31];
node* solve(int x, int y, node* p)
{
if (x > y||x==0||y==0) return NULL;
p = new node;
p->val = post[y];
p->lch = NULL;
p->rch = NULL;
int left = 0, right = 0, s = 0, t = 0, cur = 0;
for (int i = 1; i <= n; i++)
{
if (in[i] == post[y])
{
book[i] = 1;
s = i;
cur = i;
t = i;
break;
}
}
while (t - 1 >= 1 && !book[t - 1]) t--;
while (s + 1 <= n && !book[s + 1]) s++;
left = cur - t;
right = s - cur;
p->lch = solve(x, x + left - 1, p->lch);
p->rch = solve(y - right, y - 1, p->rch);
return p;
}
void LevelOrder(node *p)
{
queue<node*> queue;
queue.push(p);
while (queue.size())
{
p = queue.front();
queue.pop();
cout << p->val;
num++;
if (num != n) cout << " ";
if (p->lch) queue.push(p->lch);
if (p->rch) queue.push(p->rch);
}
}
int main()
{
while (cin >> n)
{
num = 0;
memset(book, 0, sizeof(book));
if (n <= 0) return 0;
for (int i = 1; i <= n; i++)
{
cin >> post[i];
}
for (int i = 1; i <= n; i++)
{
cin >> in[i];
}
head = solve(1, n, head);
LevelOrder(head);
cout << endl;
}
}