数据结构问题---二叉树遍历

本文介绍了一种使用C++实现的二叉树数据结构及遍历方法,包括前序、中序、后序和层次遍历,并展示了如何构建二叉树、查找节点以及计算树的深度。

-------------------------------------
典型例题17:数据结构问题---二叉树遍历
-------------------------------------
 1    #include <iostream>
 2    #include <cstdlib>
 3    using namespace std;
 4   
 5    typedef char ElemType;
 6   
 7    struct BTreeNode{
 8          ElemType data;
 9          BTreeNode* left;
10          BTreeNode* right;
11    };
12   
13    void InitBTree(BTreeNode *&BT)
14    {
15        BT = NULL;
16    }
17   
18    void CreateBTree(BTreeNode *&BT,char* a)
19    {
20        const int maxsize = 10;
21        BTreeNode * s[maxsize];
22        int top = -1;
23        BT = NULL;
24        BTreeNode *p;
25        int k;
26        int i = 0;
27        while(a[i])
28        {
29            switch (a[i])
30            {
31                case ' '://nop for spaces;
32                    break;
33                case '(':
34                    if(top == maxsize -1)
35                    {
36                        cout << "stack to small" << endl;
37                        exit(1);
38                    }
39                    top++;
40                    s[top] = p;
41                    k = 1;
42                    break;
43                case ')':
44                    if (top == -1)
45                    {
46                        cout << "input string error!" << endl;
47                    }
48                    top--;
49                    break;
50                case ',':
51                    k = 2;
52                    break;
53                default:
54                    p = new BTreeNode;
55                    p->data = a[i];
56                    p->left =p->right = NULL;
57                    if(BT == NULL)
58                        BT = p;
59                    else{
60                        if(k ==1) s[top]->left = p;
61                        else s[top]->right = p;
62                    }
63            }
64            i++;
65        }
66    }
67   
68    bool EmptyBTree(BTreeNode* BT)
69    {
70        return BT == NULL;
71    }
72   
73    int depthbtree(BTreeNode * BT)
74    {
75        if(EmptyBTree(BT))
76            return 0;
77        else{
78            int dep1=depthbtree(BT->left);
79            int dep2=depthbtree(BT->right);
80            if (dep1>dep2)
81            {
82                return dep1+1;
83            }
84            else
85            {
86                return dep2+1;
87            }
88        }
89    }
90   
91    bool FindBTree(BTreeNode *BT,ElemType &x)
92    {
93       if(EmptyBTree(BT))
94            return 0;
95        else{
96            if (BT->data == x)
97            {
98                x = BT->data;
99                return true;
100            }
101            else
102            {
103                if(FindBTree(BT->left,x)) return true;
104                if(FindBTree(BT->right,x)) return true;
105                return false;
106            }
107        }
108    }
109   
110    //print the tree
111   
112    void printbtree(BTreeNode * BT)
113    {
114   
115       if(!EmptyBTree(BT))
116       {
117           cout<<BT->data;
118           if (BT->left!=NULL||BT->right!=NULL)
119           {
120               cout<<'(';
121               printbtree(BT->left);
122               if(BT->right!=NULL)
123                   cout << ',';
124               printbtree(BT->right);
125               cout<<')';
126           }
127       }
128    }
129   
130    // clear the tree
131   
132    void clearbtree(BTreeNode *&BT)
133    {
134        if (!EmptyBTree(BT))
135        {
136            clearbtree(BT->left);
137            clearbtree(BT->right);
138            delete BT;
139            BT = NULL;
140        }
141    }
142   
143    void PreOrder(BTreeNode *BT)
144    {
145        if (!EmptyBTree(BT)){
146            cout<<BT->data<<' ';
147            PreOrder(BT->left);
148            PreOrder(BT->right);
149        }
150    }
151   
152    void InOrder(BTreeNode *BT)
153    {
154        if(!EmptyBTree(BT)){
155            InOrder(BT->left);
156            cout<<BT->data<<' ';
157            InOrder(BT->right);
158        }
159    }
160   
161    void PostOrder(BTreeNode *BT)
162    {
163        if(!EmptyBTree(BT)){
164            PostOrder(BT->left);
165            PostOrder(BT->right);
166            cout<<BT->data<<' ';   
167        }
168    }
169   
170    void LevelOrder(BTreeNode* BT)
171    {
172        const int maxsize = 30;
173        BTreeNode* q[maxsize];
174        int front = 0,rear = 0;
175        BTreeNode* p;
176        if(!EmptyBTree(BT)){
177            rear = (rear+1)%maxsize;
178            q[rear] = BT;
179        }
180        while(front != rear){
181            front = (front+1)%maxsize;
182            p = q[front];
183            cout<<p->data<<' ';
184            if(p->left!=NULL){
185                rear = (rear+1)%maxsize;
186                q[rear] = p->left;
187            }
188            if(p->right!=NULL){
189                rear = (rear+1)%maxsize;
190                q[rear] = p->right;
191            }
192               
193        }//end of while
194    }
195    int main()
196    {
197        BTreeNode * bt;
198        InitBTree(bt);
199        char b[50];
200        cout << "请输入用广义表表示的字符串:" << endl;
201        cin.getline(b,sizeof(b));
202        CreateBTree(bt,b);
203        printbtree(bt);
204        cout<<endl;
205        cout << "前序:";PreOrder(bt);cout<<endl;
206        cout << "中序:";InOrder(bt);cout<< endl;
207        cout << "后序:";PostOrder(bt);cout<< endl;
208        cout << "按层:";LevelOrder(bt);cout<< endl;
209   
210        ElemType x;
211   
212        cout<<"输入一个待查字符:";
213        cin>>x;
214        if(FindBTree(bt,x))
215            cout<<"Find "<<x<< " Success"<<endl;
216        else
217            cout<<"Find "<<x<< " Failure"<<endl;
218        cout<<"深度:";cout<<depthbtree(bt)<<endl;
219        clearbtree(bt);
220        return 0;
221    }
----------------------------
$ ./a.out
请输入用广义表表示的字符串:
a(b(c),d(e(f,g),h(,i)))
a(b(c),d(e(f,g),h(,i)))
前序:a b c d e f g h i
中序:c b a f e g d h i
后序:c b f g e i h d a
按层:a b d c e h f g i
输入一个待查字符:e
Find e Success
深度:4

----------------------------

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值