Code is in Java In pictures I will provide full code, AVL Tree utilizes BinNode Class that will provide here and in image The AVL tree is an extension of a BinaryTree and uses its search method... but for some reason search method cannot find the data even though it should be in the tree (For AVL it does not work but for Binary Tree it does)... can you please provide help in fixing the SearchMethod to work with AVL Tree? //Binary Tree Search Method public BinNode search(String val) { return search(this.getRoot(), val); } private BinNode search(BinNode r, String val) { if (r == null) { return null; } int comparison = val.compareTo(r.getData()); if (comparison < 0) { return search(r.getLeft(), val); } else if (comparison > 0) { return search(r.getRight(), val); } else { return r; } } //Bin Node Class public class BinNode { private String data; private BinNode left; private BinNode right; private int height; private boolean color; private BinNode parent; public BinNode(){ data = ""; left = null; right = null; parent = null; color = false; // Default color is black } public boolean getColor() { return color; } public void setColor(boolean color) { this.color = color; } public BinNode getParent() { return parent; } public void setParent(BinNode parent) { this.parent = parent; } public int getHeight() { return height; } public void setHeight(int height) { this.height = height; } public BinNode(String d){ data = d; left = null; right = null; } public void setData(String d){ this.data = d; } public String getData(){ return this.data; } public void setLeft(BinNode l){ this.left = l; } public BinNode getLeft(){ return this.left; } public void setRight(BinNode r){ this.right = r; } public BinNode getRight(){ return this.right; } }   //AVL Tree Class public class AVLTree extends BinaryTree { private BinNode root; AVLTree() { root = null; } private int height(BinNode node) { return (node == null) ? 0 : node.getHeight(); } private int getBalance(BinNode node) { return (node == null) ? 0 : height(node.getLeft()) - height(node.getRight()); } private BinNode rightRotate(BinNode y) { BinNode x = y.getLeft(); BinNode T2 = x.getRight(); x.setRight(y); y.setLeft(T2); y.setHeight(Math.max(height(y.getLeft()), height(y.getRight())) + 1); x.setHeight(Math.max(height(x.getLeft()), height(x.getRight())) + 1); return x; } private BinNode leftRotate(BinNode x) { BinNode y = x.getRight(); BinNode T2 = y.getLeft(); y.setLeft(x); x.setRight(T2); x.setHeight(Math.max(height(x.getLeft()), height(x.getRight())) + 1); y.setHeight(Math.max(height(y.getLeft()), height(y.getRight())) + 1); return y; } @Override public void insert(String data) { root = insert(root, data); } private BinNode insert(BinNode node, String data) { if (node == null) { return new BinNode(data); } int cmp = data.compareTo(node.getData()); if (cmp < 0) { node.setLeft(insert(node.getLeft(), data)); } else if (cmp > 0) { node.setRight(insert(node.getRight(), data)); } else { // Duplicate values are not allowed in this AVL tree return node; } node.setHeight(1 + Math.max(height(node.getLeft()), height(node.getRight()))); int balance = getBalance(node); if (balance > 1) { if (data.compareTo(node.getLeft().getData()) < 0) { return rightRotate(node); } else { node.setLeft(leftRotate(node.getLeft())); return rightRotate(node); } } if (balance < -1) { if (data.compareTo(node.getRight().getData()) > 0) { return leftRotate(node); } else { node.setRight(rightRotate(node.getRight())); return leftRotate(node); } } return node; } @Override public void remove(String data) { root = remove(root, data); } private BinNode remove(BinNode node, String data) { if (node == null) { return node; } int cmp = data.compareTo(node.getData()); if (cmp < 0) { node.setLeft(remove(node.getLeft(), data)); } else if (cmp > 0) { node.setRight(remove(node.getRight(), data)); } else { // Node found, perform deletion if (node.getLeft() == null || node.getRight() == null) { BinNode temp = (node.getLeft() != null) ? node.getLeft() : node.getRight(); if (temp == null) { temp = node; node = null; } else { node = temp; } } else { BinNode temp = minValueNode(node.getRight()); node.setData(temp.getData()); node.setRight(remove(node.getRight(), temp.getData())); } } if (node == null) { return node; } node.setHeight(1 + Math.max(height(node.getLeft()), height(node.getRight())));

