Sunteți pe pagina 1din 14

Lab Manual for Data Structures

Lab 14
Day-Stout-Warren (DSW) Algorithm

Department of Computer Science Page 135


MAJU, Islamabad
Lab12: Day-Stout-Warren (DSW) Algorithm

Table of Contents
1. Introduction 137
1.1 Day–Stout–Warren (DSW) Algorithm 137
1.2 Relevant Lecture Readings 137

2. Activity Time boxing 137

3. Objectives of the experiment 137

4. Concept Map 137


4.1 Rotations in DSW Algorithm 138
4.2 Backbone or Vine 138
4.3 Pseudo code 140

5. Homework before Lab 141


5.1 Problem Solution Modeling 141
5.2 Problem description: 141
5.3 Practices from home 141
5.3.1 Task-1 141

6. Procedure & Tools 141


6.1 Tools 141
6.2 Walk through Tasks [Expected time = 20 mins] 141

7. Practice Tasks 146


7.1 Practice Task 1 [Expected time = 20 mins] 146
7.2 Out comes 147

8. Evaluation Task (Unseen) [Expected time = 60 mins for two tasks] 147

9. Evaluation criteria 148

10. Further Readings 148


10.1 Web sites related to C++ tutorials about linked list implementation of binary trees 148
10.2 Web sites containing supporting material 148

Department of Computer Science Page 136


MAJU, Islamabad
Lab12: Day-Stout-Warren (DSW) Algorithm

Lab12: Day–Stout–Warren (DSW) Algorithm

1. Introduction

In this lab, we will discuss the usage of binary trees and binary search trees using linked
list notations. We will see that how we may insert and remove nodes from binary trees and how
pre order, post order and in order traversal can be performed on binary trees. We will further see
that how search operation can be performed on binary search trees which are stored using a
linked list notation.

1.1 Day–Stout–Warren (DSW) Algorithm

The Day–Stout–Warren (DSW) algorithm is a method for efficiently balancing binary


search trees — that is, decreasing their height to O(log n) nodes, where n is the total number of
nodes. Unlike a self-balancing binary search tree, it does not do this incrementally during each
operation, but periodically, so that its cost can be amortized over many operations. The algorithm
was designed by Quentin F. Stout and Bette Warren in a 1986 CACM paper, based on work
done by Colin Day in 1976.

1.2Relevant Lecture Readings

a) Revise Lecture No. 25 and 26 available at \\dataserver\jinnah$\ in instructor’s


folder.
b) From books: C++ Data Structures by Nell Dale and Data structures using C++ by
D. S. Malik.

2. Activity Time boxing

Table 1: Activity Time Boxing


Task No. Activity Name Activity time Total Time
5.1 Design Evaluation 20mins 20mins
6.2 Walk through tasks 20mins 20mins
7 Practice tasks 20mins for task 1 and 2, 30 mins 70mins
for task 3
9 Evaluation Task 60mins for all assigned tasks 60mins

3. Objectives of the experiment

 To understand and implement binary trees using linked lists with their operations in C++
 To understand and implement binary search trees using linked lists in C++.
 To understand and implement pre order, post order and in order traversals on binary trees
and binary search trees using recursive functions in C++.

4. Concept Map
This concept map will help students to understand the main concepts of topic covered in
Department of Computer Science Page 137
MAJU, Islamabad
Lab12: Day-Stout-Warren (DSW) Algorithm

lab.

4.1 Rotations in DSW Algorithm

The building block for tree transformations in this algorithm is the rotation introduced by
Adel’son-Vel’skii and Landis (1962).There are two types of rotation, left and right, which are
symmetrical to one another. The right rotation of the node Ch about its parent Paris performed
according to the following algorithm:

rotateRight(Gr, Par, Ch)

if Par is not the root of the tree // i.e., if Gr is not null


grandparent Gr of child Ch becomes Ch’s parent;
rightsubtree of Ch becomes left subtree of Ch’s parent Par;
nodeCh acquires Par as its right child;

