Skip to content

Commit 9638e9e

Browse files
authored
Create binary search tree 01-intro.cpp
1 parent 45f035c commit 9638e9e

File tree

1 file changed

+161
-0
lines changed

1 file changed

+161
-0
lines changed

binary-search-tree/01-intro.cpp

Lines changed: 161 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,161 @@
1+
//----------------------------------------
2+
// Binary search tree
3+
// Topics covered:
4+
// - tree creation, node insertion with and without recursion
5+
6+
#include <iostream>
7+
#include <vector>
8+
using namespace std;
9+
10+
struct Node
11+
{
12+
int data;
13+
Node* left;
14+
Node* right;
15+
};
16+
17+
Node* Create(int value)
18+
{
19+
return new Node{value, nullptr, nullptr};
20+
}
21+
22+
// recursive insert function for bst
23+
Node* rInsert(Node* root, int value)
24+
{
25+
if (root == nullptr)
26+
return Create(value);
27+
28+
if (value < root->data)
29+
{
30+
root->left = rInsert(root->left, value);
31+
}
32+
else if (value > root->data)
33+
{
34+
root->right = rInsert(root->right, value);
35+
}
36+
else
37+
{
38+
// ignore dupe values; an alternate solution is to have a ref count
39+
// inside every node and increment it when we get a dupe value
40+
}
41+
42+
return root;
43+
}
44+
45+
// wrapper function to create bst from a list of keys
46+
Node* rCreate(const vector<int> & input)
47+
{
48+
Node* root = nullptr;
49+
for (auto value : input)
50+
root = rInsert(root, value);
51+
52+
return root;
53+
}
54+
55+
// bst insert function without using recursion
56+
Node* Insert(Node* root, int value)
57+
{
58+
if (root == nullptr)
59+
return Create(value);
60+
61+
Node* current = root;
62+
while (true)
63+
{
64+
if (value == current->data)
65+
{
66+
// dupe value, ignore
67+
break;
68+
}
69+
70+
if (value < current->data)
71+
{
72+
if (current->left == nullptr)
73+
{
74+
current->left = Create(value);
75+
break;
76+
}
77+
else
78+
{
79+
current = current->left;
80+
}
81+
}
82+
else
83+
{
84+
if (current->right == nullptr)
85+
{
86+
current->right = Create(value);
87+
break;
88+
}
89+
else
90+
{
91+
current = current->right;
92+
}
93+
}
94+
}
95+
96+
return root;
97+
}
98+
99+
// wrapper function
100+
Node* Create(const vector<int> & input)
101+
{
102+
Node* root = nullptr;
103+
for (auto value : input)
104+
root = Insert(root, value);
105+
106+
return root;
107+
}
108+
109+
// for testing
110+
void Inorder(Node* root)
111+
{
112+
if (root)
113+
{
114+
Inorder(root->left);
115+
cout << root->data << " ";
116+
Inorder(root->right);
117+
}
118+
}
119+
120+
void testcase(const vector<int> & input)
121+
{
122+
{
123+
auto root = rCreate(input);
124+
cout << "Inorder traversal of bst created using recursion is ";
125+
Inorder(root);
126+
cout << endl;
127+
}
128+
129+
{
130+
auto root = Create(input);
131+
cout << "Inorder traversal of bst created *WITHOUT* recursion is ";
132+
Inorder(root);
133+
cout << endl;
134+
}
135+
}
136+
137+
int main(int argc, char *argv[])
138+
{
139+
{
140+
/*
141+
3
142+
/ \
143+
1 5
144+
\ /
145+
2 4
146+
147+
*/
148+
149+
const vector<int> input{3, 1, 2, 5, 4};
150+
testcase(input);
151+
}
152+
153+
return 0;
154+
}
155+
156+
/*
157+
Output:
158+
Inorder traversal of bst created using recursion is 1 2 3 4 5
159+
Inorder traversal of bst created *WITHOUT* recursion is 1 2 3 4 5
160+
161+
*/

0 commit comments

Comments
 (0)