john
john
JCHJava Community | Help. Code. Learn.
Created by john on 11/19/2024 in #java-help
Error in remove Node method in Java
My code fails only in edge cases
7 replies
JCHJava Community | Help. Code. Learn.
Created by john on 11/19/2024 in #java-help
Error in remove Node method in Java
Hallo, believe in this method I have a error AVLNode y = (z.left != null && (z.right == null || z.left.height >= z.right.height)) ? z.left : z.right; AVLNode x = (y.left != null && (y.right == null || y.left.height >= y.right.height)) ? y.left : y.right; do this two lines correspond to: Put y on child of z with greatest height Put x on child of y with greatest height If both heights are the same, decide for the single-rotation case! Or I am doing it wrong? the method is responsible for removing a node in AVL tree, methods removeBST() do the remove in binary search tree, restructure does the linking and rotation, and updateHeight, just recusively updates the heights of each node Help would be appreciated The method works in most of the time, but in certain combinations of tree strucutre, gives an unbalanced tree as output This is an example
7 replies
JCHJava Community | Help. Code. Learn.
Created by john on 11/15/2024 in #java-help
AVL trees
this is the testcase I fail
195 replies
JCHJava Community | Help. Code. Learn.
Created by john on 11/15/2024 in #java-help
AVL trees
195 replies
JCHJava Community | Help. Code. Learn.
Created by john on 11/15/2024 in #java-help
AVL trees
but this happened to be a error if I do multiple operations
195 replies
JCHJava Community | Help. Code. Learn.
Created by john on 11/15/2024 in #java-help
AVL trees
No description
195 replies
JCHJava Community | Help. Code. Learn.
Created by john on 11/15/2024 in #java-help
AVL trees
I wrote this and I get the expected tree and not a false one
195 replies
JCHJava Community | Help. Code. Learn.
Created by john on 11/15/2024 in #java-help
AVL trees
public class Main {
public static void main(String[] args) {
AVLTree tree = new AVLTree();

// Insert nodes
int[] nodesToInsert = {12,5,14,3,9,13,17,0,4,11,19};
for (int key : nodesToInsert) {
tree.insertNode(key, 0); // Using 0 as the value for simplicity
}

int[] nodesToRemove = {14};
for (int key : nodesToRemove) {
tree.removeNode(key); // Using 0 as the value for simplicity
}


// Display the tree
StringBuilder treeDisplay = display(tree);
System.out.println(treeDisplay);
}

public static StringBuilder display(AVLTree tree) {
final int height = 5, width = 64;

int len = width * height * 2 + 2;
StringBuilder sb = new StringBuilder(len);

for (int i = 1; i <= len; i++)
sb.append(i < len - 2 && i % width == 0 ? "\n" : ' ');

displayR(sb, width / 2, 1, width / 4, width, tree.getTreeRoot(), " ");
return sb;
}

private static void displayR(StringBuilder sb, int c, int r, int d, int w, AVLNode n,
String edge) {
if (n != null) {
if (n != n.left)
displayR(sb, c - d, r + 2, d / 2, w, n.left, " /");

String s = String.valueOf(n.key);
int idx1 = r * w + c - (s.length() + 1) / 2;
int idx2 = idx1 + s.length();
int idx3 = idx1 - w;
if (idx2 < sb.length())
sb.replace(idx1, idx2, s).replace(idx3, idx3 + 2, edge);

if (n != n.right)
displayR(sb, c + d, r + 2, d / 2, w, n.right, "\\ ");
}
}
}
public class Main {
public static void main(String[] args) {
AVLTree tree = new AVLTree();

// Insert nodes
int[] nodesToInsert = {12,5,14,3,9,13,17,0,4,11,19};
for (int key : nodesToInsert) {
tree.insertNode(key, 0); // Using 0 as the value for simplicity
}

int[] nodesToRemove = {14};
for (int key : nodesToRemove) {
tree.removeNode(key); // Using 0 as the value for simplicity
}


// Display the tree
StringBuilder treeDisplay = display(tree);
System.out.println(treeDisplay);
}

public static StringBuilder display(AVLTree tree) {
final int height = 5, width = 64;

int len = width * height * 2 + 2;
StringBuilder sb = new StringBuilder(len);

for (int i = 1; i <= len; i++)
sb.append(i < len - 2 && i % width == 0 ? "\n" : ' ');

displayR(sb, width / 2, 1, width / 4, width, tree.getTreeRoot(), " ");
return sb;
}

private static void displayR(StringBuilder sb, int c, int r, int d, int w, AVLNode n,
String edge) {
if (n != null) {
if (n != n.left)
displayR(sb, c - d, r + 2, d / 2, w, n.left, " /");

String s = String.valueOf(n.key);
int idx1 = r * w + c - (s.length() + 1) / 2;
int idx2 = idx1 + s.length();
int idx3 = idx1 - w;
if (idx2 < sb.length())
sb.replace(idx1, idx2, s).replace(idx3, idx3 + 2, edge);

if (n != n.right)
displayR(sb, c + d, r + 2, d / 2, w, n.right, "\\ ");
}
}
}
195 replies
JCHJava Community | Help. Code. Learn.
Created by john on 11/15/2024 in #java-help
AVL trees
but what does this mean for us
195 replies
JCHJava Community | Help. Code. Learn.
Created by john on 11/15/2024 in #java-help
AVL trees
I think so
195 replies
JCHJava Community | Help. Code. Learn.
Created by john on 11/15/2024 in #java-help
AVL trees
No description
195 replies
JCHJava Community | Help. Code. Learn.
Created by john on 11/15/2024 in #java-help
AVL trees
Ok I am trying that
195 replies
JCHJava Community | Help. Code. Learn.
Created by john on 11/15/2024 in #java-help
AVL trees
yeah 14
195 replies
JCHJava Community | Help. Code. Learn.
Created by john on 11/15/2024 in #java-help
AVL trees
@dan1st sorry for the ping, but I'm hopeless. If you can check please. Its a remove error there and as you can see from the images, first the before tree, than the expected result and as a output the tree that happened. Sorry again. As a link is the actual code
195 replies
JCHJava Community | Help. Code. Learn.
Created by john on 11/15/2024 in #java-help
AVL trees
sorry this is my last problem here. If you can give it a look please. Thanks
195 replies
JCHJava Community | Help. Code. Learn.
Created by john on 11/15/2024 in #java-help
AVL trees
No description
195 replies
JCHJava Community | Help. Code. Learn.
Created by john on 11/15/2024 in #java-help
AVL trees
after many operation it happens a problem
195 replies
JCHJava Community | Help. Code. Learn.
Created by john on 11/15/2024 in #java-help
AVL trees
this is what I get
195 replies
JCHJava Community | Help. Code. Learn.
Created by john on 11/15/2024 in #java-help
AVL trees
No description
195 replies