Skip to content

Latest commit

 

History

History

Trees

Binary tree and Binary search tree

Challenge

Create a Node class that has properties for the value stored in the node, the left child node, and the right child node. Create a BinaryTree class Define a method for each of the depth first traversals called preOrder, inOrder, and postOrder which returns an array of the values, ordered appropriately. Any exceptions or errors that come from your code should be semantic, capturable errors. For example, rather than a default error thrown by your language, your code should raise/throw a custom, semantic error that describes what went wrong in calling the methods you wrote for this lab.

Create a BinarySearchTree class Define a method named add that accepts a value, and adds a new node with that value in the correct location in the binary search tree. Define a method named contains that accepts a value, and returns a boolean indicating whether or not the value is in the tree at least once.

Write a breadth first traversal method which takes a Binary Tree as its unique input. Without utilizing any of the built-in methods available to your language, traverse the input tree using a Breadth-first approach, and return a list of the values in the tree in the order they were encountered.

Approach & Efficiency

Using nodes with .value, .left, and .right properties I was able to implement the correct functionality easily. O(n) for time complexity, as the entire tree may need to be traversed in order to access contents.

The Binary Search tree has an o(log(n)) time complexity.

API

Binary Tree

preOrder() - returns pre ordered tree contents as array

inOrder() - returns in ordered tree contents as array

postOrder() - returns post ordered tree contents as array

findMaximumValue() - returns the maximum value stored in a tree containing Numbers

breadthFirst() - returns breadth-first ordered tree contents as array

Binary Search Tree

add(value) - adds given value as node correctly ordered in BST

contains(value) - returns boolean if given value is present at least once in BST

Solution

whiteboard

whiteboard