-
Notifications
You must be signed in to change notification settings - Fork 0
/
Lab5_TREE.cpp
151 lines (122 loc) · 2.72 KB
/
Lab5_TREE.cpp
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
#include <iostream>
#include <fstream>
using namespace std;
class Node
{
public:
int data;
Node* left = NULL;
Node* right = NULL;
};
class BinaryTree
{
private:
Node* root = NULL;
bool insert(Node* &root, int value) {
if (root == NULL) {
root = new Node();
root->data = value;
return true;
}
else if (root->data > value) {
return insert(root->left, value);
}
else return insert(root->right, value);
}
// TODO ID:445
bool find(Node* &root, int value){
if (root == NULL) return 0;
if (value < root->data) return find(root->left, value);
else if (value > root->data) return find(root->right, value);
else return root;
}
// TODO ID: 446
bool insert(Node* &root, int value) {
if (root == NULL) {
root = new Node();
root->data = value;
return true;
}
else if (root->data > value) return insert(root->left, value);
else return insert(root->right, value);
}
// TODO ID: 448
void inOrder(Node* &root) {
if (root != NULL) {
inOrder(root->left);
cout << root->data;
inOrder(root->right);
}
}
void insert(Node* &root, int* arr, int size, int index) {
if(index < size){
Node* p = new Node();
p->data = arr[index];
root = p;
insert(root->left,arr,size,2*index+1);
insert(root->right,arr,size,2*index+2);
}
}
//TODO ID: 449
int height(Node* &root) {
if(root == NULL) return 0;
if(root->left==NULL && root->right ==NULL) return 0;
int static h1 =0;
int static h2 =0;
h1 = height(root->left)+1;
h2 = height(root->right)+1;
h1>=h2?h1:h1=h2;
return h1;
}
//TODO ID: 450
bool find(Node* &root, int value) {
if (root == NULL) return 0;
if (value < root->data) return find(root->left, value);
else if (value > root->data) return find(root->right, value);
else return root;
}
int find_and_add(Node* &root, int value) {
int static sum = 0;
if (value < root->data) {
sum += root->data;
return find_and_add(root->left, value);
}
else if (value > root->data) {
sum += root->data;
return find_and_add(root->right, value);
}
int temp = sum + value;
sum = 0;
return temp;
}
public:
bool insert(int value)
{
return insert(root, value);
}
void insert(int* arr, int size) {
// TODO
insert(root,arr,size,0);
}
bool find(int value)
{
// TODO
return find(root,value);
}
void inOrder()
{
// TODO
inOrder(root);
}
void find_and_add(int value)
{
// TODO
if(find(root, value)==0) cout <<"Cannot find!" <<endl;
else cout << find_and_add(root, value) <<endl;
}
};
int main(int argc, char* argv[]) {
BinaryTree bt;
// bt.
return 0;
}