Project2-SampleReport

Project2-SampleReport - Project 2 Binary Search Tree Index...

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: Project 2: Binary Search Tree Index Chapter 1: Introduction………………………..…………..…3 1-1 Problem Description………………………………………..…3 1-2 Our method to solve this problem…………………………….3 Chapter 2: Algorithm specification………………………..…3 2-1 Our Pretty Way………………………………………………..3 2-2 Pseudo-code algorithm specification……………….………...5 Chapter 3: Testing Result…………………………………..…9 Chapter 4: Analysis and Comments………………………...13 Chapter 5: the Environment the Program ………………...14 Appendix: Source Code (in C)…………………………….14 -1- CS130A Fundamentals of Data Structures Programming Project 2 -2- Chapter 1: Introduction 1.1 Problem description As we know, unbalanced binary search trees, AVL trees, and splay trees are three different search tree structures. Though they are similar to each other, their performances are quite different. All three have their advantages and disadvantages. Now we want to analyze and compare the performances of a sequence of insertions and deletions on these search tree structures. The testing will be done on a set of N distinct integers in the following ways: (1) Insert N integers in increasing order and delete them in the same order; (2) Insert N integers in increasing order and delete them in the reverse order; (3) Insert N integers in random order and delete them in random order; We will plot the run times with respect to the sizes of input to illustrate the difference. Then we will clearly compare the function of these three search tree structures 1.2 Our method to solve this problem We use several simple functions to perform this project. Namely, Tree RotateLL(Tree K2), Tree RotateRL(Tree K2), Tree splayinsert(int n,Tree T), Tree AVLinsert(int n,Tree T) and so on. Thus the structure of our code is quite clear. When imply the function on a splay tree, we make use of the Stack to get a quite wonderful result. We use the C’s standard library time.h to measure the time. Then we will give a chart and a graph to show our results. Chapter 2: Algorithm specification 2.1 Our Pretty Way We will use a pretty way to work it out. 1. Tree unbaltreeinsert(int n, Tree T) The insertion routine is conceptually simple. To insert X into tree T, proceed down the tree .If X is found, do nothing. Otherwise, insert X at the last spot on the path traversed. -3- 2. Tree unbaltreedelet(int n,Tree T); The deletion is also simple. If the node we want to delete is a leaf, it can be deleted immediately. If the node has one child, the node can be deleted after its parent adjusts a pointer to bypass the node. If the node has two children, we will replace the data of this node with the smallest data of the right subtree and recursively delete the node. after delete 60 3. Tree AVLinsert(int n,Tree T); To insert a new node with key X into an AVL tree T, we recursively insert X into the appropriate subtree of T(let us call this TLR). If the height of TLR does not change, then we are done. Otherwise, if a height imbalance appears in T, we do the appropriate single or double rotation depending on X and the keys in T and TLR, update the heights, and are done. After insert June 4. Tree AVLdelet(int n,Tree T); If the node we want to delete is a leaf, it can be deleted immediately. If the node has one child, the node can be deleted after its parent adjusts a pointer to bypass the node. If the node has two children, we will replace the data of this node with the -4- smallest data of the right subtree and recursively delete the node. We need to adjust the height of the tree while doing the deletion. 5. Tree splayinsert(int n,Tree T); To insert a new node with key X into an splay tree T, we recursively insert X into the appropriate subtree of T. We use a stack to record the path to the node. After finding the position to insert, we insert X. Then we do the adjust routine. If the parent of X is the root of the tree, we merely rotate X and the root. This is the last rotation along the access path. Otherwise, X has both a parent(P) and a grandparent(G), and there are two cases, plus symmetries, to consider. The first case is the zig-zag case. Here X is a right child and P is a left child (or vice versa). If this is the case, we perform a double rotation. Otherwise, we have a zig-zig case: X and P are either both left children or both right children. In that case, we transform the tree like the graph below. 6. Tree splaydelet(int n, Tree T); To delete a node with key X in an splay tree T, we recursively find X in the appropriate subtree of T. We use a stack to record the path to the node. After finding the node, we splay it to the position of the root. If it is deleted, we get two subtrees TR and TL (left and right). If we find the largest element in TL (which is easy), then this element is rotated to the root of TL, and TL will now have a root with no right child. We can finish the deletion by making TR the right child. 2-2 Pseudo-code algorithm specification Tree unbaltreeinsert(int n, Tree T) { if (T is NULL) -5- build a new tree; else { if(X<T->element) go left until X<T->element is not true; else if(X>T->element) go right until X>T->element is not true; else find the place to insert; } } Tree unbaltreedelet(int n,Tree T) { if(T is NULL) Error; else { if(X<T->element) go left until X<T->element is not true; else if(X>T->element) go right until X>T->element is not true; else { find element to be deleted; if(the element has two children) replace the data of this node with the smallest data of the right subtree and recursively delete the node; else delete it after its parent adjusts a pointer to bypass the node; } } } Tree AVLinsert(int n,Tree T) { if (T is NULL) build a new tree; else { if(X<T->element) { go left until X<T->element is not true; find the position to insert; -6- if(Height of left subtree-Height of right subtree=2) { if(X<T->left->element) singlerotatewithleft(T); else doublerotatewithleft(T); } } else if(X>T->element) { go right until X>T->element is not true; find the position to insert; if(Height of right subtree-Height of left subtree=2) { if(X>T->right->element) singlerotatewithright(T); else doublerotatewithright(T); } } } update the heights; } Tree AVLdelet(int n,Tree T) { if (T is NULL) Error; else { if(X<T->element) { go left until X<T->element is not true; if(Height of right subtree-Height of left subtree=2) { if(Height(T->right->left) >= Height(T->right->right)) doublerotatewithright(T); else singlerotatewithright(T); } } else if(X>T->element) { go right until X>T->element is not true; -7- if(Height of left subtree-Height of right subtree=2) { if(Height(T->left->left) >= Height(T->left->right)) singlerotatewithleft(T); else doublerotatewithleft(T); } } else { find element to be deleted; if(the element has two children) replace the data of this node with the smallest data of the right subtree and recursively delete the node; else delete it after its parent adjusts a pointer to bypass the node; } } update the heights; } Tree splayinsert(int n,Tree T) { if (T is NULL) build a new tree; else { if(X<T->element) { go left until X<T->element is not true; use stack to record the path to a node; } else if(X>T->element) { go right until X>T->element is not true; use stack to record the path to a node; } else find the place to insert; Insert X; if (the parent of X is the root of the tree) rotate X and the root; else if(X is a right child and P is a left child (or vice versa)) do zig-zag; -8- else if(X and P are either both left children or both right children) do zig-zig; } } Tree splaydelet(int n, Tree T); { if (T is NULL) Error; else { Find(X); Splay X to the root; Remove X; FindMax ( TL ) ; Make TR the right child of the root of TL; } } Chapter3: Testing results There are 3 main operations on the three kinds of tree structures (binary search tree, AVL tree, splay tree) as followed: (1) Insert N integers in increasing order and delete them in the same order; (2) Insert N integers in increasing order and delete them in the reverse order; (3) Insert N integers in random order and delete them in random order; Since the exact values of the integers can hardly affect the total running time of the program, we assign the N integers as 1,2,…, N so that simplify the program and the test. The test results have been listed in the following table: -9- N 1000 2000 3000 4000 5000 6000 7000 8000 9000 10000 0.005000 0.036333 0.088667 0.156333 0.234667 0.343667 0.463667 0.609333 0.802000 0.942667 0.015667 0.073000 0.166667 0.307333 0.473667 0.687667 0.921667 1.198000 1.526000 1.854333 0.005333 0.000000 0.015667 0.005333 0.005333 0.010333 0.015667 0.015667 0.021000 0.010333 0.001088 0.002456 0.004667 0.005754 0.007404 0.008772 0.010421 0.012053 0.013702 0.015351 0.001368 0.002754 0.004386 0.006579 0.011509 0.012877 0.018632 0.017825 0.027140 0.018088 0.001386 0.003281 0.005211 0.007404 0.009877 0.011789 0.013439 0.015632 0.025211 0.020561 0.000728 0.001521 0.002249 0.003039 0.003829 0.004681 0.005412 0.006198 0.006930 0.007782 0.000424 0.000790 0.001214 0.001700 0.002128 0.002553 0.002977 0.003467 0.003891 0.004319 0.002556 0.005716 0.009241 0.012708 0.016778 0.020673 0.026813 0.034167 0.031860 0.041463 Time of operation(1) (sec) Binary search tree Time of operation(2) (sec) Time of operation(3) (sec) Time of operation(1) (sec) AVL tree Time of operation(2) (sec) Time of operation(3) (sec) Time of operation(1) (sec) Splay tree Time of operation(2) (sec) Time of operation(3) (sec) For comparing the results directly and easily, we also give the test results in graphs: The three diagrams below compare the results of applying different trees. ‘Series1’ refer to same order operation; ‘Series2’ refer to reverse order operation; ‘Series3’ refer to random order operation. - 10 - - 11 - The three diagrams below compare the results of different operation. ‘Series1’ refer to applying of BS Tree; ‘Series2’ refer to applying of AVL Tree; ‘Series3’ refer to applying of Splay Tree. - 12 - Chapter 4: Analyses and Comments In the three trees we all use structure to store the element in the node. Since there are N nodes in total, it is obvious that the space complexities of the three trees are all S=O(N). As to time complexity, analysis is followed: For the ordinary binary search tree, we haven’t do any special work on its structure. So we expect it to be slow when inserting and deleting elements in it and in fact for operation 1 and 2, binary search tree is the slowest but for operation 3, it is the fastest. Because when operations are in a certain order, the other two trees have extra strategies (rotation and splaying) to reduce running time but binary search tree has none. And when operations are not in a certain order, the strategies in those trees increase the running time and the ordinary binary search tree have a better running time conversely. The time complexity of binary search is O (log N) in average and O (N) in the worst case. For AVL tree, we have an efficient strategy, rotation, including single rotation and double rotation. This will balance the tree and make inserting and deleting operations more efficient in some cases. Actually we can also see this through the test results in operation 1 and 2. At the same time, for operation 3, this strategy does not function well, since it is slower than the binary search tree. The time complexity of AVL tree is O (log N). As to splay tree, we also do some work to make it run faster, the splaying strategy. It is like rotation. The running time of operation 1 and 2 for AVL tree and splay tree is very close and both are faster than binary search tree very much but splay tree is a little faster than AVL tree. And for operation 3, splaying has no advantage, so as to - 13 - rotation. It means when operating without order, binary search tree is better. Splay tree is useful when operating certain data in many times. It has a amortized time complexity O (log N) and it guarantee for any M consecutive operations, it cost at most O (M log N). There is a special point in the second graph for AVL tree in operation 2 at size 5000. It may be caused by the unstable of the running environment. In short, different trees are useful in different cases. We can choose the right one when we handle different data. Chapter 5: the Environment the Program As we know, the environment which the program runs on directly influences the test result, for instance, a Pentium 2 CPU and a Core (TM)2 CPU differs a lot in speed, and the software which runs the source code will make some difference to the final result, too. Thus, here are the main aspects of the environment we use to do the program. CPU: Intel (R) Core (TM) 2 T 7200 @ 2.00 GHz Memory: 1.00 GB Software: C-Free3.5 Appendix: Source Code (in C) - 14 - ...
View Full Document

This note was uploaded on 01/23/2012 for the course CS 130a taught by Professor Staff during the Spring '08 term at UCSB.

Ask a homework question - tutors are online