An exercise in building a balanced BST using Ruby.
This project has a Tree
class that can build binary search trees from an array of numbers. Tree
also has several methods for traversing and managing the tree:
#insert(value)
-- inserts a new given value into the tree.#delete(value)
-- deletes a given value from the tree.#find(value)
-- returns the node in the tree with the given value.#level_order
-- traverses the tree in breadth-first fashion#inorder
-- traverses the tree inorder#preorder
-- traverses the tree preorder#postorder
-- traverses the tree postorder#height(node)
-- returns the height of the given node (that is the number of edges in longest path from the node to a leaf node)#depth(target)
-- returns the depth of the target node (that is the number of edge in path from the node to the tree's root)#balanced?
-- returnstrue
if the tree is balanced#rebalance
-- rebalances a tree if it is unbalanced
In main.rb
there is a short script that tests most of the above methods using a random dataset.