C++实现二叉树基本操作详解

时间:2021-07-29 06:09:02

树是一种重要的非线性数据结构,二叉树是树型结构的一种重要类型。本学年论文介绍了二叉树的定义,二叉树的存储结构,二叉树的相关术语,以此引入二叉树这一概念,为展开二叉树的基本操作做好理论铺垫。二叉树的基本操作主要包含以下几个模块:二叉树的遍历方法,计算二叉树的结点个数,计算二叉树的叶子结点个数,二叉树深度的求解等内容。

前序遍历(递归&非递归)

  • 访问根节点
  • 前序访问左子树
  • 前序访问右子树
?
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
//前序非递归
  void PrevOrder()
  {
    stack<Node*> s;
    Node *cur = _root;
 
    while (cur || !s.empty())
    {
      while (cur)
      {
        cout << cur->_data << " ";
        s.push(cur);
        cur = cur->_left;
      }
      //此时当前节点的左子树已遍历完毕
      Node *tmp = s.top();
      s.pop();
      cur = tmp->_right;
    }
    cout << endl;
  }
 
  //前序递归
  void PrevOrderR()
  {
    _PrevOrder(_root);
 
    cout << endl;
  }
 
  void _PrevOrder(Node *root)
  {
    if (root == NULL) //必须有递归出口!!!
      return;
 
    cout << root->_data << " ";
    _PrevOrder(root->_left);
    _PrevOrder(root->_right);
  }

中序遍历(递归&非递归)

  • 中序访问左子树
  • 访问根节点
  • 中序访问右子树
?
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
//中序非递归
  void InOrder()
  {
    stack<Node*> s;
    Node *cur = _root;
 
    while (cur || !s.empty())
    {
      while (cur)
      {
        s.push(cur);
        cur = cur->_left;
      }
      //此时当前节点的左子树已遍历完毕
      Node *tmp = s.top();
      cout << tmp->_data << " ";
      s.pop();
      cur = tmp->_right;
    }
    cout << endl;
  }
 
  //中序递归
  void InOrderR()
  {
    _InOrder(_root);
 
    cout << endl;
  }
 
  void _InOrder(Node *root)
  {
    if (root == NULL)
      return;
 
    _InOrder(root->_left);
    cout << root->_data << " ";
    _InOrder(root->_right);
  }

后序遍历(递归&非递归)

?
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
//后序非递归
//后序遍历可能会出现死循环,所以要记录下前一个访问的节点
void PostOrder()
{
  stack<Node*> s;
  Node *cur = _root;
  Node *prev = NULL;
 
  while (cur || !s.empty())
  {
    while (cur)
    {
      s.push(cur);
      cur = cur->_left;
    }
    Node *tmp = s.top();
    if (tmp->_right && tmp->_right != prev)
    {
      cur = tmp->_right;
    }
    else
    {
      cout << tmp->_data << " ";
      prev = tmp;
      s.pop();
    }
  }
  cout << endl;
}
 
//后序递归
void PostOrderR()
{
  _PostOrder(_root);
 
  cout << endl;
}
 
void _PostOrder(Node *root)
{
  if (root == NULL)
    return;
 
  _PostOrder(root->_left);
  _PostOrder(root->_right);
  cout << root->_data << " ";
}

层序遍历

从根节点开始,依次访问每层结点。
利用队列先进先出的特性,把每层结点从左至右依次放入队列。

?
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
void LevelOrder() //利用队列!!!
 {
   queue<Node*> q;
   Node *front = NULL;
 
   //1.push根节点
   if (_root) 
   {
     q.push(_root);
   }
   //2.遍历当前节点,push当前节点的左右孩子,pop当前节点
   //3.遍历当前节点的左孩子,再遍历右孩子,循环直至队列为空
   while (!q.empty())
   {
 
     front = q.front();
     cout << front->_data << " ";
 
     if (front->_left)
       q.push(front->_left);
     if (front->_right)
       q.push(front->_right);
 
     q.pop();
   }
 
   cout << endl;
 }

求二叉树的高度

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
size_t Depth()
{
  return _Depth(_root);
}
 
