-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Tree.h
111 lines (98 loc) · 2.11 KB
/
Tree.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
// This header file is under development
#include <queue>
class Node
{
public:
int data;
Node *left;
Node *right;
};
class Tree : public Node
{
Node *root = NULL;
void inOrder();
void preOrder(Node *root, std::vector<int>&);
void postOrder(Node *root, std::vector<int>&);
public:
Tree(int data)
{
this->root = new Node();
this->root->data = data;
this->root->left = NULL;
this->root->right = NULL;
}
~Tree();
void addNode(int data);
void getInOrderTraversal();
std::vector<int> getPreOrderTraversal();
std::vector<int> getPostOrderTraversal();
};
void Tree::inOrder()
{
if(this->root == NULL)
return
this->root->left->inOrder();
std::cout<<root->data<<" ";
this->root->left->inOrder();
}
/*
void Tree::preOrder(Node *root, std::vector<int> &process)
{
if(!root)
return
process.push_back(root->data);
inOrder(root->left, process);
inOrder(root->right, process);
}
void Tree::postOrder(Node *root, std::vector<int> &process)
{
if(!root)
return
inOrder(root->left, process);
inOrder(root->right, process);
process.push_back(root->data);
}
*/
void Tree::getInOrderTraversal()
{
if(this->root == NULL)
return;
inOrder(this->root);
}
void Tree::addNode(int data)
{
if(!(this->root))
{
this->root = new Node();
this->root->data = data;
this->root->left = NULL;
this->root->right = NULL;
return;
}
Node *new_node = new Node();
new_node->data = data;
new_node->left = NULL;
new_node->right = NULL;
std::queue<Node *> store;
store.push(this->root);
while(!store.empty())
{
Node *temp = store.front();
std::cout<<temp->data<<" ";
store.pop();
if (temp->left != NULL)
store.push(temp->left);
else
{
temp->left = new_node;
return;
}
if (temp->right != NULL)
store.push(temp->right);
else
{
temp->right = new_node;
return;
}
}
}