Programming Logic & Design Comprehensive
9th Edition
ISBN:9781337669405
Author:FARRELL
Publisher:FARRELL
Chapter6: Arrays
Section: Chapter Questions
Problem 14RQ
icon
Related questions
Question

Code is in Java

In pictures I will provide full code, AVL Tree utilizes BinNode Class that will provide here and in image

The AVL tree is an extension of a BinaryTree and uses its search method... but for some reason search method cannot find the data even though it should be in the tree (For AVL it does not work but for Binary Tree it does)... can you please provide help in fixing the SearchMethod to work with AVL Tree?

//Binary Tree Search Method

public BinNode search(String val)
{
return search(this.getRoot(), val);
}
private BinNode search(BinNode r, String val)
{

if (r == null) {
return null;
}

int comparison = val.compareTo(r.getData());

if (comparison < 0) {
return search(r.getLeft(), val);
} else if (comparison > 0) {
return search(r.getRight(), val);
} else {
return r;
}
}

//Bin Node Class

public class BinNode
{
private String data;
private BinNode left;
private BinNode right;

private int height;

private boolean color;

private BinNode parent;


public BinNode(){
data = "";
left = null;
right = null;
parent = null;
color = false; // Default color is black
}

public boolean getColor() {
return color;
}

public void setColor(boolean color) {
this.color = color;
}
public BinNode getParent() {
return parent;
}

public void setParent(BinNode parent) {
this.parent = parent;
}

public int getHeight() {
return height;
}

public void setHeight(int height) {
this.height = height;
}
public BinNode(String d){
data = d;
left = null;
right = null;
}

public void setData(String d){
this.data = d;
}
public String getData(){
return this.data;
}
public void setLeft(BinNode l){
this.left = l;
}
public BinNode getLeft(){
return this.left;
}
public void setRight(BinNode r){
this.right = r;
}
public BinNode getRight(){
return this.right;
}
}

 

//AVL Tree Class

