BinaryTree.h
1
#ifndef BINARYTREE_H
2 #define BINARYTREE_H
3
4 #include < queue >
5 using namespace std;
6
7 const int MAX_NODES_SIZE = 100 ;
8
9 template < typename Type > class BinaryTree;
10
11 template < typename Type > class BinTreeNode
12 {
13 friend class BinaryTree < Type > ;
14 public :
15 BinTreeNode() : left(NULL), right(NULL) { }
16 BinTreeNode(Type val, BinTreeNode < Type > * L = NULL, BinTreeNode < Type > * R = NULL) : data(val), left(L), right(R) {}
17
18 Type getData() const { return data; }
19 BinTreeNode < Type > * getLeftChild() const { return left; }
20 BinTreeNode < Type > * getRightChild() const { return right; }
21 void setData( const Type & val) { data = val; }
22 void setLeftChild(BinTreeNode < Type > * L) { left = L; }
23 void setRightChild(BinTreeNode < Type > * R) { right = R; }
24 void visit(){ cout << getData() << endl;} // 访问的具体操作
25
26 private :
27 BinTreeNode < Type > * left, * right;
28 Type data;
29 };
30
31
32 template < typename Type > class BinaryTree
33 {
34 public :
35 BinaryTree() : root(NULL) {}
36 BinaryTree(Type arr[], int size); // 构造完全二叉树
37 virtual ~ BinaryTree() { destroy(root); }
38 virtual int isEmpty() { return root == NULL ? 1 : 0 ; }
39
40 // virtual BinTreeNode *parent(BinTreeNode *current);
41 virtual BinTreeNode < Type > * leftChild(BinTreeNode < Type > * current) { return root != NULL ? current -> getLeftChild() : NULL; }
42 virtual BinTreeNode < Type > * rightChild(BinTreeNode < Type > * current) { return root != NULL ? current -> getRightChild() : NULL; }
43 const BinTreeNode < Type > * getRoot() const { return root; }
44 // virtual int insert(const Type &val);
45 // virtual int find(const Type &val) const;
46 void preOrderTraverse() { preOrderTraverse(root); }
47 void inOrderTraverse() { inOrderTraverse(root); }
48 void postOrderTraverse() { postOrderTraverse(root); }
49 void levelOrderTraverse();
50 void preOrderTraverse(BinTreeNode < Type > * current);
51 void inOrderTraverse(BinTreeNode < Type > * current);
52 void postOrderTraverse(BinTreeNode < Type > * current);
53
54 private :
55 BinTreeNode < Type > * root;
56
57 void destroy(BinTreeNode < Type > * current);
58
59 };
60
61 template < typename Type >
62 BinaryTree < Type > ::BinaryTree(Type arr[], int size)
63 {
64 BinTreeNode < Type > * ptr[MAX_NODES_SIZE];
65
66 for ( int i = size; i >= 1 ; i -- )
67 {
68 ptr[i] = new BinTreeNode < Type > (arr[i - 1 ]);
69
70 if ( 2 * i <= size)
71 ptr[i] -> left = ptr[ 2 * i];
72
73 if ( 2 * i + 1 <= size)
74 ptr[i] -> right = ptr[ 2 * i + 1 ];
75 }
76
77 root = ptr[ 1 ];
78 }
79
80 template < typename Type >
81 void BinaryTree < Type > ::destroy(BinTreeNode < Type > * current)
82 {
83 if (current -> left == NULL && current -> right == NULL)
84 {
85 delete current;
86 return ;
87 }
88
89 if (current -> left != NULL)
90 destroy(current -> left);
91 if (current -> right != NULL)
92 destroy(current -> right);
93 }
94
95 template < typename Type >
96 void BinaryTree < Type > ::preOrderTraverse(BinTreeNode < Type > * current)
97 {
98 if (current != NULL)
99 {
100 current -> visit();
101 preOrderTraverse(current -> left);
102 preOrderTraverse(current -> right);
103 }
104
105 }
106
107 template < typename Type >
108 void BinaryTree < Type > ::inOrderTraverse(BinTreeNode < Type > * current)
109 {
110 if (current != NULL)
111 {
112 inOrderTraverse(current -> left);
113 current -> visit();
114 inOrderTraverse(current -> right);
115 }
116 }
117
118 template < typename Type >
119 void BinaryTree < Type > ::postOrderTraverse(BinTreeNode < Type > * current)
120 {
121 if (current != NULL)
122 {
123 postOrderTraverse(current -> left);
124 postOrderTraverse(current -> right);
125 current -> visit();
126 }
127 }
128
129 template < typename Type >
130 void BinaryTree < Type > ::levelOrderTraverse()
131 {
132 queue < BinTreeNode < Type > *> qu;
133 qu.push(root);
134 BinTreeNode < Type > * current;
135
136 while ( ! qu.empty())
137 {
138 current = qu.front();
139
140 if (current -> left != NULL)
141 qu.push(current -> left);
142 if (current -> right != NULL)
143 qu.push(current -> right);
144
145 current -> visit();
146 qu.pop();
147 }
148
149 }
150
151 #endif
2 #define BINARYTREE_H
3
4 #include < queue >
5 using namespace std;
6
7 const int MAX_NODES_SIZE = 100 ;
8
9 template < typename Type > class BinaryTree;
10
11 template < typename Type > class BinTreeNode
12 {
13 friend class BinaryTree < Type > ;
14 public :
15 BinTreeNode() : left(NULL), right(NULL) { }
16 BinTreeNode(Type val, BinTreeNode < Type > * L = NULL, BinTreeNode < Type > * R = NULL) : data(val), left(L), right(R) {}
17
18 Type getData() const { return data; }
19 BinTreeNode < Type > * getLeftChild() const { return left; }
20 BinTreeNode < Type > * getRightChild() const { return right; }
21 void setData( const Type & val) { data = val; }
22 void setLeftChild(BinTreeNode < Type > * L) { left = L; }
23 void setRightChild(BinTreeNode < Type > * R) { right = R; }
24 void visit(){ cout << getData() << endl;} // 访问的具体操作
25
26 private :
27 BinTreeNode < Type > * left, * right;
28 Type data;
29 };
30
31
32 template < typename Type > class BinaryTree
33 {
34 public :
35 BinaryTree() : root(NULL) {}
36 BinaryTree(Type arr[], int size); // 构造完全二叉树
37 virtual ~ BinaryTree() { destroy(root); }
38 virtual int isEmpty() { return root == NULL ? 1 : 0 ; }
39
40 // virtual BinTreeNode
41 virtual BinTreeNode < Type > * leftChild(BinTreeNode < Type > * current) { return root != NULL ? current -> getLeftChild() : NULL; }
42 virtual BinTreeNode < Type > * rightChild(BinTreeNode < Type > * current) { return root != NULL ? current -> getRightChild() : NULL; }
43 const BinTreeNode < Type > * getRoot() const { return root; }
44 // virtual int insert(const Type &val);
45 // virtual int find(const Type &val) const;
46 void preOrderTraverse() { preOrderTraverse(root); }
47 void inOrderTraverse() { inOrderTraverse(root); }
48 void postOrderTraverse() { postOrderTraverse(root); }
49 void levelOrderTraverse();
50 void preOrderTraverse(BinTreeNode < Type > * current);
51 void inOrderTraverse(BinTreeNode < Type > * current);
52 void postOrderTraverse(BinTreeNode < Type > * current);
53
54 private :
55 BinTreeNode < Type > * root;
56
57 void destroy(BinTreeNode < Type > * current);
58
59 };
60
61 template < typename Type >
62 BinaryTree < Type > ::BinaryTree(Type arr[], int size)
63 {
64 BinTreeNode < Type > * ptr[MAX_NODES_SIZE];
65
66 for ( int i = size; i >= 1 ; i -- )
67 {
68 ptr[i] = new BinTreeNode < Type > (arr[i - 1 ]);
69
70 if ( 2 * i <= size)
71 ptr[i] -> left = ptr[ 2 * i];
72
73 if ( 2 * i + 1 <= size)
74 ptr[i] -> right = ptr[ 2 * i + 1 ];
75 }
76
77 root = ptr[ 1 ];
78 }
79
80 template < typename Type >
81 void BinaryTree < Type > ::destroy(BinTreeNode < Type > * current)
82 {
83 if (current -> left == NULL && current -> right == NULL)
84 {
85 delete current;
86 return ;
87 }
88
89 if (current -> left != NULL)
90 destroy(current -> left);
91 if (current -> right != NULL)
92 destroy(current -> right);
93 }
94
95 template < typename Type >
96 void BinaryTree < Type > ::preOrderTraverse(BinTreeNode < Type > * current)
97 {
98 if (current != NULL)
99 {
100 current -> visit();
101 preOrderTraverse(current -> left);
102 preOrderTraverse(current -> right);
103 }
104
105 }
106
107 template < typename Type >
108 void BinaryTree < Type > ::inOrderTraverse(BinTreeNode < Type > * current)
109 {
110 if (current != NULL)
111 {
112 inOrderTraverse(current -> left);
113 current -> visit();
114 inOrderTraverse(current -> right);
115 }
116 }
117
118 template < typename Type >
119 void BinaryTree < Type > ::postOrderTraverse(BinTreeNode < Type > * current)
120 {
121 if (current != NULL)
122 {
123 postOrderTraverse(current -> left);
124 postOrderTraverse(current -> right);
125 current -> visit();
126 }
127 }
128
129 template < typename Type >
130 void BinaryTree < Type > ::levelOrderTraverse()
131 {
132 queue < BinTreeNode < Type > *> qu;
133 qu.push(root);
134 BinTreeNode < Type > * current;
135
136 while ( ! qu.empty())
137 {
138 current = qu.front();
139
140 if (current -> left != NULL)
141 qu.push(current -> left);
142 if (current -> right != NULL)
143 qu.push(current -> right);
144
145 current -> visit();
146 qu.pop();
147 }
148
149 }
150
151 #endif
main.cpp
1
#include
<
iostream
>
2 #include " BinaryTree.h "
3 using namespace std;
4
5 int main()
6 {
7 int arr[ 10 ] = { 25 , 32 , 8 , 4 , 7 , 34 , 63 , 44 , 35 , 84 };
8
9 BinaryTree < int > BTree(arr, 10 );
10
11 BTree.preOrderTraverse();
12 cout << " ======================== " << endl;
13 BTree.inOrderTraverse();
14 cout << " ======================== " << endl;
15 BTree.postOrderTraverse();
16 cout << " ======================== " << endl;
17 BTree.levelOrderTraverse();
18
19 // cout << BTree.getRoot()->getLeftChild()->getRightChild()->getLeftChild()->getData() << endl;
20
21 return 0 ;
22
23 }
2 #include " BinaryTree.h "
3 using namespace std;
4
5 int main()
6 {
7 int arr[ 10 ] = { 25 , 32 , 8 , 4 , 7 , 34 , 63 , 44 , 35 , 84 };
8
9 BinaryTree < int > BTree(arr, 10 );
10
11 BTree.preOrderTraverse();
12 cout << " ======================== " << endl;
13 BTree.inOrderTraverse();
14 cout << " ======================== " << endl;
15 BTree.postOrderTraverse();
16 cout << " ======================== " << endl;
17 BTree.levelOrderTraverse();
18
19 // cout << BTree.getRoot()->getLeftChild()->getRightChild()->getLeftChild()->getData() << endl;
20
21 return 0 ;
22
23 }