树结构完整C++代码实现

二叉树、二叉排序树的完整C++实现代码

Posted by DoraemonJack on September 6, 2025

树结构完整代码实现

本文提供树结构的完整C++实现代码,包括二叉树和二叉排序树的所有基本操作。

完整代码实现

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
#include <iostream>
#include <queue>
#include <stack>
#include <vector>
using namespace std;

// 二叉树节点定义
struct TreeNode {
    int val;
    TreeNode* left;
    TreeNode* right;
    TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
};

// 二叉树类
class BinaryTree {
private:
    TreeNode* root;
    
public:
    BinaryTree() : root(nullptr) {}
    
    // 插入节点(按层序插入)
    void insert(int val) {
        if (!root) {
            root = new TreeNode(val);
            return;
        }
        
        queue<TreeNode*> q;
        q.push(root);
        
        while (!q.empty()) {
            TreeNode* node = q.front();
            q.pop();
            
            if (!node->left) {
                node->left = new TreeNode(val);
                return;
            } else if (!node->right) {
                node->right = new TreeNode(val);
                return;
            } else {
                q.push(node->left);
                q.push(node->right);
            }
        }
    }
    
    // 前序遍历(递归)
    void preorder(TreeNode* node) {
        if (!node) return;
        cout << node->val << " ";
        preorder(node->left);
        preorder(node->right);
    }
    
    // 前序遍历(迭代)
    vector<int> preorderIterative(TreeNode* root) {
        vector<int> result;
        if (!root) return result;
        
        stack<TreeNode*> stk;
        stk.push(root);
        
        while (!stk.empty()) {
            TreeNode* node = stk.top();
            stk.pop();
            result.push_back(node->val);
            
            if (node->right) stk.push(node->right);
            if (node->left) stk.push(node->left);
        }
        
        return result;
    }
    
    // 中序遍历(递归)
    void inorder(TreeNode* node) {
        if (!node) return;
        inorder(node->left);
        cout << node->val << " ";
        inorder(node->right);
    }
    
    // 中序遍历(迭代)
    vector<int> inorderIterative(TreeNode* root) {
        vector<int> result;
        stack<TreeNode*> stk;
        TreeNode* curr = root;
        
        while (curr || !stk.empty()) {
            while (curr) {
                stk.push(curr);
                curr = curr->left;
            }
            curr = stk.top();
            stk.pop();
            result.push_back(curr->val);
            curr = curr->right;
        }
        
        return result;
    }
    
    // 后序遍历(递归)
    void postorder(TreeNode* node) {
        if (!node) return;
        postorder(node->left);
        postorder(node->right);
        cout << node->val << " ";
    }
    
    // 后序遍历(迭代)
    vector<int> postorderIterative(TreeNode* root) {
        vector<int> result;
        if (!root) return result;
        
        stack<TreeNode*> stk;
        TreeNode* lastVisited = nullptr;
        TreeNode* curr = root;
        
        while (curr || !stk.empty()) {
            if (curr) {
                stk.push(curr);
                curr = curr->left;
            } else {
                TreeNode* peekNode = stk.top();
                if (peekNode->right && lastVisited != peekNode->right) {
                    curr = peekNode->right;
                } else {
                    result.push_back(peekNode->val);
                    lastVisited = stk.top();
                    stk.pop();
                }
            }
        }
        
        return result;
    }
    
    // 层序遍历
    void levelOrder() {
        if (!root) return;
        queue<TreeNode*> q;
        q.push(root);
        
        while (!q.empty()) {
            TreeNode* node = q.front();
            q.pop();
            cout << node->val << " ";
            
            if (node->left) q.push(node->left);
            if (node->right) q.push(node->right);
        }
    }
    
    // 计算树的高度
    int height(TreeNode* root) {
        if (!root) return 0;
        return 1 + max(height(root->left), height(root->right));
    }
    
    // 计算节点总数
    int countNodes(TreeNode* root) {
        if (!root) return 0;
        return 1 + countNodes(root->left) + countNodes(root->right);
    }
    
    // 验证是否为平衡二叉树
    bool isBalanced(TreeNode* root) {
        return checkHeight(root) != -1;
    }
    
