-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBinaryTree.h
122 lines (110 loc) · 3.86 KB
/
BinaryTree.h
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
#ifndef BINARYTREE_H
#define BINARYTREE_H
#include "Node.h"
#include <queue>
#include <cstddef>
#include <cassert>
#include <iostream>
using namespace std;
template <class T>
void preorder(TNode<T> *position){
if (position != NULL){
cout << position -> getData() << endl;
preorder(position -> getLeft());
preorder(position -> getRight());
}
}
template <class T>
void inorder(TNode<T> *position){
if (position != NULL){
inorder(position -> getLeft());
cout << position -> getData() << endl;
inorder(position -> getRight());
}
}
template <class T>
void postorder(TNode<T> *position){
if (position != NULL){
postorder(position -> getLeft());
postorder(position -> getRight());
cout << position -> getData() << endl;
}
}
template <class T>
TNode<T> *copy(TNode<T> *ori){
TNode<T> *new_node = new TNode<T>;
if (ori == NULL) return NULL;
new_node -> addData(ori -> getData());
new_node -> addLeft(copy(ori -> getLeft())); // copy the data of left childs.
new_node -> addRight(copy(ori -> getRight())); // copy the data of right childs.
return new_node;
}
template <class T>
bool equal(TNode<T> *s, TNode<T> *t){
if (s==NULL && t==NULL) return true;
else if (s!=NULL && t!=NULL){
if (s->getData() == t->getData()){
if (equal(s->getLeft(), t->getLeft())) // check if left childs are equal.
return equal(s->getRight(), t->getRight()); // check if right childs are equal.
}
}
return false;
}
template <class T>
void remove(TNode<T> *position){
if (position == NULL) return;
remove(position -> getLeft());
remove(position -> getRight());
delete position; // delete node data.
}
template <class T>
class BinaryTree{
private:
TNode<T> *root;
int n; // the number of nodes.
public:
BinaryTree():root(NULL),n(0){}
bool IsEmpty(){ return root == NULL;}
TNode<T> *GetRoot(){ return root;}
int NodeNum(){ return n;}
void PreOrder(){ preorder(root);} // DLR
void InOrder(){ inorder(root);} // LDR
void PostOrder(){ postorder(root);} // LRD
void Copy(BinaryTree<T> BTree){ root = copy(BTree.GetRoot());} // copy one tree to another.
bool Equal(BinaryTree<T> BTree){ return equal(root, BTree.GetRoot());} // check if two binary tree are equal.
void RemoveTree(){ remove(root);} // free the node space.
void AddRoot(T data){
TNode<T> *new_node = new TNode<T>;
new_node -> addData(data);
root = new_node;
n = 1;
}
void AddChild(T data){
assert(!IsEmpty() && "The tree is empty!");
queue<TNode<T>*> q;
TNode<T> *new_node = new TNode<T>;
new_node -> addData(data);
TNode<T> *position = root;
while (new_node -> getPar() == NULL){ // if the new node is added to the tree.
if (position -> getLeft() == NULL){ // if the new node can add to left.
new_node -> addPar(position);
position -> addLeft(new_node);
continue;
}
q.push(position -> getLeft());
if (position -> getRight() == NULL){ // if the new node can add to right.
new_node -> addPar(position);
position -> addRight(new_node);
continue;
}
q.push(position -> getRight());
position = q.front();
q.pop();
}
n++;
}
BinaryTree operator=(const BinaryTree<T> &BTree){ RemoveTree(); Copy(BTree); return *this;}
bool operator==(const BinaryTree<T> &BTree){ return Equal(BTree);}
//~BinaryTree(){ RemoveTree(root);}
};
#endif