public class AVLTree extends BinaryTree {

private BinNode root;

AVLTree() {
root = null;
}

private int height(BinNode node) {
return (node == null) ? 0 : node.getHeight();
}

private int getBalance(BinNode node) {
return (node == null) ? 0 : height(node.getLeft()) - height(node.getRight());
}

private BinNode rightRotate(BinNode y) {
BinNode x = y.getLeft();
BinNode T2 = x.getRight();

x.setRight(y);
y.setLeft(T2);

y.setHeight(Math.max(height(y.getLeft()), height(y.getRight())) + 1);
x.setHeight(Math.max(height(x.getLeft()), height(x.getRight())) + 1);

return x;
}

private BinNode leftRotate(BinNode x) {
BinNode y = x.getRight();
BinNode T2 = y.getLeft();

y.setLeft(x);
x.setRight(T2);

x.setHeight(Math.max(height(x.getLeft()), height(x.getRight())) + 1);
y.setHeight(Math.max(height(y.getLeft()), height(y.getRight())) + 1);

return y;
}

@Override
public void insert(String data) {
root = insert(root, data);
}

private BinNode insert(BinNode node, String data) {
if (node == null) {
return new BinNode(data);
}

int cmp = data.compareTo(node.getData());

if (cmp < 0) {
node.setLeft(insert(node.getLeft(), data));
} else if (cmp > 0) {
node.setRight(insert(node.getRight(), data));
} else {
// Duplicate values are not allowed in this AVL tree
return node;
}

node.setHeight(1 + Math.max(height(node.getLeft()), height(node.getRight())));

int balance = getBalance(node);

if (balance > 1) {
if (data.compareTo(node.getLeft().getData()) < 0) {
return rightRotate(node);
} else {
node.setLeft(leftRotate(node.getLeft()));
return rightRotate(node);
}
}

if (balance < -1) {
if (data.compareTo(node.getRight().getData()) > 0) {
return leftRotate(node);
} else {
node.setRight(rightRotate(node.getRight()));
return leftRotate(node);
}
}

return node;
}

@Override
public void remove(String data) {
root = remove(root, data);
}

private BinNode remove(BinNode node, String data) {
if (node == null) {
return node;
}

int cmp = data.compareTo(node.getData());

if (cmp < 0) {
node.setLeft(remove(node.getLeft(), data));
} else if (cmp > 0) {
node.setRight(remove(node.getRight(), data));
} else {
// Node found, perform deletion
if (node.getLeft() == null || node.getRight() == null) {
BinNode temp = (node.getLeft() != null) ? node.getLeft() : node.getRight();

if (temp == null) {
temp = node;
node = null;
} else {
node = temp;
}
} else {
BinNode temp = minValueNode(node.getRight());
node.setData(temp.getData());
node.setRight(remove(node.getRight(), temp.getData()));
}
}

if (node == null) {
return node;
}

node.setHeight(1 + Math.max(height(node.getLeft()), height(node.getRight())));

int balance = getBalance(node);

if (balance > 1) {
if (getBalance(node.getLeft()) >= 0) {
return rightRotate(node);
} else {
node.setLeft(leftRotate(node.getLeft()));
return rightRotate(node);
}
}

if (balance < -1) {
if (getBalance(node.getRight()) <= 0) {
return leftRotate(node);
} else {
node.setRight(rightRotate(node.getRight()));
return leftRotate(node);
}
}

return node;
}

private BinNode minValueNode(BinNode node) {
BinNode current = node;
while (current.getLeft() != null) {
current = current.getLeft();
}
return current;
}

public void display() {
display(root);
}

private void display(BinNode node) {
if (node != null) {
display(node.getLeft());
System.out.print(node.getData() + " ");
display(node.getRight());
}
}
}