The steps involved in this compound operation are shown in Figure 1. The third step is the core
of the rotation, when Par, the parent node of child Ch, becomes the child of Ch, when the roles of
a parent and its child change. However, this exchange of roles cannot affect the principal
property of the tree, namely, that it is a search tree. The first and the second steps of
rotateRight()are needed to ensure that, after the rotation, the tree remains a search tree.

Figure 1 Right rotation of child Ch about parent Par

4.2 Backbone or Vine


The DSW algorithm transfigures an arbitrary binary search tree into a linked list like tree called a
backbone or vine. Then this elongated tree is transformed in a series of passes into a perfectly
balanced tree by repeatedly rotating every second node of the backbone about its parent.

Department of Computer Science Page 138


MAJU, Islamabad
Lab12: Day-Stout-Warren (DSW) Algorithm

This algorithm is illustrated in Figure 2. Note that a rotation requires knowledge about the parent
of tmp, so another pointer has to be maintained when implementing the algorithm.

Figure 2 Transforming a binary search tree into a backbone.

In the best case, when the tree is already a backbone, the while loop is executed n times and no
rotation is performed. In the worst case, when the root does not have a right child, the while loop
executes 2n– 1 times with n– 1 rotations performed, where n is the number of nodes in the tree;
that is, the run time of the first phase is O(n). In this case, for each node except the one with the
smallest value, the left child of tmp is rotated about tmp. After all rotations are finished, tmp
points to the root, and after n iterations, it descends down the backbone to become null.

In the second phase, the backbone is transformed into a tree, but this time, the tree is perfectly
balanced by having leaves only on two adjacent levels. In each pass down the backbone, every
second node down to a certain point is rotated about its parent. The first pass is used to account
for the difference between the number n of nodes in the current tree and the number 2[lg(n+1)] – 1
of nodes in the closest complete binary tree where [x] is the closest integer less than x. That is,
the overflowing nodes are treated separately.

Department of Computer Science Page 139


MAJU, Islamabad
Lab12: Day-Stout-Warren (DSW) Algorithm

Figure 3 contains an example. The backbone in Figure 2e has nine nodes and is preprocessed by
one pass outside the loop to be transformed into the backbone shown in Figure 3b. Now, two
passes are executed. In each backbone, the nodes to be promoted by one level by left rotations
are shown as squares; their parents, about which they are rotated, are circles.

Figure 3 Transforming a backbone into a perfectly balanced tree.

4.3 Pseudo code


The following is a presentation of the basic DSW algorithm in pseudo code, after the Stout–
Warren paper. It consists of a main routine with three subroutines. The main routine is given by;

1. Allocate a node, the "pseudo-root", and make the tree's actual root the right child of
the pseudo-root.
2. Call tree-to-vine with the pseudo-root as its argument.
3. Call vine-to-tree on the pseudo-root and the size (number of elements) of the tree.
4. Dispose of the pseudo-root.

Department of Computer Science Page 140


MAJU, Islamabad
Lab12: Day-Stout-Warren (DSW) Algorithm

5. Homework before Lab

This homework will help students to study the concepts of topic before start of lab.

5.1 Problem Solution Modeling


After studying the introduction and concept map sections you should be ready to provide
the solution of following problems. Design the solutions of the problems in C++ and
bring your code to lab so that lab instructor should assess and grade it.

5.2Problem description:
Write a program of binary search tree using linked listfor objects of “person” class.
Attributes of “person” class (privately defined) are per_id (int), per_name (string) and
per_age (int). “person” class contains member functions (publicly defined): constructor,
input and output functions. You are required to define two more classes: one of these
classes should define the node implementation of linked list for objects of “person” class.
Other class should define the binary tree implementation for objects of “person” class,
this class should contain the member functions like insert, search, delete, pre order, and
post order and in order traversals of binary tree.

5.3Practices from home

5.3.1 Task-1
Provide differences between binary tree and binary search tree implementations using
linked list structures.

6. Procedure& Tools
This section provides information about tools and programming procedures used for the
lab.

6.1 Tools
Microsoft Visual Studio 2008 with Visual C++ compiler configured.

6.2 Walk through Tasks [Expected time = 20mins]


