-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathBinarySearchTree.cpp
More file actions
174 lines (145 loc) · 3.37 KB
/
BinarySearchTree.cpp
File metadata and controls
174 lines (145 loc) · 3.37 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
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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
#include <iostream>
using namespace std;
class Node{
public:
Node* left;
Node* right;
int data;
Node* prev;
Node(){}
Node(int data,Node* prev){this->left=NULL; this->right=NULL; this->data=data; this->prev=prev;}
};
class BinarySearchTree{
Node* root;
public:
BinarySearchTree(int value){this->root=new Node(value,nullptr);};
void PreOrder(Node* root);
void InOrder();
void InOrder(Node* root);
void PostOrder(Node* root);
void PostOrder();
int height(Node* node);
void ancestors(int key);
int WhatLevelAmI(Node* node, int key,int count);
void PreOrder();
int height();
Node* BinarySearch(Node* node, int key);
void Insertion(int value);
int WhatLevelAmI(int key);
Node* Insertion(Node* root, int value);
};
Node* BinarySearchTree::Insertion(Node* root, int value){
if (root == nullptr) {
root = new Node(value,nullptr);
return root;
}
if(value <= root->data){
root->left = (root->left==nullptr) ? new Node(value,root) : Insertion(root->left, value);
}
else{
root->right = (root->right==nullptr) ? new Node(value,root) : Insertion(root->right, value);
}
return root;
}
void BinarySearchTree::Insertion(int value){
Insertion(this->root,value);
}
void BinarySearchTree::PreOrder(Node* root){
if (root==NULL){
return;
}
cout<<root->data<<" ";
PreOrder(root->left);
PreOrder(root->right);
}
void BinarySearchTree::PreOrder(){
PreOrder(this->root);
}
void BinarySearchTree::InOrder(Node* root){
if (root==nullptr){
return;
}
InOrder(root->left);
cout<<root->data<<" ";
InOrder(root->right);
}
void BinarySearchTree::InOrder(){
InOrder(this->root);
}
void BinarySearchTree::PostOrder(Node* root){
if (root == NULL){
return;
}
PostOrder(root->left);
PostOrder(root->right);
cout << root->data << " ";
}
void BinarySearchTree::PostOrder(){
PostOrder(this->root);
}
int BinarySearchTree::height(Node* node){
if (node==nullptr){
return 0;
}
return 1+max(height(node->left),height(node->right));
}
int BinarySearchTree::height(){
return height(this->root);
}
void BinarySearchTree::ancestors(int key){
cout<<endl<<"Los valores de ancestros son (desde actual->Raiz):";
Node* temp=BinarySearch(this->root,key);
while(temp!=nullptr){
cout<<endl<<temp->data;
temp=temp->prev;
}
}
Node* BinarySearchTree::BinarySearch(Node* node, int key){
if(node==nullptr){
cout<<"No se encontro dicho elemento: "<<endl;
return NULL;
}
if(key==node->data){
return node;
}
if(key>node->data){
BinarySearch(node->right,key);
}
else{
BinarySearch(node->left,key);
};
}
int BinarySearchTree::WhatLevelAmI(Node* node,int key,int count){
if(node==nullptr){
return -1;
}
if(key==node->data){
return count;
}
if(key>node->data){
WhatLevelAmI(node->right,key,++count);
}
else{
WhatLevelAmI(node->left,key,++count);
};
}
int BinarySearchTree::WhatLevelAmI(int key){
return WhatLevelAmI(this->root,key,1);
}
int main(){
BinarySearchTree arbol(1);
arbol.Insertion(58);
arbol.Insertion(88);
arbol.Insertion(45);
arbol.Insertion(17);
arbol.Insertion(7);
arbol.Insertion(3);
arbol.Insertion(51);
arbol.Insertion(63);
arbol.Insertion(46);
arbol.Insertion(1);
arbol.PreOrder();
arbol.WhatLevelAmI(5);
cout<<"La altura es:"<< arbol.WhatLevelAmI(3);
arbol.ancestors(17);
}