Odin Binary Search Tree (Console Only)

Warning!  This page is meant for technical reviewers only, hence, it is not interesting for anyone else.

The "Tree" class is available in the "console" and has the following:

  1. buildTree(array) function that takes an array of data (e.g., [1, 7, 4, 23, 8, 9, 4, 3, 5, 7, 9, 67, 6345, 324]) and turns it into a balanced binary tree full of Node objects appropriately placed. The buildTree function removes any duplicates and returns the level-0 root node.

  2. insert(value) and deleteItem(value) functions that insert/delete the given value.

  3. find(value) function that returns the node with the given value.

  4. levelOrder(callback) function that accepts an optional callback function as its parameter. levelOrder traverses the tree in breadth-first level order and provide each node as an argument to the callback. As a result, the callback will perform an operation on each node following the order in which they are traversed. The method returns an array of values if no callback is given as an argument.

  5. inOrder(callback), preOrder(callback), and postOrder(callback) functions that also accept an optional callback as a parameter. Each of these functions traverses the tree in their respective depth-first order and yield each node to the provided callback. The functions returns an array of values if no callback is given as an argument.

  6. height(node) function that returns the given node's height. Height is defined as the number of edges in the longest path from a given node to a leaf node.

  7. depth(node) function that returns the given node's depth. Depth is defined as the number of edges in the path from a given node to the tree's root node.

  8. isBalanced function that checks if the tree is balanced. A balanced tree is one where the difference between heights of the left subtree and the right subtree of every node is not more than 1.

  9. rebalance function that rebalances an unbalanced tree.