Following screens in figure 4 to figure 9represent source code implementation of a binary
search tree using linked list implementation in Microsoft Visual Studio 2008. You are required to
type, debug and execute this program in Microsoft Visual Studio 2008.

Department of Computer Science Page 141


MAJU, Islamabad
Lab12: Day-Stout-Warren (DSW) Algorithm

Figure 4: Implementation of DSW algorithm in C++

Figure 5: Implementation of DSW algorithm in C++


Department of Computer Science Page 142
MAJU, Islamabad
Lab12: Day-Stout-Warren (DSW) Algorithm

Figure 6: Implementation of DSW algorithm in C++

Department of Computer Science Page 143


MAJU, Islamabad
Lab12: Day-Stout-Warren (DSW) Algorithm

Figure 7: Implementation of DSW algorithm in C++

Department of Computer Science Page 144


MAJU, Islamabad
Lab12: Day-Stout-Warren (DSW) Algorithm

Figure 8: Implementation of DSW algorithm in C++

Department of Computer Science Page 145


MAJU, Islamabad
Lab12: Day-Stout-Warren (DSW) Algorithm

Figure 9: Implementation of DSW algorithm in C++

7. Practice Tasks
This section will provide information about the practice tasks which are required to be performed
in lab session. Design solutions of problems discussed in each task and placesolution code in a
folder specified by your lab instructor.

7.1 Practice Task 1 [Expected time = 20 mins]


Apply DSW to following values.
1) 67, 45, 64, 23, 87, 67, 56, 34, 98, 62, 10, 8, 5, 2, 1.
2) 34, 6, 2, 7, 2, 54, 9, 89, 67, 45, 33, 56, 78, 88, 100.
And answer following questions without drawing tree
Number of Node: _________
Number of Initial rotations (n-m): _________
How many iterations/passes of while loop would be required: ____________
Write number of left rotations in each iteration of while loop

Details of rotations per Loop Iterations goes here


Iteration Number Number of Rotations

Department of Computer Science Page 146


MAJU, Islamabad
Lab12: Day-Stout-Warren (DSW) Algorithm

7.1.2 Practice Task 2:

You have been provided with a BST having of integer values.

Balance the above BST using DSW Algorithm. Convert it to vine. Once Vine is done then give following details
(You must complete it without drawing tree)

Number of Node: _________


Number of Initial rotations (n-m): _________
How many iterations/passes of while loop would be required: ____________
Write number of left rotations in each iteration of while loop

Details of rotations per Loop Iterations goes here


Iteration Number Number of Rotations

7.2Out comes

After completing this lab, student will be able to understand and develop programs related to
AVL trees and heap treesin C++ using Microsoft Visual Studio 2008 environment.

8.Evaluation Task (Unseen) [Expected time = 60mins for two tasks]

Department of Computer Science Page 147


MAJU, Islamabad
Lab12: Day-Stout-Warren (DSW) Algorithm

The lab instructor will give you unseen task depending upon the progress of the class.

9. Evaluation criteria
The evaluation criteria for this lab will be based on the completion of the following tasks. Each
task is assigned the marks percentage which will be evaluated by the instructor in the lab whether
the student has finished the complete/partial task(s).

Table 2: Evaluation of the Lab


Sr. No. Task No Description Marks
1 4 Problem Modeling 20
2 6 Procedures and Tools 10
3 7,8 Practice tasks and Testing 35
4 8.1 Evaluation Tasks (Unseen) 20
5 Comments 5
6 Good Programming Practices 10

10. Further Readings


10.1 Web sites related to C++ tutorials about linked list implementation of binary trees
1. http://www.dreamincode.net/forums/topic/10157-data-structures-in-c-tutorial/
2. http://login2win.blogspot.com/2011/06/binary-search-trees-in-c.html
3. http://www.dailyfreecode.com/Code/binary-search-tree-operations-1152.aspx

10.2 Web sites containing supporting material


1. http://web.eecs.utk.edu/~bvz/teaching/cs140Fa09/lecture_notes.html
2. http://oopweb.com/Algorithms/Files/Algorithms.html

Department of Computer Science Page 148


MAJU, Islamabad

S-ar putea să vă placă și