Skip to content

Commit 9cdbb4a

Browse files
committed
Binary Search Tree
Main.cpp takes two files that contain two binary trees and performs zigs and zags on one of the trees until it becomes the other tree.
0 parents  commit 9cdbb4a

File tree

12 files changed

+650
-0
lines changed

12 files changed

+650
-0
lines changed

BST.cpp

Lines changed: 140 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,140 @@
1+
//
2+
// bst.cpp
3+
//
4+
5+
#include "BST.h"
6+
7+
/* constructs an empty Binary Search Tree */
8+
BST::BST()
9+
{
10+
root = nullptr;
11+
}
12+
13+
/* constructs a one-node Binary Search Tree with the given key */
14+
BST::BST(int num)
15+
{
16+
root = new Node;
17+
root->key = num;
18+
root->left = nullptr;
19+
root->right = nullptr;
20+
}
21+
22+
/* constructs a Binary Search Tree by repeatedly inserting ints in <nums>
23+
in the order they appear in the array */
24+
BST::BST(vector<int>& nums)
25+
{
26+
root = new Node;
27+
root->key = nums[0];
28+
root->left = nullptr;
29+
root->right = nullptr;
30+
for (int i=1; i<nums.size(); i++)
31+
insert(nums[i]);
32+
}
33+
34+
/* insert num as a new key to the BST */
35+
void BST::insert(int num)
36+
{
37+
insert(num, root);
38+
}
39+
40+
41+
/* insert num as a new key to the BST at node 'leaf' */
42+
void BST::insert(int num, Node *leaf)
43+
{
44+
if (num > leaf->key) // insert to the right
45+
{
46+
if (leaf->right == nullptr)
47+
{
48+
leaf->right = new Node;
49+
leaf->right->key = num;
50+
leaf->right->left = nullptr;
51+
leaf->right->right = nullptr;
52+
}
53+
else
54+
insert(num, leaf->right);
55+
}
56+
else // insert to the left
57+
{
58+
if (leaf->left == nullptr)
59+
{
60+
leaf->left = new Node;
61+
leaf->left->key = num;
62+
leaf->left->left = nullptr;
63+
leaf->left->right = nullptr;
64+
}
65+
else
66+
insert(num, leaf->left);
67+
}
68+
}
69+
70+
/* return the BST rooted at this node as a human-readable string */
71+
string BST::print()
72+
{
73+
return print(root, 0);
74+
}
75+
76+
/* rturn the BST rooted at leaf as a human-readable string,
77+
indented by <depth> characters */
78+
string BST::print(Node *leaf, int depth)
79+
{
80+
string result = "";
81+
82+
result.append(to_string(leaf->key)); // output the key
83+
result.append("\n");
84+
85+
if (leaf->left != nullptr)
86+
{
87+
result.append(RepeatChar(depth));
88+
result.append("L");
89+
result.append(print(leaf->left, depth+1));
90+
}
91+
if (leaf->right != nullptr)
92+
{
93+
result.append(RepeatChar(depth));
94+
result.append("R");
95+
result.append(print(leaf->right, depth+1));
96+
}
97+
98+
return result;
99+
}
100+
101+
/* return a string of <depth> spaces */
102+
string BST::RepeatChar(int depth)
103+
{
104+
string result = "";
105+
106+
for (int i=0; i<depth; i++)
107+
result.append("-");
108+
109+
return result;
110+
}
111+
112+
113+
/**
114+
* Represents one rotation (ZIG or ZAG) around one key in a Binary Search Tree
115+
*/
116+
Rotation::Rotation(int num, RotationType rot)
117+
{
118+
myKey = num;
119+
myRot = rot;
120+
}
121+
122+
/* return A human-readable description of the rotation */
123+
string Rotation::print()
124+
{
125+
string result="";
126+
127+
switch (myRot)
128+
{
129+
case ZIG:
130+
result="ZIG";
131+
break;
132+
case ZAG:
133+
result="ZAG";
134+
break;
135+
}
136+
result.append(" on ");
137+
result.append(to_string(myKey));
138+
139+
return result;
140+
}

BST.h

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
//
2+
// BST.h
3+
//
4+
5+
#ifndef BST_h
6+
#define BST_h
7+
8+
#include <stdio.h>
9+
#include <iostream>
10+
#include <vector>
11+
#include <string>
12+
13+
using namespace std;
14+
15+
enum RotationType {ZIG, ZAG};
16+
17+
18+
/* A Binary Search Tree node */
19+
struct Node
20+
{
21+
int key; // the key stored by this node
22+
Node *left; // the left child of this node
23+
Node *right; // the right child of this node
24+
};
25+
26+
27+
/**
28+
* Implements a Binary Search Tree keyed by Integers
29+
*/
30+
class BST{
31+
public:
32+
/* constructs an empty Binary Search Tree */
33+
BST();
34+
35+
/* constructs a one-node Binary Search Tree with the given key */
36+
BST(int num);
37+
38+
/* constructs a Binary Search Tree by repeatedly inserting ints in <nums>
39+
in the order they appear in the array */
40+
BST(vector<int>& nums);
41+
42+
/* insert num as a new key to the BST */
43+
void insert(int num);
44+
45+
/* return the BST rooted at this node as a human-readable string */
46+
string print();
47+
48+
private:
49+
/* insert num as a new key to the BST at node 'leaf' */
50+
void insert(int num, Node *leaf);
51+
52+
53+
/* rturn the BST rooted at leaf as a human-readable string,
54+
indented by <depth> characters */
55+
string print(Node *leaf, int depth);
56+
57+
/* return a string of <depth> spaces */
58+
string RepeatChar(int depth);
59+
60+
public:
61+
Node *root;
62+
};
63+
64+
65+
/**
66+
* Represents one rotation (ZIG or ZAG) around one key in a Binary Search Tree
67+
*/
68+
class Rotation{
69+
public:
70+
/* constructs a rotation of type rot on key=num */
71+
Rotation(int num, RotationType rot);
72+
73+
/* return A human-readable description of the rotation */
74+
string print();
75+
76+
private:
77+
int myKey; // the key that is at the root of the rotation being performed
78+
RotationType myRot; // the type of rotation being performed
79+
};
80+
81+
82+
#endif /* BST_h */

0 commit comments

Comments
 (0)