CIS 3223 Lab07 – Implementing Binary Search Trees in Matlab
-- This is Extra Credit Lab –
DUE: (before the next lab, 10:30am Friday, March 21, 2008)
Binary Search Trees (BST) in
Matlab
The natural implementation of
BST is using pointers. The challenge in this lab is to implement Binary Search
Trees using Matlab that does not support pointers. How
could we do it? One approach is to define array key that
stores key values of tree nodes. Index i in array key defines the position
of a node in the tree as follows: if node with index i is a parent node, its left
child has index 2i and its right
child has index 2i+1. Root node has
index i. key[i] is the key value of i-th node.
TASKS
- (20 p) Write the pseudocode suited for Matlab BST implementation of Traversal, Insert and Delete
operations.
- Write Matlab code that implements
- (10 p) Traversal
(input is BST, output the sorted list of keys)
- (15 p) Insert (input
is BST and the new number, output is updated BST)
- (20 p) Delete. (input is BST tree and the number to be deleted, output
is updated BST. NOTE: this is the
most difficult problem)
- (15 p) Generate a
sequence of 10 random numbers. Starting from the empty tree, insert the 10
numbers in the tree, one by one using your matlab
function. List the generated
numbers and plot the resulting
tree.
- Generate BST by hand.
- Generate the BST using
insert Matlab function.
- Compare if the resulting
BSTs – they should be the same
- (10 p) Use the
traversal function to list the 10 numbers in a sorted order. Check if the
result is correct. Report the
results.
- (10 p) Delete the
smallest, then the 5-th smallest, and then the largest elements from the
BST using delete routing. Report the results
- Delete the nodes from
the BST by hand.
- Delete the nodes using
delete Matlab function.
- Compare if the resulting
BSTs – they should be the same