Data Structures and Algorithms -- CSci 230
Chapter 4 -- Trees

Overview


Basics


Exercises on the Basics


Here are two exercises exploring properties of rooted trees.

1.
A full m-ary tree is a tree where each internal node has exactly m children. One interesting property of full m-ary trees is that you can write a formula relating the number of internal nodes i, the number of leaf nodes l, and the total number of nodes n (obviously, n = i + l). If a m-ary tree has i internal nodes, how many leaf nodes does it have? (Hint: start from a tree having just a root node (it is a leaf node), then successively change leaf nodes into internal nodes and determine how i and l change.) Prove your answer by mathematical induction on i, the number of internal nodes in the full m-ary tree.
2.
Sketch an algorithm to calculate the height of a node in an m-ary tree. What is the time complexity of your algorithm? What would the time complexity be for an algorithm computing the height of all nodes in the tree?

Binary Search Trees


Exercises on Binary Search Trees


AVL trees


AVL Tree Exercises


1.
Starting from an empty tree, show the structure of the tree after each of the following is inserted: 30, 50, 70, 80, 90, 15, 85, 97, 82. Draw the tree after each insert (before rebalancing) and after rebalancing. Write the heights of the subtrees if it helps.
2.
Show the state of the tree after removing 15.

3.
In examining the AVL tree code, you will notice that pointers to tree nodes are frequently passed by reference. For example, look at the function Insert. Why is this necessary?

4.
Create an AVL tree that will require two rotations after a certain node is deleted. The smallest one I could create contained 12 nodes. Hint: you will need to make the tree as unbalanced as possible while still maintaining the AVL property.

Review Problems


Here are review exercises for trees. Not covered by these exercises are problems involving maps and multimaps from the standard library. Since these were the subject of Lab 5, they are ``fair game'' for the quiz.

1.
Suppose each non-leaf node of a m-ary has exactly m children -- in other words, each node of the tree has either exactly 0 or exactly m children. Suppose we build such a tree of height $h \geq
0$ having the minimum possible number of nodes. What is the number of nodes in this ``minimal'' tree? Prove your answer using mathematical induction.
2.
Consider the following binary search tree.


\psfig {figure=g1.eps}

Assuming it is an AVL tree, show the state of the tree after inserting 10 and then again after deleting 28.

3.
Given an empty AVL tree of integers, show the structure of the tree after each of the values 5, 2, 4, 6, 7, 1, 8 is inserted and then show the change to the resulting tree when 4 is deleted. Indicate where rotations are done to rebalance the tree.

4.
Given a binary search tree containing n floating point values and having a height that is $O( \log n )$ (i.e. it is balanced), write an algorithm to count the number of nodes storing values greater than x0 and less than x1. What is the running time of your algorithm? (More credit will be given for an efficient algorithm.)

You may assume the following declaration for tree nodes:

    struct TreeNode {
       float x;
       TreeNode * left;
       TreeNode * right;
    }
Start from the following prototype and assume the function is initially passed a pointer to the root:
        int Count( TreeNode * T, float x0, float x1 )

5.
It is possible to turn a sorted array of N floating point values into an AVL tree that is as balanced as possible in O(N) time. Write a function to do this. (Hint: think about which value should be stored at the root.) Assume the following structure declaration for AVL nodes.
    struct Avl_Node {
      float Element;
      Avl_Node *Left;
      Avl_Node *Right;
      int Height;
    };
Your function should calculate the heights of the nodes and it should return a pointer to the root of the tree. You do not need to prove that the result is an AVL tree and you do not need to prove the function requires O(N) time. Here is a prototype
     Avl_Node * ArrayToAVLTree( float values[], int N  )



 

Charles Stewart
10/2/1998