in this assignment you are asked to write a simple driver program and set of functions (maybein
a library) that can be performed on a binary search tree.
Your program should allow user to insert/delete integer values into the binary search tree along
with several other operations on the binary search tree. You can use the code given in slides. But
this time your key will be int! Specifically, your program will ask user to enter a command and
related parameters (if any) in a loop, and then perform the given commands. Here is the list of
commands that your program must implement:
* insert
*find\'
*delete
*list inorder
*list preorder
*list postorder
*list levelorder
* max
* min
* height
*count
* sum
*quit
As always, make sure you release (free) the dynamically allocated memories if you allocate any
memory in your programs. So, before submitting your program, run it with valgrind to see if
there is any memory leakage
//my proggram in C
struct tree_node {
int data;
struct tree_node *left, *right;
}
typedef struct nodeT {
int key;
struct nodeT *left, *right;
} nodeT, *treeT;
int main(){
while (TRUE) {
printf(\"> \");
line = GetLine();
ch = toupper(line[0]);
switch (ch) {
case \'I\': insert(); break;
case \'F\': find(); break;
case \'D\': delete(); break;
case \'LI\': listInorder; break;
case \'LPR\': listPreorder(); break;
case \'LPO\': listPostorder(); break;
case \'MAX\': max(); break;
case \'min\': min(); break;
case \'H\': height(); break;
case \'C\': count(); break;
case \'S\': sum(); break;
case \'Q\': exit(0);
default:printf(\"Illegal command\ \"); break;
}
}
}
nodeT *FindNode(nodeT *t, int key){
while(t !=NULL) {
if (key == t->key) return t;
if (key < t->key) {
t = t->left;
} else {
t = t->right;
}
return NULL;
}
void delete(nodeT **p){
nodeT
*target;
target=*p;
if (target->left==NULL && target->right==NULL) {
*p=NULL;
} else if (target->left == NULL) {
*p=target->right;
} else
if (target->right == NULL) {
*p=target->left;
} else {
/* target has two children, see next slide */
}
free(target);
}
void listInorder(nodeT *T){
if (t != NULL) {
DisplayTree(t->left);
printf(“%d “, t->key);
DisplayTree(t->right);
}
}
void listPreorder(nodeT *t) {
if (t != NULL) {
printf(“%d “, t->key);
DisplayTree(t->left);
DisplayTree(t->right);
}
}
void listPostOrder(nodeT *t){
if (t != NULL) {
DisplayTree(t->left);
DisplayTree(t->right);
printf(“%d “, t->key);
}
}
void intsert(nodeT **tptr, int key){
nodeT*t, *tmp;
t=*tptr;
if (t == NULL) {
tmp=New(nodeT*);
tmp->key = key;
tmp->left=tmp->right=NULL;
*tptr=tmp;
return;
}
if (key < t->key) {
InsertNode
(&t->left, key);
} else {
InsertNode(&t->right, key);
}
}
int height(nodeT *t){
if (t == NULL)
return 0;
else
return (1 + maximumof(
height(t->left),
height(t->right)) );
}
int sum(struct tree_node *p){
if (p == NULL)
return 0;
else
return (p->data +
sum(p->left) +
sum(p->right) );
}
Solution
1. /*
2. * Java Program to Implement Binary Search Tree
3. */
4.
5. import java.util.Scanner;
6.
7. /* Class BSTNode */
8. cl.
in this assignment you are asked to write a simple driver program an.pdf
1. in this assignment you are asked to write a simple driver program and set of functions (maybein
a library) that can be performed on a binary search tree.
Your program should allow user to insert/delete integer values into the binary search tree along
with several other operations on the binary search tree. You can use the code given in slides. But
this time your key will be int! Specifically, your program will ask user to enter a command and
related parameters (if any) in a loop, and then perform the given commands. Here is the list of
commands that your program must implement:
* insert
*find'
*delete
*list inorder
*list preorder
*list postorder
*list levelorder
* max
* min
* height
*count
* sum
*quit
As always, make sure you release (free) the dynamically allocated memories if you allocate any
memory in your programs. So, before submitting your program, run it with valgrind to see if
there is any memory leakage
//my proggram in C
struct tree_node {
int data;
struct tree_node *left, *right;
}
typedef struct nodeT {
int key;
struct nodeT *left, *right;
} nodeT, *treeT;
int main(){
while (TRUE) {
printf("> ");
2. line = GetLine();
ch = toupper(line[0]);
switch (ch) {
case 'I': insert(); break;
case 'F': find(); break;
case 'D': delete(); break;
case 'LI': listInorder; break;
case 'LPR': listPreorder(); break;
case 'LPO': listPostorder(); break;
case 'MAX': max(); break;
case 'min': min(); break;
case 'H': height(); break;
case 'C': count(); break;
case 'S': sum(); break;
case 'Q': exit(0);
default:printf("Illegal command "); break;
}
}
}
nodeT *FindNode(nodeT *t, int key){
while(t !=NULL) {
if (key == t->key) return t;
if (key < t->key) {
t = t->left;
} else {
t = t->right;
}
return NULL;
}
void delete(nodeT **p){
nodeT
*target;
target=*p;
3. if (target->left==NULL && target->right==NULL) {
*p=NULL;
} else if (target->left == NULL) {
*p=target->right;
} else
if (target->right == NULL) {
*p=target->left;
} else {
/* target has two children, see next slide */
}
free(target);
}
void listInorder(nodeT *T){
if (t != NULL) {
DisplayTree(t->left);
printf(“%d “, t->key);
DisplayTree(t->right);
}
}
void listPreorder(nodeT *t) {
if (t != NULL) {
printf(“%d “, t->key);
DisplayTree(t->left);
DisplayTree(t->right);
}
}
void listPostOrder(nodeT *t){
if (t != NULL) {
DisplayTree(t->left);
DisplayTree(t->right);
printf(“%d “, t->key);
}
}
void intsert(nodeT **tptr, int key){
nodeT*t, *tmp;
5. 2. * Java Program to Implement Binary Search Tree
3. */
4.
5. import java.util.Scanner;
6.
7. /* Class BSTNode */
8. class BSTNode
9. {
10. BSTNode left, right;
11. int data;
12.
13. /* Constructor */
14. public BSTNode()
15. {
16. left = null;
17. right = null;
18. data = 0;
19. }
20. /* Constructor */
21. public BSTNode(int n)
22. {
23. left = null;
24. right = null;
25. data = n;
26. }
27. /* Function to set left node */
28. public void setLeft(BSTNode n)
29. {
30. left = n;
31. }
32. /* Function to set right node */
33. public void setRight(BSTNode n)
34. {
35. right = n;
36. }
37. /* Function to get left node */
6. 38. public BSTNode getLeft()
39. {
40. return left;
41. }
42. /* Function to get right node */
43. public BSTNode getRight()
44. {
45. return right;
46. }
47. /* Function to set data to node */
48. public void setData(int d)
49. {
50. data = d;
51. }
52. /* Function to get data from node */
53. public int getData()
54. {
55. return data;
56. }
57. }
58.
59. /* Class BST */
60. class BST
61. {
62. private BSTNode root;
63.
64. /* Constructor */
65. public BST()
66. {
67. root = null;
68. }
69. /* Function to check if tree is empty */
70. public boolean isEmpty()
71. {
72. return root == null;
73. }
7. 74. /* Functions to insert data */
75. public void insert(int data)
76. {
77. root = insert(root, data);
78. }
79. /* Function to insert data recursively */
80. private BSTNode insert(BSTNode node, int data)
81. {
82. if (node == null)
83. node = new BSTNode(data);
84. else
85. {
86. if (data <= node.getData())
87. node.left = insert(node.left, data);
88. else
89. node.right = insert(node.right, data);
90. }
91. return node;
92. }
93. /* Functions to delete data */
94. public void delete(int k)
95. {
96. if (isEmpty())
97. System.out.println("Tree Empty");
98. else if (search(k) == false)
99. System.out.println("Sorry "+ k +" is not present");
100. else
101. {
102. root = delete(root, k);
103. System.out.println(k+ " deleted from the tree");
104. }
105. }
106. private BSTNode delete(BSTNode root, int k)
107. {
108. BSTNode p, p2, n;
109. if (root.getData() == k)
8. 110. {
111. BSTNode lt, rt;
112. lt = root.getLeft();
113. rt = root.getRight();
114. if (lt == null && rt == null)
115. return null;
116. else if (lt == null)
117. {
118. p = rt;
119. return p;
120. }
121. else if (rt == null)
122. {
123. p = lt;
124. return p;
125. }
126. else
127. {
128. p2 = rt;
129. p = rt;
130. while (p.getLeft() != null)
131. p = p.getLeft();
132. p.setLeft(lt);
133. return p2;
134. }
135. }
136. if (k < root.getData())
137. {
138. n = delete(root.getLeft(), k);
139. root.setLeft(n);
140. }
141. else
142. {
143. n = delete(root.getRight(), k);
144. root.setRight(n);
145. }
9. 146. return root;
147. }
148. /* Functions to count number of nodes */
149. public int countNodes()
150. {
151. return countNodes(root);
152. }
153. /* Function to count number of nodes recursively */
154. private int countNodes(BSTNode r)
155. {
156. if (r == null)
157. return 0;
158. else
159. {
160. int l = 1;
161. l += countNodes(r.getLeft());
162. l += countNodes(r.getRight());
163. return l;
164. }
165. }
166. /* Functions to search for an element */
167. public boolean search(int val)
168. {
169. return search(root, val);
170. }
171. /* Function to search for an element recursively */
172. private boolean search(BSTNode r, int val)
173. {
174. boolean found = false;
175. while ((r != null) && !found)
176. {
177. int rval = r.getData();
178. if (val < rval)
179. r = r.getLeft();
180. else if (val > rval)
181. r = r.getRight();