-1

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!

Bart Kiers
  • 166,582
  • 36
  • 299
  • 288
Hadi
  • 29
  • 7
  • I don't see what ANTLR has to do with this. I'll remove the tag, but if it has relevance, please edit the question to make that clear. Thanks. – Bart Kiers Feb 06 '18 at 22:24
  • How is your AVL tree implemented? An array or pointers? –  Feb 06 '18 at 22:25
  • @BartKiers i've put the tag for antlr experts to answer by "Y" or "NO". If you cannot or don't want to answer it, pleas leave it to someone else to look at it. – Hadi Feb 07 '18 at 01:23
  • @JabariDash I did it by doing a linked list, but i inherited the avl from a binary search tree that i implemented in javascript not java. your method is very smart, but as you said it is only for small amount of data. i actually did show the hight level of the nodes to prove the balancing and i don't know if this is what the professor want to see. my question might had been more specific because i was wondering and research for a any "graphical" tool to use that can show the avl tree balanced. thanks again! – Hadi Feb 07 '18 at 01:29
  • @Hadi Haha, I see. I'm not too sure about graphical representations. Good luck. I am curious though because I have wanted that too –  Feb 07 '18 at 01:33

1 Answers1

0

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;
}