    int checkHeight(TreeNode* root) {
        if (!root) return 0;
        
        int leftHeight = checkHeight(root->left);
        if (leftHeight == -1) return -1;
        
        int rightHeight = checkHeight(root->right);
        if (rightHeight == -1) return -1;
        
        if (abs(leftHeight - rightHeight) > 1) return -1;
        
        return 1 + max(leftHeight, rightHeight);
    }
    
    TreeNode* getRoot() { return root; }
    
    ~BinaryTree() {
        destroyTree(root);
    }
    
private:
    void destroyTree(TreeNode* node) {
        if (node) {
            destroyTree(node->left);
            destroyTree(node->right);
            delete node;
        }
    }
};

// 二叉排序树类
class BinarySearchTree {
private:
    TreeNode* root;
    
public:
    BinarySearchTree() : root(nullptr) {}
    
    // 插入节点
    void insert(int val) {
        root = insert(root, val);
    }
    
    // 查找节点
    bool search(int val) {
        return search(root, val) != nullptr;
    }
    
    // 删除节点
    void deleteNode(int val) {
        root = deleteNode(root, val);
    }
    
    // 中序遍历(BST的中序遍历是有序的)
    void inorder() {
        inorder(root);
        cout << endl;
    }
    
    // 验证是否为有效的二叉搜索树
    bool isValidBST() {
        return isValidBST(root, nullptr, nullptr);
    }
    
    // 查找最小值
    int findMin() {
        TreeNode* minNode = findMin(root);
        return minNode ? minNode->val : -1;
    }
    
    // 查找最大值
    int findMax() {
        TreeNode* maxNode = findMax(root);
        return maxNode ? maxNode->val : -1;
    }
    
    TreeNode* getRoot() { return root; }
    
    ~BinarySearchTree() {
        destroyTree(root);
    }
    
private:
    TreeNode* insert(TreeNode* root, int val) {
        if (!root) {
            return new TreeNode(val);
        }
        
        if (val < root->val) {
            root->left = insert(root->left, val);
        } else if (val > root->val) {
            root->right = insert(root->right, val);
        }
        
        return root;
    }
    
    TreeNode* search(TreeNode* root, int val) {
        if (!root || root->val == val) {
            return root;
        }
        
        if (val < root->val) {
            return search(root->left, val);
        } else {
            return search(root->right, val);
        }
    }
    
    TreeNode* deleteNode(TreeNode* root, int val) {
        if (!root) return root;
        
        if (val < root->val) {
            root->left = deleteNode(root->left, val);
        } else if (val > root->val) {
            root->right = deleteNode(root->right, val);
        } else {
            if (!root->left) return root->right;
            if (!root->right) return root->left;
            
            TreeNode* minNode = findMin(root->right);
            root->val = minNode->val;
            root->right = deleteNode(root->right, minNode->val);
        }
        
        return root;
    }
    
    TreeNode* findMin(TreeNode* root) {
        while (root && root->left) {
            root = root->left;
        }
        return root;
    }
    
    TreeNode* findMax(TreeNode* root) {
        while (root && root->right) {
            root = root->right;
        }
        return root;
    }
    
    void inorder(TreeNode* root) {
        if (!root) return;
        inorder(root->left);
        cout << root->val << " ";
        inorder(root->right);
    }
    
    bool isValidBST(TreeNode* root, TreeNode* minNode, TreeNode* maxNode) {
        if (!root) return true;
        
        if ((minNode && root->val <= minNode->val) || 
            (maxNode && root->val >= maxNode->val)) {
            return false;
        }
        
        return isValidBST(root->left, minNode, root) && 
               isValidBST(root->right, root, maxNode);
    }
    
    void destroyTree(TreeNode* node) {
        if (node) {
            destroyTree(node->left);
            destroyTree(node->right);
            delete node;
        }
    }
};

