-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathbinaryTree.cpp
More file actions
68 lines (66 loc) · 1.94 KB
/
binaryTree.cpp
File metadata and controls
68 lines (66 loc) · 1.94 KB
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
#include <iostream>
#include "BinaryTree.h"
using namespace std;
//将二叉树所有节点增加1
void increaseOne(int *p)
{
*p = *p + 1;
}
int main()
{
BinaryTree<int> intTree(1);
//插入
intTree.insert(2, 1, 1);
intTree.insert(3, 0, 1);
intTree.insert(4, 1, 2);
intTree.insert(8, 0, 2);
intTree.insert(5, 0, 3);
intTree.insert(6, 1, 3);
intTree.insert(10, 0, 8);
cout << "二叉树结构:\n";
intTree.print();
cout << "递归遍历:\n";
intTree.preOrderTraverse();
intTree.inOrderTraverse();
intTree.postOrderTraverse();
//非递归遍历
cout << "非递归遍历:\n";
intTree.iPreOrderTraverse();
intTree.iInOrderTraverse();
//后序遍历 将二叉树所有元素增加1
intTree.iPostOrderTraverse(increaseOne);
cout << "二叉树结构:\n";
intTree.print();
intTree.iPostOrderTraverse();
cout << "层次遍历:\n";
intTree.levelTraverse();
intTree.insert(100, 0, 11);
cout << "二叉树结构:\n";
intTree.print();
cout << "节点数 = " << intTree.countNodes() << endl;
cout << "叶子节点数 = " << intTree.countLeaves() << endl;
cout << "二叉树高度 = " << intTree.depth() << endl;
//由先序遍历数组构造二叉树 0 标志空节点
/*
10
/ \
20 30
/ \
40 50
/ \
60 70
/
80
\
90
*/
double array[] = {10, 20, 40, 0, 0, 50, 60, 0, 0, 70, 80, 0, 90, 0, 0, 0, 30, 0, 0};
int length = sizeof(array) / sizeof(double);
BinaryTree<double> doubleTree(array, length, 0);
doubleTree.print();
cout << "层次遍历:\n";
doubleTree.levelTraverse();
cout << "递归先序遍历:\n";
doubleTree.preOrderTraverse();
return 0;
}