Skip to content

Commit f31edd7

Browse files
authored
Merge pull request #458 from ishriayush/main
Add files via upload
2 parents 16f1843 + 0945dbc commit f31edd7

File tree

2 files changed

+304
-0
lines changed

2 files changed

+304
-0
lines changed

aa_tree.cpp

+238
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,238 @@
1+
2+
#include <iostream>
3+
#include <cstdlib>
4+
#include <cstring>
5+
#include <fstream>
6+
using namespace std;
7+
/*
8+
* Node Declaration
9+
*/
10+
struct node
11+
{
12+
int count, level;
13+
string key;
14+
node *right;
15+
node *left;
16+
node *parent;
17+
node *root;
18+
}*root;
19+
20+
/*
21+
* Class Declaration
22+
*/
23+
class AATree
24+
{
25+
public:
26+
int lookup(string &);
27+
void skew(node *);
28+
bool split(node *);
29+
void rebal(node *);
30+
node *insert(node *,node *);
31+
void print(node *);
32+
int countnode(node *);
33+
AATree()
34+
{
35+
root = NULL;
36+
}
37+
};
38+
39+
/*
40+
* Main: Contains Menu
41+
*/
42+
int main()
43+
{
44+
AATree at;
45+
int ch;
46+
string x;
47+
ifstream fin ("test.txt");
48+
while (1)
49+
{
50+
cout<<"\n---------------------"<<endl;
51+
cout<<"\nOperations on AA Tree"<<endl;
52+
cout<<"\n---------------------"<<endl;
53+
cout<<"1.Insert String into the Tree"<<endl;
54+
cout<<"2.Print Tree Data"<<endl;
55+
cout<<"3.Total Tree Nodes"<<endl;
56+
cout<<"4.Exit"<<endl;
57+
cout<<"Enter Your Choice: ";
58+
cin>>ch;
59+
switch (ch)
60+
{
61+
case 1:
62+
if (fin.is_open())
63+
{
64+
while (fin>>x)
65+
{
66+
at.lookup(x);
67+
}
68+
fin.close();
69+
}
70+
break;
71+
case 2:
72+
cout<<"Elemets of AA Tree"<<endl;
73+
at.print(root);
74+
break;
75+
case 3:
76+
cout<<"Total number of nodes"<<endl;
77+
cout<<at.countnode(root)<<endl;
78+
break;
79+
case 4:
80+
cout<<"Exiting"<<endl;
81+
exit(1);
82+
break;
83+
default:
84+
cout<<"Wrong Choice"<<endl;
85+
}
86+
}
87+
return 0;
88+
}
89+
/*
90+
* Insert String into the Tree
91+
*/
92+
int AATree::lookup(string &key)
93+
{
94+
node *temp = new node;
95+
temp->key = key;
96+
temp->level = 1;
97+
temp->count = 0;
98+
temp->left = NULL;
99+
temp->right = NULL;
100+
temp->parent = NULL;
101+
temp = insert(root, temp);
102+
return temp->count;
103+
}
104+
105+
/*
106+
* Skew Tree
107+
*/
108+
109+
void AATree::skew(node *temp)
110+
{
111+
node *ptr = temp->left;
112+
if (temp->parent->left == temp)
113+
temp->parent->left = ptr;
114+
else
115+
temp->parent->right = ptr;
116+
ptr->parent = temp->parent;
117+
temp->parent = ptr;
118+
temp->left = ptr->right;
119+
if (temp->left != NULL)
120+
temp->left->parent = temp;
121+
ptr->right = temp;
122+
temp->level = (temp->left ? temp->left->level + 1 : 1);
123+
}
124+
125+
/*
126+
* Splitting of AA Tree
127+
*/
128+
bool AATree::split(node *temp)
129+
{
130+
node* ptr = temp->right;
131+
if (ptr && ptr->right && (ptr->right->level == temp->level))
132+
{
133+
if (temp->parent->left == temp)
134+
temp->parent->left = ptr;
135+
else
136+
temp->parent->right = ptr;
137+
ptr->parent = temp->parent;
138+
temp->parent = ptr;
139+
temp->right = ptr->left;
140+
if (temp->right != NULL)
141+
temp->right->parent = temp;
142+
ptr->left = temp;
143+
ptr->level = temp->level + 1;
144+
return true;
145+
}
146+
return false;
147+
}
148+
149+
/*
150+
* Rebalancing of AA Tree
151+
*/
152+
void AATree::rebal(node* temp)
153+
{
154+
temp->left = NULL;
155+
temp->right = NULL;
156+
temp->level = 1;
157+
for (temp = temp->parent; temp != root; temp = temp->parent)
158+
{
159+
if (temp->level != (temp->left ? temp->left->level + 1 : 1 ))
160+
{
161+
skew(temp);
162+
if (temp->right == NULL)
163+
temp = temp->parent;
164+
else if (temp->level != temp->right->level)
165+
temp = temp->parent;
166+
}
167+
if (temp->parent != root)
168+
{
169+
if (split(temp->parent) == false)
170+
break;
171+
}
172+
}
173+
}
174+
175+
/*
176+
* Insert Function to insert string into the tree
177+
*/
178+
node* AATree::insert(node* temp, node* ins)
179+
{
180+
if (root == NULL)
181+
{
182+
ins->count = 1;
183+
ins->parent = NULL;
184+
ins->left = NULL;
185+
ins->right = NULL;
186+
root = ins;
187+
return root;
188+
}
189+
if (ins->key < temp->key)
190+
{
191+
if (temp->left)
192+
return insert(temp->left, ins);
193+
temp->left = ins;
194+
ins->parent = temp;
195+
ins->count = 1;
196+
rebal(ins);
197+
return ins;
198+
}
199+
if (ins->key > temp->key)
200+
{
201+
if (temp->right)
202+
return insert(temp->right, ins);
203+
temp->right = ins;
204+
ins->parent = temp;
205+
ins->count = 1;
206+
rebal(ins);
207+
return ins;
208+
}
209+
temp->count++;
210+
delete ins;
211+
return temp;
212+
}
213+
214+
/*
215+
* Display Tree Elements
216+
*/
217+
void AATree::print(node* temp)
218+
{
219+
if (!temp)
220+
return;
221+
print(temp->left);
222+
cout <<"Value: "<<temp->key << " Count:" << temp->count;
223+
cout<<" Level: "<<temp->level<<endl;
224+
print(temp->right);
225+
}
226+
227+
/*
228+
* Count number of nodes in AA Tree
229+
*/
230+
int AATree::countnode(node* temp)
231+
{
232+
if (!temp)
233+
return 0;
234+
int count = 1;
235+
count = count + countnode(temp->left);
236+
count = count + countnode(temp->right);
237+
return count;
238+
}

