数据结构问题---二叉搜索树问题

来源:互联网 发布:全职高手 知乎 编辑:程序博客网 时间:2024/06/07 07:59

-------------------------------------
典型例题15:数据结构问题---二叉搜索树问题
-------------------------------------
 1    #include <iostream>
 2    #include <cstdlib>
 3   
 4    using namespace std;
 5   
 6    typedef int ElemType;
 7   
 8    typedef struct BTreeNode {
 9        ElemType data;
10        BTreeNode* left;
11        BTreeNode* right;
12    };
13   
14    void InitBTree(BTreeNode*& BT)
15    {
16        BT = NULL;
17    }
18   
19    bool find(BTreeNode* BST,ElemType& item)
20    {
21        if(BST == NULL) return false;
22        else{
23            if (item == BST->data)
24                {
25                    item = BST->data;
26                    return true;
27                }else if(item<BST->data)
28                return find(BST->left,item);
29            else
30                return find(BST->right,item);
31        }
32    }
33   
34   
35    bool find1(BTreeNode* BST,ElemType& item)
36    {
37        if(BST == NULL) return false;
38        else{
39            while (BST!=NULL)
40                {
41                    if (item == BST->data)
42                        {
43                            item = BST->data;
44                            return true;
45                        }else if(item<BST->data) BST = BST->left;
46                    else BST = BST->right;
47                }
48        }
49        return false;
50    }
51   
52   
53    void insert(BTreeNode*& BST,const ElemType& item)
54    {
55        if(BST == NULL)
56            {
57                BTreeNode* p = new BTreeNode;
58                p->data = item;
59                p->left = p->right =NULL;
60                BST = p;
61            }else if(item<BST->data)
62            insert(BST->left,item);
63        else
64            insert(BST->right,item);
65    }
66   
67   
68    void insert1(BTreeNode*& BST,const ElemType& item)
69    {
70        BTreeNode* t = BST,*parent = NULL;
71        while (t != NULL)
72            {
73                parent = t;
74                if (item<t->data) t = t-> left;
75                else t = t-> right;
76            }
77        BTreeNode* p = new BTreeNode;
78        p->data = item;
79        p->left = p->right =NULL;
80   
81        if(parent == NULL) BST = p;
82        else if(item<parent->data) parent->left = p;
83        else parent->right = p;
84    }
85   
86    void CreateBSTree(BTreeNode*& BST,ElemType a[],int n)
87    {
88        //BST = NULL;insert return to here?X
89        for(int i = 0;i<n;++i)
90            insert(BST,a[i]);
91    }
92   
93    bool Delete(BTreeNode *& BST,const ElemType& item)
94    {
95        if(BST == NULL) return false;
96        if(item < BST->data) return Delete(BST->left,item);
97        if(item > BST->data) return Delete(BST->right,item);
98        BTreeNode* temp = BST;
99        if(BST->left == NULL){
100            BST = BST->right;
101            delete temp;
102            return true;
103        }else if (BST->right == NULL)
104            {
105                BST = BST->left;
106                delete temp;
107                return true;
108            }else{
109            if (BST->left->right == NULL)
110                {
111                    BST->data = BST->left->data;
112                    return Delete(BST->left,BST->left->data);
113                }
114            else
115                {
116                    BTreeNode* p1 = BST,*p2 = BST->left;
117                    while (p2->right != NULL)
118                        {
119                            p1 = p2;
120                            p2 = p2->right;
121                        }
122                    BST->data = p2->data;
123                    return Delete(p1->right,p2->data);
124                }
125        }
126    }
127   
128    bool EmptyBTree(BTreeNode* BT)
129    {
130        return BT == NULL;
131    }
132   
133    int depthbtree(BTreeNode * BT)
134    {
135        if(BT == NULL)
136            return 0;
137        else{
138            int dep1=depthbtree(BT->left);
139            int dep2=depthbtree(BT->right);
140            if (dep1>dep2)
141                {
142                    return dep1+1;
143                }
144            else
145                {
146                    return dep2+1;
147                }
148        }
149    }
150    void PrintBTree(BTreeNode * BT)
151    {
152   
153        if (!EmptyBTree(BT))
154            {
155                cout<<BT->data;
156                if (BT->left!=NULL||BT->right!=NULL)
157                    {
158                        cout<<'(';
159                        PrintBTree(BT->left);
160                        if(BT->right!=NULL)
161                            cout << ',';
162                        PrintBTree(BT->right);
163                        cout<<')';
164                    }
165            }
166    }
167   
168    void clearbtree(BTreeNode *&BT)
169    {
170        if (!EmptyBTree(BT))
171            {
172                clearbtree(BT->left);
173                clearbtree(BT->right);
174                delete BT;
175                BT = NULL;
176            }
177    }
178    void PreOrder(BTreeNode *BT)
179    {
180        if (!EmptyBTree(BT)){
181            cout<<BT->data<<' ';
182            PreOrder(BT->left);
183            PreOrder(BT->right);
184        }
185    }
186   
187    void InOrder(BTreeNode *BT)
188    {
189        if (!EmptyBTree(BT)){
190            InOrder(BT->left);
191            cout<<BT->data<<' ';
192            InOrder(BT->right);
193        }
194    }
195   
196    void PostOrder(BTreeNode *BT)
197    {
198        if (!EmptyBTree(BT)){
199            PostOrder(BT->left);
200            PostOrder(BT->right);
201            cout<<BT->data<<' ';   
202        }
203    }
204   
205    int main(int argc, char * argv[])
206    {
207        ElemType x;
208        BTreeNode* bst;
209   
210        InitBTree(bst);
211        ElemType a[10] = {30,50,20,40,25,70,54,23,80,92};
212   
213        CreateBSTree(bst,a,10);
214        PrintBTree(bst);
215        cout << endl;
216   
217        cout<<" Depth = "<<depthbtree(bst)<<endl;
218   
219        //    cout<<"PreOreder:";PreOrder(bst); cout<<endl;
220        cout<<" InOreder:";InOrder(bst); cout<<endl;
221        //    cout<<"PostOreder:";PostOrder(bst);cout<<endl;
222   
223        cout << " Input a item you want to find:";
224        cin>>x;
225   
226   
227        if (find(bst,x))
228            {
229                cout << " Find "<<x<<" Success!" << endl;
230            }else{
231            cout << " Find "<<x<<" Failure!" << endl;
232        }
233   
234        cout <<" Input a item you want to insert:";
235        cin>>x;
236        insert1(bst,x);
237   
238        cout << " After Insert:" << endl;
239        PrintBTree(bst);   
240        cout << endl;
241   
242        cout << " Input a item you want to delete:";
243        cin>>x;
244   
245        if (Delete(bst,x))
246            {
247                cout << " Delete "<<x<<" Success!" << endl;
248            }else{
249            cout << " Delete "<<x<<" Failure!" << endl;
250        }
251       
252        cout << " After Delete:" << endl;
253        PrintBTree(bst);
254        cout << endl;
255   
256        cout<<" InOreder:";InOrder(bst); cout<<endl;
257   
258        clearbtree(bst);
259        return 0;
260    }
----------------------------
$ ./a.out
30(20(,25(23)),50(40,70(54,80(,92))))
 Depth = 5
 InOreder:20 23 25 30 40 50 54 70 80 92
 Input a item you want to find:20
 Find 20 Success!
 Input a item you want to insert:21
 After Insert:
30(20(,25(23(21))),50(40,70(54,80(,92))))
 Input a item you want to delete:23
 Delete 23 Success!
 After Delete:
30(20(,25(21)),50(40,70(54,80(,92))))
 InOreder:20 21 25 30 40 50 54 70 80 92
----------------------------

原创粉丝点击