You are on page 1of 2

BIRLA INSTITUTE OF TECHNOLOGY & SCIENCE, PILANI First Semester 2011-2012 CS / IS C363 Data Structures and Algorithms [Section

1 Monday, 16th September] Lab Sheet 6 [Duration: 165 minutes] Note: All files should be saved inside the directory named lab6 $ scp -r <username>@blade3:~/ lab6 . All names of files/functions are to be maintained as is listed. Files/functions may not be evaluated if the names are changed. Upload only the folder lab4 to the server. $ scp -r lab6 <username>@blade3:~ End Note.

Problem
A Binary Search Tree(BST) is a binary tree in which all nodes in the left sub-tree are less than the current node, and all node in the every node left sub-tree are less than the current node. In this lab, you are expected to create a BST tree of integers stored in an input file and then convert it to a heightbalanced BST. This is achieved by first putting the median of values in the tree as the root node, putting all values less than this value in the left sub-tree and all the value greater than or equal to this value in the right sub-tree. The process is then repeated for the left sub-tree and right sub-tree. For this problem, assume that all the values in the input are unique. Node structure: left value right

Here, value is the element stored in the BST, and left and right are the pointers to left and right subtrees. Type definitions The structure BST contains the definitions for the node structure indicated above. Steps: 1. Create a driver procedure (tree.c) which takes in as command-line parameters the filename of the file containing the list of elements and the name of the output file. a. This procedure will populate the BST by repeatedly inserting elements from input.txt into the tree b. Compute the height of the initial tree. c. call the balance method that balances the tree by recursively performs the balancing logic mentioned earlier. d. print the result in an output file by calling the printTree procedure. e. Compute the height of the balanced tree.

f. Print the difference between the initial hieght and the hieght after balancing in a file named result.txt 2. Create the necessary header files for the ADT definition, and ADT function prototypes in files named BST.h and BSTOps.h respectively. 3. Write a procedure to insert element to an BST tree in a file named insertBST.c. a. This procedure will take the element to insert and the tree as input parameters, and returns the updated tree. b. While inserting each node, the code should gaurantee the property of BST. 4. Write a procedure to balance the tree in a file named balance.c. The steps are as follows: a. First perform inorder traversal and put all the values in the tree into a list b. find mid element, and put it as root of tree. c. All elements smaller than mid element have to be inserted in the left of the root node d. All elements larger than or equal to the mid element have to be inserted in the right of the root node e. Repeat the procedure for the left-subtree and right sub-tree of this root node 5. Write a procedure to print the contents of the tree in pre-order into the output file. Deliverables: BST.h, BSTOps.h, tree.c, balance.c, insertBST.c, Makefile, executable(tree), input file, output file(output.txt, result.txt).

You might also like