binary-space-partitioning.cpp

+66
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
// Language: cpp
2+
// Path: Tree/binary-space-partitioning.cpp
3+
// Implementation of the binary space partitioning tree.
4+
5+
#include <bits/stdc++.h>
6+
using namespace std;
7+
8+
// A node of the tree.
9+
struct Node {
10+
int x, y;
11+
Node *left, *right;
12+
Node(int x, int y) : x(x), y(y), left(NULL), right(NULL) {}
13+
};
14+
15+
// A comparator for sorting the points by x-coordinate.
16+
struct XComparator {
17+
bool operator()(const Node *a, const Node *b) {
18+
return a->x < b->x;
19+
}
20+
};
21+
22+
// A comparator for sorting the points by y-coordinate.
23+
struct YComparator {
24+
bool operator()(const Node *a, const Node *b) {
25+
return a->y < b->y;
26+
}
27+
};
28+
29+
// Builds a binary space partitioning tree for the given points.
30+
Node *build(vector<Node *> &points, int depth) {
31+
if (points.empty()) return NULL;
32+
if (points.size() == 1) return points[0];
33+
if (depth % 2 == 0) {
34+
sort(points.begin(), points.end(), XComparator());
35+
} else {
36+
sort(points.begin(), points.end(), YComparator());
37+
}
38+
int mid = points.size() / 2;
39+
Node *node = points[mid];
40+
vector<Node *> left(points.begin(), points.begin() + mid);
41+
vector<Node *> right(points.begin() + mid + 1, points.end());
42+
node->left = build(left, depth + 1);
43+
node->right = build(right, depth + 1);
44+
return node;
45+
}
46+
47+
// Prints the tree in the inorder traversal order.
48+
void print(Node *node) {
49+
if (node == NULL) return;
50+
print(node->left);
51+
printf("(%d, %d), ", node->x, node->y);
52+
print(node->right);
53+
}
54+
55+
int main() {
56+
vector<Node *> points;
57+
points.push_back(new Node(2, 3));
58+
points.push_back(new Node(5, 4));
59+
points.push_back(new Node(9, 6));
60+
points.push_back(new Node(4, 7));
61+
points.push_back(new Node(8, 1));
62+
points.push_back(new Node(4, 2));
63+
Node *root = build(points, 0);
64+
print(root);
65+
return 0;
66+
}

0 commit comments

Comments
 (0)