size_t _Depth(Node *root)
{
  if (root == NULL)
    return 0;
  else if (root->_left == NULL && root->_right == NULL)
    return 1;
  else
  {
    size_t leftDepth = _Depth(root->_left) + 1;
    size_t rightDepth = _Depth(root->_right) + 1;
    return leftDepth > rightDepth ? leftDepth : rightDepth;
  }
}

求叶子节点的个数

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
size_t LeafSize()
  {
    return _LeafSize(_root);
  }
 
  size_t _LeafSize(Node *root)
  {
    if (root == NULL)
      return 0;
    else if (root->_left == NULL && root->_right == NULL)
      return 1;
    else
      return _LeafSize(root->_left) + _LeafSize(root->_right);
  }

求二叉树第k层的节点个数

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
size_t GetKLevel(int k)
  {
    return _GetKLevel(_root, k);
  }
 
  size_t _GetKLevel(Node *root, int k)
  {
    if (root == NULL)
      return 0;
    else if (k == 1)
      return 1;
    else
      return _GetKLevel(root->_left, k - 1) + _GetKLevel(root->_right, k - 1);
  }

完整代码如下:

?
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
template<class T>
struct BinaryTreeNode
{
  T _data;
  BinaryTreeNode *_left;
  BinaryTreeNode *_right;
 
  BinaryTreeNode(const T& d)
    :_data(d)
    , _left(NULL)
    , _right(NULL)
  {}
};
 
template<class T>
class BinaryTree
{
public:
  typedef BinaryTreeNode<T> Node;
 
  BinaryTree()
    :_root(NULL)
  {}
 
  BinaryTree(T *arr, size_t n, const T& invalid)
  {
    size_t index = 0;
    _root = _CreateBinaryTree(arr, n, invalid, index);
  }
 
  BinaryTree(const BinaryTree<T>& t)
    :_root(NULL)
  {
    _root = _CopyTree(t._root);
  }
 
  BinaryTree<T>& operator=(const BinaryTree<T>& t)
  {
    if (this != t)
    {
      Node *tmp = new Node(t._root);
      if (tmp != NULL)
      {
        delete _root;
        _root = tmp;
      }
    }
    return *this;
  }
 
  ~BinaryTree()
  {
    _DestroyTree(_root);
    cout << endl;
  }
 
  //前序非递归
  void PrevOrder()
  {
    stack<Node*> s;
    Node *cur = _root;
 
    while (cur || !s.empty())
    {
      while (cur)
      {
        cout << cur->_data << " ";
        s.push(cur);
        cur = cur->_left;
      }
      //此时当前节点的左子树已遍历完毕
      Node *tmp = s.top();
      s.pop();
      cur = tmp->_right;
    }
    cout << endl;
  }
 
  //前序递归
  void PrevOrderR()
  {
    _PrevOrder(_root);
 
    cout << endl;
  }
 
  //中序非递归
  void InOrder()
  {
    stack<Node*> s;
    Node *cur = _root;
 
    while (cur || !s.empty())
    {
      while (cur)
      {
        s.push(cur);
        cur = cur->_left;
      }
      //此时当前节点的左子树已遍历完毕
      Node *tmp = s.top();
      cout << tmp->_data << " ";
      s.pop();
      cur = tmp->_right;
    }
    cout << endl;
  }
 
  //中序递归
  void InOrderR()
  {
    _InOrder(_root);
 
    cout << endl;
  }
 
  //后序非递归
  //后序遍历可能会出现死循环,所以要记录下前一个访问的节点
  void PostOrder()
  {
    stack<Node*> s;
    Node *cur = _root;
    Node *prev = NULL;
 
    while (cur || !s.empty())
    {
      while (cur)
      {
        s.push(cur);
        cur = cur->_left;
      }
      Node *tmp = s.top();
      if (tmp->_right && tmp->_right != prev)
      {
        cur = tmp->_right;
      }
      else
      {
        cout << tmp->_data << " ";
        prev = tmp;
        s.pop();
      }
    }
    cout << endl;
  }
 