// 测试函数
int main() {
    cout << "=== 二叉树测试 ===" << endl;
    BinaryTree bt;
    
    vector<int> values = {1, 2, 3, 4, 5, 6, 7};
    for (int val : values) {
        bt.insert(val);
    }
    
    cout << "前序遍历(递归): ";
    bt.preorder(bt.getRoot());
    cout << endl;
    
    cout << "前序遍历(迭代): ";
    vector<int> preorderResult = bt.preorderIterative(bt.getRoot());
    for (int val : preorderResult) {
        cout << val << " ";
    }
    cout << endl;
    
    cout << "中序遍历(递归): ";
    bt.inorder(bt.getRoot());
    cout << endl;
    
    cout << "中序遍历(迭代): ";
    vector<int> inorderResult = bt.inorderIterative(bt.getRoot());
    for (int val : inorderResult) {
        cout << val << " ";
    }
    cout << endl;
    
    cout << "后序遍历(递归): ";
    bt.postorder(bt.getRoot());
    cout << endl;
    
    cout << "后序遍历(迭代): ";
    vector<int> postorderResult = bt.postorderIterative(bt.getRoot());
    for (int val : postorderResult) {
        cout << val << " ";
    }
    cout << endl;
    
    cout << "层序遍历: ";
    bt.levelOrder();
    cout << endl;
    
    cout << "树的高度: " << bt.height(bt.getRoot()) << endl;
    cout << "节点总数: " << bt.countNodes(bt.getRoot()) << endl;
    cout << "是否平衡: " << (bt.isBalanced(bt.getRoot()) ? "是" : "否") << endl;
    
    cout << "\n=== 二叉排序树测试 ===" << endl;
    BinarySearchTree bst;
    
    vector<int> bstValues = {5, 3, 7, 2, 4, 6, 8, 1, 9};
    for (int val : bstValues) {
        bst.insert(val);
    }
    
    cout << "BST中序遍历: ";
    bst.inorder();
    
    cout << "查找节点4: " << (bst.search(4) ? "找到" : "未找到") << endl;
    cout << "查找节点9: " << (bst.search(9) ? "找到" : "未找到") << endl;
    cout << "查找节点10: " << (bst.search(10) ? "找到" : "未找到") << endl;
    
    cout << "最小值: " << bst.findMin() << endl;
    cout << "最大值: " << bst.findMax() << endl;
    
    cout << "是否为有效BST: " << (bst.isValidBST() ? "是" : "否") << endl;
    
    cout << "删除节点3后中序遍历: ";
    bst.deleteNode(3);
    bst.inorder();
    
    cout << "删除节点7后中序遍历: ";
    bst.deleteNode(7);
    bst.inorder();
    
    return 0;
}

代码说明

主要功能

  1. 二叉树类 (BinaryTree)
    • 层序插入节点
    • 四种遍历方式(前序、中序、后序、层序)
    • 递归和迭代两种实现方式
    • 计算树高度和节点总数
    • 验证平衡二叉树
  2. 二叉排序树类 (BinarySearchTree)
    • 插入、查找、删除操作
    • 查找最小值和最大值
    • 验证BST有效性
    • 中序遍历(结果有序)

编译和运行

1
2
g++ -o tree_structures tree_structures_complete_code.cpp
./tree_structures

预期输出

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
**二叉树测试**
前序遍历(递归): 1 2 4 5 3 6 7 
前序遍历(迭代): 1 2 4 5 3 6 7 
中序遍历(递归): 4 2 5 1 6 3 7 
中序遍历(迭代): 4 2 5 1 6 3 7 
后序遍历(递归): 4 5 2 6 7 3 1 
后序遍历(迭代): 4 5 2 6 7 3 1 
层序遍历: 1 2 3 4 5 6 7 
树的高度: 3
节点总数: 7
是否平衡: 是

**二叉排序树测试**
BST中序遍历: 1 2 3 4 5 6 7 8 9 
查找节点4: 找到
查找节点9: 找到
查找节点10: 未找到
最小值: 1
最大值: 9
是否为有效BST: 是
删除节点3后中序遍历: 1 2 4 5 6 7 8 9 
删除节点7后中序遍历: 1 2 4 5 6 8 9 

时间复杂度分析

操作 二叉树 二叉排序树
插入 O(n) O(log n)
查找 O(n) O(log n)
删除 O(n) O(log n)
遍历 O(n) O(n)

注:BST的时间复杂度在平衡情况下为O(log n),最坏情况为O(n)