8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
33
34
37
38
39
42
43
46
47
50
51
54
55
56
57
58
59
60
63
66
69
72
75
78
79
public class BinNode
{
A
+
+
+
+
+
+
A
A
+
+
+
+
B
4 usages
private String data;
4 usages
private BinNode left;
4 usages
private BinNode right;
}
2 usages
private int height; //height field as with AVL tree or Red Black Tree
3 usages
private boolean color; //Needed for Red Black Tree
3 usages
private BinNode parent; // Needed field for parents of nodes
3 usages
public BinNode () {
}
data = "";
left = null;
right = null;
parent = null;
color = false; // Default color is black
// Getter and setter methods for color
17 usages
public boolean getColor() { return color; }
31 usages
public void setColor (boolean color) { this.color = color; }
1 usage
public void setData(String d) { this.data = d; }
13 usages
B public String getData() { return this.data; }
// Getter and setter methods for parent
72 usages
public BinNode getParent() { return parent; }
public void setParent (BinNode parent) { this.parent = parent; }
public int getHeight() { return height; }
}
public void setHeight(int height) { this.height = height; }
2 usages
public BinNode (String d) {
data = d;
left = null;
right = null;
11 usages
public void setLeft (BinNode 1) { this.left = 1; }
43 usages
public BinNode getLeft() { return this.left; }
11 usages
public void setRight (BinNode r) { this.right = r; }
38 usages
public BinNode getRight() { return this.right; }
Transcribed Image Text:8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 33 34 37 38 39 42 43 46 47 50 51 54 55 56 57 58 59 60 63 66 69 72 75 78 79 public class BinNode { A + + + + + + A A + + + + B 4 usages private String data; 4 usages private BinNode left; 4 usages private BinNode right; } 2 usages private int height; //height field as with AVL tree or Red Black Tree 3 usages private boolean color; //Needed for Red Black Tree 3 usages private BinNode parent; // Needed field for parents of nodes 3 usages public BinNode () { } data = ""; left = null; right = null; parent = null; color = false; // Default color is black // Getter and setter methods for color 17 usages public boolean getColor() { return color; } 31 usages public void setColor (boolean color) { this.color = color; } 1 usage public void setData(String d) { this.data = d; } 13 usages B public String getData() { return this.data; } // Getter and setter methods for parent 72 usages public BinNode getParent() { return parent; } public void setParent (BinNode parent) { this.parent = parent; } public int getHeight() { return height; } } public void setHeight(int height) { this.height = height; } 2 usages public BinNode (String d) { data = d; left = null; right = null; 11 usages public void setLeft (BinNode 1) { this.left = 1; } 43 usages public BinNode getLeft() { return this.left; } 11 usages public void setRight (BinNode r) { this.right = r; } 38 usages public BinNode getRight() { return this.right; }
12 O
13
14
15
16
17
18
21
22
23
24
25
26
27
28
31
32
33
34
38
39
43
44 @
45
46
47
48
49
50
51
52
53
54
55
56 Ⓒ
57
58
59
60
61
62
63
67
68
69
70
71
72
73
74 Ⓒ
75
76
77
78
79
80
81
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
108
112
113
114
115
116
117
118
119
120
121
122
140
141
142
143
144
145
146
147
148
149
150
151
+
Q
163
164
165
A
B
H
196
197
A
F
A
A
A
B
B
@
A
+
A
105 Ą
106
107
Ą
public class BinaryTree
{
B
A
A
A
126
127
128
129 A
130
131
132
133 보
134
135
136
F
A
A
B
152 @
153
154
155
156
157 A
158
159
160
161
162
Ĥ
Ô }
166
167
168
169
170
171
172
173 A
174
A
175
176
177
B
A
A
A
178
179
180
181
182 B
183 B
184
185
186 Ĥ
187
188
189
190 A
191 보
192
193
194
195
A
A
15 usages
private BinNode root;
H {
A
A
3 usages
private BinNode curr;
/* Function to traverse right */
public void goRight() { this.curr = this.curr.getRight(); }
A
2 usages
public Binary Tree ()
{
198
199
200
201
A
202 Ą
203
204 Ĥ
205
206
207
208 보
209
210
211
212
}
A
1 usage 1 override
public BinNode getRoot() { return this.root; }
public void setRoot (BinNode r) {this.root = r;}
A
Ô }
}
/* Function to check if tree is empty */
public boolean isEmpty() { return this.root == null; }
/* Functions to insert data */
14 usages 2 overrides
public void insert(String data) { this.root = insert(this.root, data); } //done
/* Function to insert data recursively */
3 usages
private BinNode insert (BinNode node, String data)
{
Đ }
root = new BinNode ();
curr = new BinNode();
1 usage
# public void inorder() { inorder (this.root); }
if (node == null)
else
{
}
/* Function to count number of nodes */
public int countNodes () { return countNodes (this.root); }
/* Function to count number of nodes recursively */
3 usages
private int countNodes (BinNode r)
{
}
{
}
return node;
}
/* Function to search for an element */
node = new BinNode (data);
3 usages
public BinNode search (String val) { return search (this.root, val); }
/* Function to search for an element recursively */
3 usages
private BinNode search (BinNode r, String val)
{
if (r == null) {
return 0; // If the current node is null, there are no nodes to count.
A }
int i = node.getData().compareTo (data);
if (i < 0) {
int leftCount = countNodes (r.getLeft()); // Count nodes in the left subtree
int rightCount = countNodes (r.getRight()); // Count nodes in the right subtree
}
}
/* Function for postorder traversal */
# public void postorder() { postorder (root); }
node.setRight (insert(node.getRight(), data));
} else if (i >= 0) {
// Go left
node.setLeft (insert(node.getLeft(), data));
// Add 1 to count the current node itself
return 1+ leftCount + rightCount;
}
// write this!
// Go right
// Base case: If the current node is null, the value is not found.
if (r == null) {
return null;
}
int comparison = val.compareTo (r.getData());
if (comparison < 0) {
// Value is smaller, so search in the left subtree.
/* Function for inorder traversal */
} else if (comparison > 0) {
3 usages
private void inorder (BinNode r)
{
}
} else {
// Value is equal to the current node's data, so it's found.
//this.curr = r;
return r;
return search(r.getLeft(), val);
}
/* Function for preorder traversal */
public void preorder() { preorder (root); }
3 usages
private void preorder (BinNode r)
// Value is larger, so search in the right subtree.
return search(r.getRight(), val);
if (r != null)
{
}
3 usages
private void postorder (BinNode r)
if (r != null)
{
inorder (r.getLeft());
System.out.print (r.getData() +" ");
inorder (r.getRight());
}
if (r != null)
{
// find the in-order successor of a given node
1 usage
public BinNode findInorder Successor (BinNode node) {
BinNode current = node.getRight();
}
System.out.print(r.getData() +" ");
preorder (r.getLeft());
preorder (r.getRight());
while (current.getLeft() != null) {
current = current.getLeft();
}
postorder (r.getLeft());
postorder (r.getRight());
System.out.print(r.getData() +" ");
return current;
//Remove function
4 usages 2 overrides
public void remove (String key) {
// Find the node to be removed and its parent
BinNode parent = null;
BinNode n = this.root;
while (n != null && !n.getData().equals(key)) {
parent = n;
if (key.compare To (n.getData()) < 0) {
}
} else {
if (n == null) {
n = n.getLeft();
n = n.getRight();
// Key not found, nothing to remove
return;
// Case 1: Node with no children
if (n.getLeft() == null && n.getRight() == null) {
if (parent == null) {
}
// Removing the root node
this.root = null;
} else if (parent.getLeft() == n) {
parent.setLeft (null);
} else {
}
// Case 2: Node with one child
else
parent.setRight (null);
}
if (n.getLeft() == null || n.getRight() == null) {
BinNode child = (n.getLeft() != null) ? n.getLeft(): n.getRight();
if (n == this.root) {
this.root = child;
} else if (parent.getLeft() == n) {
parent.setLeft (child);
} else {
parent.setRight (child);
}
// Case 3: Node with two children
else {
BinNode successor = findInorder Successor (n);
n.setData(successor.getData());
remove (successor.getData()); // Recursively remove the successor
Transcribed Image Text:12 O 13 14 15 16 17 18 21 22 23 24 25 26 27 28 31 32 33 34 38 39 43 44 @ 45 46 47 48 49 50 51 52 53 54 55 56 Ⓒ 57 58 59 60 61 62 63 67 68 69 70 71 72 73 74 Ⓒ 75 76 77 78 79 80 81 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 108 112 113 114 115 116 117 118 119 120 121 122 140 141 142 143 144 145 146 147 148 149 150 151 + Q 163 164 165 A B H 196 197 A F A A A B B @ A + A 105 Ą 106 107 Ą public class BinaryTree { B A A A 126 127 128 129 A 130 131 132 133 보 134 135 136 F A A B 152 @ 153 154 155 156 157 A 158 159 160 161 162 Ĥ Ô } 166 167 168 169 170 171 172 173 A 174 A 175 176 177 B A A A 178 179 180 181 182 B 183 B 184 185 186 Ĥ 187 188 189 190 A 191 보 192 193 194 195 A A 15 usages private BinNode root; H { A A 3 usages private BinNode curr; /* Function to traverse right */ public void goRight() { this.curr = this.curr.getRight(); } A 2 usages public Binary Tree () { 198 199 200 201 A 202 Ą 203 204 Ĥ 205 206 207 208 보 209 210 211 212 } A 1 usage 1 override public BinNode getRoot() { return this.root; } public void setRoot (BinNode r) {this.root = r;} A Ô } } /* Function to check if tree is empty */ public boolean isEmpty() { return this.root == null; } /* Functions to insert data */ 14 usages 2 overrides public void insert(String data) { this.root = insert(this.root, data); } //done /* Function to insert data recursively */ 3 usages private BinNode insert (BinNode node, String data) { Đ } root = new BinNode (); curr = new BinNode(); 1 usage # public void inorder() { inorder (this.root); } if (node == null) else { } /* Function to count number of nodes */ public int countNodes () { return countNodes (this.root); } /* Function to count number of nodes recursively */ 3 usages private int countNodes (BinNode r) { } { } return node; } /* Function to search for an element */ node = new BinNode (data); 3 usages public BinNode search (String val) { return search (this.root, val); } /* Function to search for an element recursively */ 3 usages private BinNode search (BinNode r, String val) { if (r == null) { return 0; // If the current node is null, there are no nodes to count. A } int i = node.getData().compareTo (data); if (i < 0) { int leftCount = countNodes (r.getLeft()); // Count nodes in the left subtree int rightCount = countNodes (r.getRight()); // Count nodes in the right subtree } } /* Function for postorder traversal */ # public void postorder() { postorder (root); } node.setRight (insert(node.getRight(), data)); } else if (i >= 0) { // Go left node.setLeft (insert(node.getLeft(), data)); // Add 1 to count the current node itself return 1+ leftCount + rightCount; } // write this! // Go right // Base case: If the current node is null, the value is not found. if (r == null) { return null; } int comparison = val.compareTo (r.getData()); if (comparison < 0) { // Value is smaller, so search in the left subtree. /* Function for inorder traversal */ } else if (comparison > 0) { 3 usages private void inorder (BinNode r) { } } else { // Value is equal to the current node's data, so it's found. //this.curr = r; return r; return search(r.getLeft(), val); } /* Function for preorder traversal */ public void preorder() { preorder (root); } 3 usages private void preorder (BinNode r) // Value is larger, so search in the right subtree. return search(r.getRight(), val); if (r != null) { } 3 usages private void postorder (BinNode r) if (r != null) { inorder (r.getLeft()); System.out.print (r.getData() +" "); inorder (r.getRight()); } if (r != null) { // find the in-order successor of a given node 1 usage public BinNode findInorder Successor (BinNode node) { BinNode current = node.getRight(); } System.out.print(r.getData() +" "); preorder (r.getLeft()); preorder (r.getRight()); while (current.getLeft() != null) { current = current.getLeft(); } postorder (r.getLeft()); postorder (r.getRight()); System.out.print(r.getData() +" "); return current; //Remove function 4 usages 2 overrides public void remove (String key) { // Find the node to be removed and its parent BinNode parent = null; BinNode n = this.root; while (n != null && !n.getData().equals(key)) { parent = n; if (key.compare To (n.getData()) < 0) { } } else { if (n == null) { n = n.getLeft(); n = n.getRight(); // Key not found, nothing to remove return; // Case 1: Node with no children if (n.getLeft() == null && n.getRight() == null) { if (parent == null) { } // Removing the root node this.root = null; } else if (parent.getLeft() == n) { parent.setLeft (null); } else { } // Case 2: Node with one child else parent.setRight (null); } if (n.getLeft() == null || n.getRight() == null) { BinNode child = (n.getLeft() != null) ? n.getLeft(): n.getRight(); if (n == this.root) { this.root = child; } else if (parent.getLeft() == n) { parent.setLeft (child); } else { parent.setRight (child); } // Case 3: Node with two children else { BinNode successor = findInorder Successor (n); n.setData(successor.getData()); remove (successor.getData()); // Recursively remove the successor
Expert Solution
steps

Step by step

Solved in 4 steps with 1 images

Blurred answer
Knowledge Booster
Problems on Amortized Analysis
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
  • SEE MORE QUESTIONS
Recommended textbooks for you
Programming Logic & Design Comprehensive
Programming Logic & Design Comprehensive
Computer Science
ISBN:
9781337669405
Author:
FARRELL
Publisher:
Cengage
Systems Architecture
Systems Architecture
Computer Science
ISBN:
9781305080195
Author:
Stephen D. Burd
Publisher:
Cengage Learning