  //后序递归
  void PostOrderR()
  {
    _PostOrder(_root);
 
    cout << endl;
  }
 
  void LevelOrder() //利用队列!!!
  {
    queue<Node*> q;
    Node *front = NULL;
 
    //1.push根节点
    if (_root) 
    {
      q.push(_root);
    }
    //2.遍历当前节点,push当前节点的左右孩子,pop当前节点
    //3.遍历当前节点的左孩子,再遍历右孩子,循环直至队列为空
    while (!q.empty())
    {
 
      front = q.front();
      cout << front->_data << " ";
 
      if (front->_left)
        q.push(front->_left);
      if (front->_right)
        q.push(front->_right);
 
      q.pop();
    }
 
    cout << endl;
  }
 
  size_t Size()
  {
    return _Size(_root);
  }
 
  size_t LeafSize()
  {
    return _LeafSize(_root);
  }
 
  size_t GetKLevel(int k)
  {
    return _GetKLevel(_root, k);
  }
 
  size_t Depth()
  {
    return _Depth(_root);
  }
 
  Node* Find(const T& d)
  {
    return _Find(_root, d);
  }
 
protected:
  Node* _CreateBinaryTree(T *arr, size_t n, const T& invalid, size_t& index)
  {
    Node *root = NULL;
    if (index < n && arr[index] != invalid)
    {
      root = new Node(arr[index]);
      index++;
      root->_left = _CreateBinaryTree(arr, n, invalid, index);
      index++;
      root->_right = _CreateBinaryTree(arr, n, invalid, index);
    }
    return root;
  }
 
  Node* _CopyTree(Node *root)
  {
    Node *newRoot = NULL;
 
    if (root)
    {
      newRoot = new Node(root->_data);
      newRoot->_left = _CopyTree(root->_left);
      newRoot->_right = _CopyTree(root->_right);
    }
 
    return newRoot;
  }
 
  void _DestroyTree(Node *root)
  {
    if (root)
    {
      _Destroy(root->_left);
      _Destroy(root->_right);
      delete root;
    }
  }
 
  void _PrevOrder(Node *root)
  {
    if (root == NULL) //必须有递归出口!!!
      return;
 
    cout << root->_data << " ";
    _PrevOrder(root->_left);
    _PrevOrder(root->_right);
  }
 
  void _InOrder(Node *root)
  {
    if (root == NULL)
      return;
 
    _InOrder(root->_left);
    cout << root->_data << " ";
    _InOrder(root->_right);
  }
 
  void _PostOrder(Node *root)
  {
    if (root == NULL)
      return;
 
    _PostOrder(root->_left);
    _PostOrder(root->_right);
    cout << root->_data << " ";
  }
 
  size_t _Size(Node *root)
  {
    if (root == NULL)
      return 0;
    else
      return _Size(root->_left) + _Size(root->_right) + 1;
  }
 
  size_t _LeafSize(Node *root)
  {
    if (root == NULL)
      return 0;
    else if (root->_left == NULL && root->_right == NULL)
      return 1;
    else
      return _LeafSize(root->_left) + _LeafSize(root->_right);
  }
 
  size_t _GetKLevel(Node *root, int k)
  {
    if (root == NULL)
      return 0;
    else if (k == 1)
      return 1;
    else
      return _GetKLevel(root->_left, k - 1) + _GetKLevel(root->_right, k - 1);
  }
 
  size_t _Depth(Node *root)
  {
    if (root == NULL)
      return 0;
    else if (root->_left == NULL && root->_right == NULL)
      return 1;
    else
    {
      size_t leftDepth = _Depth(root->_left) + 1;
      size_t rightDepth = _Depth(root->_right) + 1;
      return leftDepth > rightDepth ? leftDepth : rightDepth;
    }
  }
 
  Node* _Find(Node *root, const T& d)
  {
    if (root == NULL)
      return NULL;
    else if (root->_data == d)
      return root;
    else if (Node *ret = _Find(root->_left, d))
      return ret;
    else
      _Find(root->_right, d);
  }
 
protected:
  Node *_root;
};

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。

原文链接:http://blog.csdn.net/tttjp/article/details/75948105