Docsity

Programming Assignment 4 - Computer Science I - Fall 2007 | COP 3502, Assignments of Computer Science

Toggle sidebar
bg1
Programming Assignment IV
COP3502H
Due: 3/31/07 11:59pm
The CEO of the Computer Sciences Corporation, Dr. Link Queue has just learned that
G.M. Adelson-Velsky and E.M. Landis will be visiting the company in two weeks. They
want to invest in the company, so Dr. Queue wants to give them a good demo. Thus, he
wants you to build an interactive AVL tree tool. In this assignment, you will write an
interactive balancing routine for binary trees that utilizes the AVL algorithm.
Requirements
You will develop an interactive program that will let users insert and delete nodes in a
binary search tree (BST) and also run the AVL algorithm. The program should also
output the contents of the tree after each operation. Thus, you should provide a menu
driven interface that lets users
1. Create an empty tree (and delete the old one if it exists).
2. Insert a node into the BST.
3. Delete a node from the BST.
4. Find the balancing factor for the root of the tree.
5. Balance the BST with AVL.
For operations 2,3, and 5, you should output the tree level, the nodes in that level, and the
children each node points to. For example, if I have a binary tree that looks like the
following:
22
/ \
15 30
/ \
8 18
The output would be
Level 0: Node: 22 โ€“ Left Child Node: 15 โ€“ Right Child Node: 30
Level 1: Node: 15 โ€“ Left Child Node 8 โ€“ Right Child Node: 18
Node: 30 โ€“ Leaf Node
Level 2: Node: 8 โ€“ Leaf Node
Node: 18 โ€“ Leaf Node
This output makes it very easy for you to draw out the trees on paper.
pf3

Partial preview of the text

Download Programming Assignment 4 - Computer Science I - Fall 2007 | COP 3502 and more Assignments Computer Science in PDF only on Docsity!

Programming Assignment IV

COP3502H

Due: 3/31/07 11:59pm

The CEO of the Computer Sciences Corporation, Dr. Link Queue has just learned that G.M. Adelson-Velsky and E.M. Landis will be visiting the company in two weeks. They want to invest in the company, so Dr. Queue wants to give them a good demo. Thus, he wants you to build an interactive AVL tree tool. In this assignment, you will write an interactive balancing routine for binary trees that utilizes the AVL algorithm.

Requirements

You will develop an interactive program that will let users insert and delete nodes in a binary search tree (BST) and also run the AVL algorithm. The program should also output the contents of the tree after each operation. Thus, you should provide a menu driven interface that lets users

  1. Create an empty tree (and delete the old one if it exists).
  2. Insert a node into the BST.
  3. Delete a node from the BST.
  4. Find the balancing factor for the root of the tree.
  5. Balance the BST with AVL.

For operations 2,3, and 5, you should output the tree level, the nodes in that level, and the children each node points to. For example, if I have a binary tree that looks like the following:

22 /
15 30 /
8 18

The output would be

Level 0: Node: 22 โ€“ Left Child Node: 15 โ€“ Right Child Node: 30 Level 1: Node: 15 โ€“ Left Child Node 8 โ€“ Right Child Node: 18 Node: 30 โ€“ Leaf Node Level 2: Node: 8 โ€“ Leaf Node Node: 18 โ€“ Leaf Node

This output makes it very easy for you to draw out the trees on paper.

Approach

Use the following data structure for your BSTs

struct tree_node { int data; struct tree_node *left; struct tree_node *right; int height; };

You should need to write 5 primary functions and some helper functions for this assignment.

struct tree_node * createBST();

struct tree_node* insertBSTNode(struct tree_node *root, int data);

struct tree_node* deleteBSTNode(struct tree_node *root, int data);

int findBalanceFactor (struct tree_node *root);

struct tree_node* balanceTreeAVL(struct tree_node *root);

The helper functions should be used as part of the balanceTreeAVL function to help determine which one of the 4 cases the BST could be in (RR, LL, RL, LR). I would also strongly encourage you to write 4 helper functions that do the rotations for the each case. This will make your code easier to read and debug. Also, make use of the notes for implementing the insert and deletion routines.

Also note, your balancing algorithm only needs to work for balancing factors up to 2 or -2.

Extra Credit

  1. Write a routine to print out the BSTs in a pretty format.
  2. Analyze the running time of your balanceTreeAVL algorithm.

Documentation

You must document your routines. Each function should be commented with what the function does, its input parameters, and what it returns. Comments are very important and will account for 20% of your grade.