what are the possible ways to print an AVL tree data? it is possible to do that with ANTLR to visualize the data in every node so that way i can also verify the balancing?
thanks!
what are the possible ways to print an AVL tree data? it is possible to do that with ANTLR to visualize the data in every node so that way i can also verify the balancing?
thanks!
I've done this in Java, not JavaScript. But, you just have to translate the syntax. The algorithm is the same. Let's say you used pointers and your Node class looked as follows:
// However this would be implemented in JavaScript
public class BinarySearchTreeNode<K, V> {
BinarySearchTreeNode left;
BinarySearchTreeNode right;
K key;
V value;
}
This is how I print a Tree:
// This function starts it off at the root
// and calls the real function
public String toTreeString() {
return this.toTreeString("", true, "", root);
}
// Keep concatenating the string
private String toTreeString(String prefix, boolean top, String tree, BinarySearchTreeNode<K, V> node) {
BinarySearchTreeNode<K, V> left;
BinarySearchTreeNode<K, V> right;
String temp;
// If we were passed an empty tree
if (node == null) {
return "";
}
// Get children nodes
left = node.leftChild;
right = node.rightChild;
// If the right child is not null
if (right != null) {
// Draw a path to the node
temp = path(top, "" + prefix, "│ ", " ");
// Recursively append to right subtree
tree = toTreeString(temp, false, tree, right);
}
// Draw a path to this node
tree = path(top, tree + prefix, "└──", "┌──");
// Append this node to the tree
tree += " " + node.key + "\n";
// If the left child is not null
if (left != null) {
// Draw a path to the node
temp = path(top, "" + prefix, " ", "│ ");
// Recursively append left sub tree
tree = toTreeString(temp, true, tree, left);
}
return tree;
}
//------------------------------------------------------------------------------
private String path(boolean condition, String s, String choice1, String choice2) {
if (condition) {
s += choice1;
} else {
s += choice2;
}
return s;
}
Once you have a tree build, you would pass
// Your implementation of AVL Tree
import structures.trees.AVLTree;
public final class Application {
public static void main(String[] args) {
AVLTree<Integer, Integer> tree = new AVLTree<>();
tree.insert(1, null);
tree.insert(4, null);
tree.insert(3, null);
tree.insert(0, null);
tree.insert(6, null);
tree.insert(9, null);
tree.insert(8, null);
tree.insert(3, null);
String treeString = tree.toTreeString();
System.out.println(treeString);
System.out.println("IsBalanced: " + tree.isBalanced());
}
}
Output:
│ ┌── 9
│ │ └── 8
│ ┌── 6
│ │ └── 4
└── 3
└── 1
└── 0
IsBalanced: true
Had you implemented the AVL Tree with an Array, the implementation is almost identical
However: This is only good for small trees. It's not easy to read for large trees. You should probably use an algorithm to check if a tree is balanced programmatically:
private int isBalanced(BinarySearchTreeNode<K, V> node) {
if (node == null)
return 0;
// Get heights of subtrees
int h1 = isBalanced(node.leftChild);
int h2 = isBalanced(node.rightChild);
if (h1 == -1 || h2 == -1)
return -1;
if (Math.abs(h1 - h2) > 1)
return -1;
if (h1 > h2)
return h1 + 1;
else
return h2 + 1;
}