Data Structures

PA 07

Adding functionality to a BST


Due by Friday, November 13th at the start of class


The introduction

In session 32 and  session 33 we looked at the creation of a binary search tree (BST) that fits a "standard" ADT for a BST.  In this particular assignment you will look at adding some less standard but still "often used" methods to our BST.  Several of these methods have meaning only in a BST while others are valid in any Binary Tree. 


Your Assignment

Begin by downloading the BST code from session 33

Add the following methods to the BST

Method Description
height() This method takes in no parameters (beyond the self parameter).  It returns an integer which indicates the depth of the deepest leaf.
depthOf() This method takes in one parameter (beyond the self parameter) which is the value of an item that may or may not be in the tree.  The method returns the level number of the first occurrence of the item if it exists in the tree and None if it does not.
leaves() This method takes in no parameters and returns a list of all of the leaves in the tree.
interiorNodes() This method takes in no parameters and returns a list of all of the interior nodes in the tree.  (Notice that there is one special case where this list will return an empty list).
predecessor() This method takes in one parameter which may or may not actually exist in the tree.  The predecessor to this parameter value is the largest item in the tree that is less than the parameter value or None if no value in the tree fits that description.  Note that the predecessor exists even if the parameter value does not.
successor() This method takes in one parameter which may or may not actually exist in the tree.  The successor to this parameter value is the smallest item in the tree that is larger than the parameter value or None if no value in the tree fits that description.  Note that the successor exists even if the parameter value does not.

Hint(s)

In the introduction to this assignment I mentioned that some of these methods are unique to a BST while others could exist in any binary tree.  I strongly suggest that for each method above you yourself which category it falls in.  It turns out that how you solve the problem is strongly related to that question/issue.


Testing your code:

Consider this tree:

You can build this tree by using the following code:

values = [50, 30, 60, 9, 34, 58, 80, 18, 32, 47, 53, 55]
testTree = BST()
for value in values:
        testTree.add(value)
For this tree, the following are some expected outcomes
Method result
testTree.height() 4
testTree.depthOf(9)

testTree.depthOf(50)

testTree.depthOf(70)

2

0

None

testTree.leaves() [18,32,47,55,80]
testTree.interiorNodes() [9,30,34,50,53,58,60]
testTree.predecessor(9)

testTree.predecessor(50)

testTree.predecessor(70)

None

47

60

testTree.successor(9)

testTree.successor(50)

testTree.successor(70)

18

53

80


Submitting your assignment

To get full credit for this assignment you must: