+ All Categories
Home > Documents > Cs2201 Unit2 Notes

Cs2201 Unit2 Notes

Date post: 04-Jun-2018
Category:
Upload: manikandanu1
View: 222 times
Download: 0 times
Share this document with a friend
15
 h  t  t  p : / /  c  s  e  t  u   b  e .  t  k / DATA STRUCTURES (CS2201 /141301) Code:141301 Issue : C Rev:01 Name of the subject :Data Structures &Algorithms Page:2of 2 Unit No:2 Period:1 Tree ADT A tree can be defined in several ways. One natural way to define a tree is r ecursiv ely. A tree is a collection of nodes. The collection can be empty, which is sometimes denoted as A. Otherwise, a tree consists of a distinguished node  r , called the root, and zero or more (sub)trees T 1 , T 2 , . . . , T k , each of whose roots are connected by a directed edge to r . he root of each subtree is said to be a  child of r , and r is the  parent of each subtree root. Figure 4.1 shows a typical tree using the recursive definition. From the recursive definition, we find that a tree is a collection of n node s, one of which is the root, and n - 1 edges. That there are n - 1 edges follows from the fact that each edge connects some node to its parent and every node except the root has one parent. Basic structure of a tree: Figure: A tree n the tree of Figure the root is  A . Node F has A as a parent and K , L, and M as children. Each node may have an arbitrary number of children, possibly zero. Nodes with no children are known as  leaves; the leaves in the tree above are  B, C , H , I , P , Q , K , L, M , and N . http://csetube.weebly.com/ GKM COLLEGE OF  ENGINEERING & TECHNOLOGY
Transcript

8/13/2019 Cs2201 Unit2 Notes

http://slidepdf.com/reader/full/cs2201-unit2-notes 1/15

 h t t p:/

/ c s e

 t u  b e

. t k/

DATA STRUCTURES (CS2201 /141301)

Code:141301 Issue : C Rev:01

Name of the subject :Data Structures &Algorithms Page:2of 2

Unit No:2 Period:1

Tree ADT

A tree can be defined in several ways. One natural way to definea tree is recursively. A tree is a collection of nodes. The collectioncan be empty, which is sometimes denoted as A. Otherwise, a treeconsists of a distinguished node r , called the root, and zero or more(sub)trees T 1, T 2, . . . , T k, each of whose roots are connected by adirected edge to r .

he root of each subtree is said to be a child of r , and r is the parent of each subtree root. Figure 4.1 shows a typical tree using therecursive definition.

From the recursive definition, we find that a tree is acollection of n nodes, one of which is the root, and n - 1 edges. Thatthere are n - 1 edges follows from the fact that each edge connectssome node to its parent and every node except the root has one parent.

Basic structure of a tree:

Figure: A tree

n the tree of Figure the root is A . Node F has A as a parent andK , L, and M as children. Each node may have an arbitrary number ofchildren, possibly zero. Nodes with no children are known as leaves;the leaves in the tree above are B, C , H , I , P , Q , K , L, M , and N .

http://csetube.weebly.com/

GKM COLLEGE OF ENGINEERING & TECHNOLOGY

8/13/2019 Cs2201 Unit2 Notes

http://slidepdf.com/reader/full/cs2201-unit2-notes 2/15

 h t t p:/

/ c s e

 t u  b e

. t k/

Nodes with the same parent are siblings; thus K , L, and M are allsiblings. Grandparent and  grandchild relations can be defined in asimilar manner.

 path from node n1 to nk is defined as a sequence of nodes n1, n2,. . . , nk such that ni is the parent of ni+1 for 1 i < k. The length ofthis path is the number of edges on the path, namely k -1. There is apath of length zero from every node to itself. Notice that in a tree

there is exactly one path from the root to each node.

For any node ni, the depth of ni is the length of the unique pathfrom the root to ni. Thus, the root is at depth 0. The height of ni isthe longest path from ni to a leaf. Thus all leaves are at height 0. Theheight of a tree is equal to the height of the root. For the tree inFigure 4.2, E is at depth 1 and height 2; F is at depth 1 and height 1;the height of the tree is 3. The depth of a tree is equal to the depthof the deepest leaf; this is always equal to the height of the tree.

If there is a path from n1 to n2, then n1 is an ancestor of n2 andn2 is a d escendant of n1. If n1 &n2, then n1 is a proper ancestor of n2and n2 is a proper descendant of n1.

http://csetube.weebly.com/

8/13/2019 Cs2201 Unit2 Notes

http://slidepdf.com/reader/full/cs2201-unit2-notes 3/15

 h t t p:/

/ c s e

 t u  b e

. t k/

Lecture Plan

Code:141301 Issue : C Rev:01

Name of the subject :Data Structures &Algorithms Page:2of 2

Unit No:2 Period:2

Tree Traversals with an Application

Traversing means visiting each node only once. Tree traversal is amethod for visiting all the nodes in the tree exactly once.there are 3types of tree traversal techniques,namely

1.Inorder Traversal

-traverse the left subtree inorder

-visit the root

-traverse the right subtree in inorder.

Void Inorder(Tree T)

{

If(T!=NULL)

{

InOrder(T->Left);

printElement(T->Element);

Inorder(T->right);

}

}

2.Preorder traversal

-visit the root.

-traverse the left subtree in preorder.

-traverse the right subtree in preorder.

Void Preorder(Tree T)

{

http://csetube.weebly.com/

8/13/2019 Cs2201 Unit2 Notes

http://slidepdf.com/reader/full/cs2201-unit2-notes 4/15

 h t t p:/

/ c s e

 t u  b e

. t k/

If(T!=NULL)

{

printElement(T->Element);

Preorder(T->Left);

Preorder(T->Right);

}

}

3.postorder traversal

-traverse the left subtree in postorder.

-traverse the right subtree in postorder.

-visit the root.

Void Postorder(Tree T)

{

If(T!=NULL)

{

Postorder(T->Left);

Postorder(T->Right);

printElement(T->Element);

}

}

http://csetube.weebly.com/

8/13/2019 Cs2201 Unit2 Notes

http://slidepdf.com/reader/full/cs2201-unit2-notes 5/15

 h t t p:/

/ c s e

 t u  b e

. t k/

Lecture Plan

Code:141301 Issue : C Rev:01

Name of the subject :Data Structures &Algorithms Page:2of 2

Unit No:2 Period:3

Implementation of Trees

One way to implement a tree would be to have in each node,besides its data, a pointer to each child of the node. However, sincethe number of children per node can vary so greatly and is not known inadvance, it might be infeasible to make the children direct links inthe data structure, because there would be too much wasted space. Thesolution is simple: Keep the children of each node in a linked list oftree nodes. The declaration in Figure is typical.

typedef struct tree_node *tree_ptr;struct tree_node{element_type element;tree_ptr first_child;tree_ptr next_sibling;};

http://csetube.weebly.com/

8/13/2019 Cs2201 Unit2 Notes

http://slidepdf.com/reader/full/cs2201-unit2-notes 6/15

 h t t p:/

/ c s e

 t u  b e

. t k/

Lecture Plan

Code:141301 Issue : C Rev:01

Name of the subject :Data Structures &Algorithms Page:2of 2

Unit No:2 Period:4

Binary Trees

A binary tree is a tree in which no node can have more than twochildren.Figure shows that a binary tree consists of a root and twosubtrees, T l and T r, both of which could possibly be empty.

A property of a binary tree that is sometimes important is thatthe depth of an average binary tree is considerably smaller than n. theexample in Figure shows.

Figure :Generic binary tree

Figure :Worst-case binary tree

Binary tree node declarations:

typedef struct tree_node *tree_ptr;struct tree_node{element_type element;tree_ptr left;tree_ptr right;};typedef tree_ptr TREE;

http://csetube.weebly.com/

8/13/2019 Cs2201 Unit2 Notes

http://slidepdf.com/reader/full/cs2201-unit2-notes 7/15

 h t t p:/

/ c s e

 t u  b e

. t k/

Many of the rules that apply to linked lists will apply to treesas well. In particular, when an insertion is performed, a node willhave to be created by a call to malloc . Nodes can be freed afterdeletion by calling free.

We could draw the binary trees using the rectangular boxes thatare customary for linked lists, but trees are generally drawn ascircles connected by lines, because they are actually graphs. We also

do not explicitly draw NULL pointers when referring to trees, becauseevery binary tree with n nodes would require n + 1 NULL pointers.

Binary trees have many important uses not associated withsearching. One of the principal uses of binary trees is in the area ofcompiler design, which we will now explore.

http://csetube.weebly.com/

8/13/2019 Cs2201 Unit2 Notes

http://slidepdf.com/reader/full/cs2201-unit2-notes 8/15

 h t t p:/

/ c s e

 t u  b e

. t k/

Lecture Plan

Code:141301 Issue : C Rev:01

Name of the subject :Data Structures &Algorithms Page:2of 2

Unit No:2 Period:5

Expression Trees

The leaves of an expression tree are operands, such as constantsor variable names, and the other nodes contain operators. Thisparticular tree happens to be binary, because all of the operations arebinary, and although this is the simplest case, it is possible fornodes to have more than two children.

It is also possible for a node to have only one child, as is thecase with the unary minus operator. We can evaluate an expression tree,T , by applying the operator at the root to the values obtained by

recursively evaluating the left and right subtrees.

Example:

(a + (b*c)) + (((d * e) + f )* g ).

Constructing an Expression Tree

As an example, suppose the input isa b + c d e + * *

The first two symbols are operands, so we create one-node trees and push

pointers

to them onto a stack.**For convenience, we will have the stack grow from left to right in the diagrams.

http://csetube.weebly.com/

8/13/2019 Cs2201 Unit2 Notes

http://slidepdf.com/reader/full/cs2201-unit2-notes 9/15

 h t t p:/

/ c s e

 t u  b e

. t k/

Next, a '+' is read, so two pointers to trees are popped, a new tree is

formed,

and a pointer to it is pushed onto the stack.*

 Next, c, d, and e are read, and for each a one-node tree is created and a pointer to the

corresponding tree is pushed onto the stack.

Now a '+' is read, so two trees are merged.

Continuing, a '*' is read, so we pop two

tree pointers and form a new tree with a '*' as root.

http://csetube.weebly.com/

8/13/2019 Cs2201 Unit2 Notes

http://slidepdf.com/reader/full/cs2201-unit2-notes 10/15

 h t t p:/

/ c s e

 t u  b e

. t k/

Finally, the last symbol is read, two trees aremerged, and a pointer to the final tree is left on the

stack.

Lecture Plan

Code:141301 Issue : C Rev:01

Name of the subject :Data Structures &Algorithms Page:2of 2

Unit No:2 Period:6

Binary Search Trees:

The property that makes a binary tree into a binary search tree is thatfor every node, X , in the tree, the values of all the keys in the leftsubtree are smaller than the key value in X , and the values of all thekeys in the right subtree are larger than the key value in X .

Figure:Two binary trees (only the left tree is a search tree)

Binary search tree declarations

typedef struct tree_node *tree_ptr;struct tree_node

http://csetube.weebly.com/

8/13/2019 Cs2201 Unit2 Notes

http://slidepdf.com/reader/full/cs2201-unit2-notes 11/15

 h t t p:/

/ c s e

 t u  b e

. t k/

{element_type element;tree_ptr left;tree_ptr right;};typedef tree_ptr SEARCH_TREE;

Make_null

This operation is mainly for initialization. Some programmers prefer toinitialize the first element as a one-node tree, but our implementationfollows the recursive definition of trees more closely.

SEARCH_TREEmake_null ( void ){return NULL;}

Find

tree_ptr

find( element_type x, SEARCH_TREE T )

{

if( T == NULL )

return NULL;

if( x < T->element )

return( find( x, T->left ) );

else

if( x > T->element )

return( find( x, T->right ) );

else

return T;}

Find_min and find_max

tree_ptr

find_min( SEARCH_TREE T )

{

if( T == NULL )

return NULL;

else

if( T->left == NULL )

return( T );

else

return( find_min ( T->left ) );

}

Insert

tree_ptr

insert( element_type x, SEARCH_TREE T )

http://csetube.weebly.com/

8/13/2019 Cs2201 Unit2 Notes

http://slidepdf.com/reader/full/cs2201-unit2-notes 12/15

 h t t p:/

/ c s e

 t u  b e

. t k/

{

if( T == NULL )

{

T = (SEARCH_TREE) malloc ( sizeof (struct tree_node) );

if( T == NULL )

fatal_error("Out of space!!!");

else

{T->element = x;

T->left = T->right = NULL;

}

}

else

if( x < T->element )

T->left = insert( x, T->left );

else

if( x > T->element )

T->right = insert( x, T->right );

return T;

}

Lecture Plan

Code:141301 Issue : C Rev:01Name of the subject :Data Structures &Algorithms Page:2of 2

Unit No:2 Period:7

Binary Search Trees

Delete

tree_ptr

delete( element_type x, SEARCH_TREE T )

{tree_ptr tmp_cell, child;

if( T == NULL )error("Element not found");

else

if( x < T->element ) /* Go left */

T->left = delete( x, T->left );

else

if( x > T->element ) /* Go right */

T->right = delete( x, T->right );

http://csetube.weebly.com/

8/13/2019 Cs2201 Unit2 Notes

http://slidepdf.com/reader/full/cs2201-unit2-notes 13/15

 h t t p:/

/ c s e

 t u  b e

. t k/

else /* Found element to be deleted */

if( T->left && T->right ) /* Two children */

{ /* Replace with smallest in right subtree */

tmp_cell = find_min( T->right );

T->element = tmp_cell->element;

T->right = delete( T->element, T->right );

}

else /* One child */}

tmp_cell = T;

if( T->left == NULL ) /* Only a right child */

child = T->right;

if( T->right == NULL ) /* Only a left child */

child = T->left;

free( tmp_cell );

return child;

}

return T;

}

Lecture Plan

Code:141301 Issue : C Rev:01

Name of the subject :Data Structures &Algorithms Page:2of 2

Unit No:2 Period:8

Applications of trees:

Trees are often used in and of themselves to store data directly,however they are also often used as the underlying implementation forother types of data structures such as Hash Tables, Sets and Maps andother associative containers. Specifically, the C++ Standard TemplateLibrary uses special red/black trees as the underlying implementationfor sets and maps, as well as multisets and multimaps.

Trees and Board Games – Trees are often used in implementinggames,

 particularly “board” games

http://csetube.weebly.com/

8/13/2019 Cs2201 Unit2 Notes

http://slidepdf.com/reader/full/cs2201-unit2-notes 14/15

 h t t p:/

/ c s e

 t u  b e

. t k/

- node represents a position in the board 

- branches from a node represent all thepossible moves from that position

-the children represent the new positions

Planning a Move Ahead – equivalent to choosing a path through the game

tree

Games with Cycles – it is possible to return to a previous position

Lecture Plan

Code:141301 Issue : C Rev:01

Name of the subject :Data Structures &Algorithms Page:2of 2

Unit No:2 Period:9

Threaded binary tree :

"A binary tree is threaded by making all right child pointersthat would normally be null point to the inorder successor of the node,and all left child pointers that would normally be null point to theinorder predecessor of the node."[1]

A threaded binary tree makes it possible to traverse the valuesin the binary tree via a linear traversal that is more rapid than arecursive in-order traversal.

http://csetube.weebly.com/

8/13/2019 Cs2201 Unit2 Notes

http://slidepdf.com/reader/full/cs2201-unit2-notes 15/15

 h t t p:/

/ c s e

 t u  b e

. t k/

It is also possible to discover the parent of a node from athreaded binary tree, without explicit use of parent pointers or astack, albeit slowly. This can be useful where stack space is limited,or where a stack of parent pointers is unavailable (for finding theparent pointer via DFS).

To see how this is possible, consider a node k that has a rightchild r . Then the left pointer of r must be either a child or a thread

back to k. In the case that r has a left child, that left child must inturn have either a left child of its own or a thread back to k, and soon for all successive left children. So by following the chain of leftpointers from r , we will eventually find a thread pointing back to k.The situation is symmetrically similar when q is the left child of p —wecan follow q's right children to a thread pointing ahead to p.

A threaded tree, with thespecial threading links shown by dashed arrows

http://csetube.weebly.com/


Recommended