37
DATA STRUCTURES DATA STRUCTURES DEF:DS is a collection of DEF:DS is a collection of organized data that are organized data that are related to each other. related to each other.

Data structures

Embed Size (px)

DESCRIPTION

Introduction to Data Structures.................

Citation preview

Page 1: Data structures

DATA STRUCTURESDATA STRUCTURES

DEF:DS is a collection of DEF:DS is a collection of organized data that are related to organized data that are related to each other.each other.

Page 2: Data structures

DATA STRUCTURES

LINEAR DS NON-LINEAR

STACKS QUEUES LINKED LISTTREES GRAPHS

Page 3: Data structures

TREESTREES

DEF:A TREE IS A SET OF NODES AND LINKSDEF:A TREE IS A SET OF NODES AND LINKS

BINARY TREE:A BINARY TREE IS A FINITE SET OF ELEMENTS BINARY TREE:A BINARY TREE IS A FINITE SET OF ELEMENTS THAT IS EITHER EMPTY OR PARTITIONED INTO 3 DISJOINT THAT IS EITHER EMPTY OR PARTITIONED INTO 3 DISJOINT

SUBSETSSUBSETS.. THE 1THE 1STST SUBSET CONTAINS A SINGLE ELEMENT CALLED AS SUBSET CONTAINS A SINGLE ELEMENT CALLED AS

“ROOT OF THE BINARY TREE”.THE OTHER TWO SUBSETS ARE “ROOT OF THE BINARY TREE”.THE OTHER TWO SUBSETS ARE THEMSELVES TREES CALLED LEFT AND RIGHT SUBTREES OF THEMSELVES TREES CALLED LEFT AND RIGHT SUBTREES OF THE ORIGINAL TREE.THE ORIGINAL TREE.

EACH ELEMENT OF THE BINARY TREE IS CALLED “NODE” OF EACH ELEMENT OF THE BINARY TREE IS CALLED “NODE” OF THE BINARY TREE.THE BINARY TREE.

Page 4: Data structures

A

B C

D E GF

LEFT SUBTREERIGHT SUBTREE

Page 5: Data structures

In the above example A is the root node of In the above example A is the root node of the tree and B is the root of the left sub tree the tree and B is the root of the left sub tree and C is the root of the right subtree.and C is the root of the right subtree.

Then A is called as father of B and C.Then A is called as father of B and C. Where B is the left son of A and C is the Where B is the left son of A and C is the

right son of A.right son of A.

Page 6: Data structures

TREE BASICSTREE BASICS Number of nodesNumber of nodes HeightHeight Root NodeRoot Node LeavesLeaves Interior nodesInterior nodes Number of levelsNumber of levels Ancestors of HAncestors of H Descendants of BDescendants of B Siblings of ESiblings of E Right subtreeRight subtree

Page 7: Data structures
Page 8: Data structures

SIBLINGSIBLING

If N is a node In T that has left sub tree S1, If N is a node In T that has left sub tree S1, and a right sub tree S2, then N is called as and a right sub tree S2, then N is called as the parent of the S1& S2the parent of the S1& S2

So S1 &S2 are called siblings.So S1 &S2 are called siblings.

Page 9: Data structures

LEVEL NUMBERLEVEL NUMBER

Every node in a tree is assigned a level Every node in a tree is assigned a level number.number.

The root is defined at level 0The root is defined at level 0 The left and right child of root node has level The left and right child of root node has level

n0.1 and so on…………….n0.1 and so on…………….

Page 10: Data structures

DEGREEDEGREE

The degree of the node is equal to the numThe degree of the node is equal to the num

ber of children that a node has.ber of children that a node has.

for eg: the degree of root node A is 2for eg: the degree of root node A is 2

Page 11: Data structures

IN DEGREE AND OUT DEGREEIN DEGREE AND OUT DEGREE

In in-degree of a node is the number of In in-degree of a node is the number of edges arriving at that node.edges arriving at that node.

for eg: the root node is the only node for eg: the root node is the only node that has an in-degree 0.that has an in-degree 0.

Similarly the out degree of a node is the Similarly the out degree of a node is the number of edges that leaving the node.number of edges that leaving the node.

Page 12: Data structures

Leaf(or)Leaf Node or Terminal nodeLeaf(or)Leaf Node or Terminal node

A “Node” that has no sons is called as “leaf A “Node” that has no sons is called as “leaf node” node”

In the above fig D,E,F&G are the leaf nodesIn the above fig D,E,F&G are the leaf nodes Where A,B,C are the non leaf nodesWhere A,B,C are the non leaf nodes

A

B C

LEAF NODES

Page 13: Data structures

ANCESTORANCESTOR

A Node ‘n1’ is an ancestor of node ‘n2’ if ‘n1’ A Node ‘n1’ is an ancestor of node ‘n2’ if ‘n1’ is neither father of ‘n2’(or) father of some is neither father of ‘n2’(or) father of some ancestor of ‘n2’.ancestor of ‘n2’. n1

n2n1

B

n2

Page 14: Data structures

In the above fig 1 ‘n1’ is the father of ‘n2’ so In the above fig 1 ‘n1’ is the father of ‘n2’ so directly we call ‘n1 as ancestor of ‘n2’directly we call ‘n1 as ancestor of ‘n2’

In fig2 there is no direct relation ship b/w n1 In fig2 there is no direct relation ship b/w n1 and n2 even though n1 Is the father of B and n2 even though n1 Is the father of B where b is the father of n2 then we call n1 where b is the father of n2 then we call n1 as father of ancestor of ‘n2’ so n1 become as father of ancestor of ‘n2’ so n1 become ancestor of n2ancestor of n2

Page 15: Data structures

DescendentDescendent

A node ‘n2 is descendent of ‘n1’if ‘n2 is son A node ‘n2 is descendent of ‘n1’if ‘n2 is son of ‘n1’of ‘n1’

In the above fig directly n2 is the In the above fig directly n2 is the son of n1.son of n1.In fig2 n2 is the son of ‘b’ where In fig2 n2 is the son of ‘b’ where B is the descendent of n1 the n2 is son of B is the descendent of n1 the n2 is son of

descendent of n1descendent of n1Then n2 is descendent of n1.Then n2 is descendent of n1.

n1

n2

n1

b

n2

Page 16: Data structures

Brother NodesBrother Nodes

Two nodes are said to be brothers if they ae Two nodes are said to be brothers if they ae left and right sons of the same father nodeleft and right sons of the same father node

A

B C

Page 17: Data structures

The direction of travelling from roots to The direction of travelling from roots to leaves is “down” and the travelling from leaves is “down” and the travelling from leaves to root is up.leaves to root is up.

Moving from leaves is known as “climbing”Moving from leaves is known as “climbing” And moving from roots to leaves is also And moving from roots to leaves is also

known as descending.known as descending.

Page 18: Data structures

BINARY TREEBINARY TREE

A binary tree is a non-linear data structure A binary tree is a non-linear data structure which is defined as collection of elements which is defined as collection of elements called nodes.called nodes.

Every node has left pointer and right pointer Every node has left pointer and right pointer and the data element.and the data element.

Every binary tree has a root node which is Every binary tree has a root node which is the top most node in the tree.the top most node in the tree.

If root is equal to null then the tree is empty.If root is equal to null then the tree is empty.

Page 19: Data structures

1

2 3

5 64 7

1089

11 12

Page 20: Data structures

In the above figure, node-2 is the left In the above figure, node-2 is the left successor and node-3 is the right succesorsuccessor and node-3 is the right succesor

Note that the left sub-tree of the root node Note that the left sub-tree of the root node consists of nodes-2,4,5,8,and 9. and right consists of nodes-2,4,5,8,and 9. and right sub-tree consists of nodes-3,6,7,10,11,12sub-tree consists of nodes-3,6,7,10,11,12

The leaf nodes are 5,8,9,10,11,12.The leaf nodes are 5,8,9,10,11,12.

Page 21: Data structures

Properties of BinaryTreeProperties of BinaryTree Property1:A tree with ‘n’ nodes has exactly n-1 Property1:A tree with ‘n’ nodes has exactly n-1

edges or branches.edges or branches. Property2:In a tree every node except the root Property2:In a tree every node except the root

has exactly 1 parenthas exactly 1 parent Property3:There is exactly one path connecting Property3:There is exactly one path connecting

any 2 nodes in a tree.any 2 nodes in a tree. Property4:the max no of nodes in a binary tree Property4:the max no of nodes in a binary tree

of height k is 2of height k is 2k+1k+1 -1 (k>=0) -1 (k>=0) Property5:Btree with n internal nodes has n+1 Property5:Btree with n internal nodes has n+1

external nodes.external nodes.

Page 22: Data structures

Strict Binary TreeStrict Binary Tree

If every non leaf node in the binary tree If every non leaf node in the binary tree having non empty left and right sub trees having non empty left and right sub trees then it is known as strict binary treesthen it is known as strict binary trees

In a binary tree if every non-leaf node In a binary tree if every non-leaf node having both left and right sons then it is having both left and right sons then it is called as strict binary tree.called as strict binary tree.

Page 23: Data structures

A

B C

F GE H

F

C

D E

B

A

Page 24: Data structures

In the above fig1 is a strict binary tree where In the above fig1 is a strict binary tree where as fig2 is not because the non leaf node D as fig2 is not because the non leaf node D having only leftson ‘F’ it doesnot have right having only leftson ‘F’ it doesnot have right son.son.

So in a strict binary tree with n leaves totally So in a strict binary tree with n leaves totally we have (2n-1) nodes.we have (2n-1) nodes.

Ie from the above fig we have 4 leaf nodes Ie from the above fig we have 4 leaf nodes the n we have (2*4-1)=7the n we have (2*4-1)=7

Page 25: Data structures

Level of the node:Level of the node:

The level of the node in a binary tree is The level of the node in a binary tree is defined as follows.defined as follows.

The root of the binary tree always having The root of the binary tree always having level ’0’ and the level of any other node is level ’0’ and the level of any other node is one more that its father node.one more that its father node.

Level of a node=1+its father node levelLevel of a node=1+its father node level

Page 26: Data structures

D E

B

A

E

C

LEVEL 0

LEVEL 1

LEVEL 2

LEVEL3

Page 27: Data structures

DEPTH OF BTDEPTH OF BT

The depth of bt is the max level of any leaf The depth of bt is the max level of any leaf node in that tree.node in that tree. A

B

D

G

F

E

C

LEVEL0

LEVEL1

LEVEL2

LEVEL3

LEVEL4

Page 28: Data structures

LEVEL:4LEVEL:4 DEPTH=4DEPTH=4 Depth always equals to length of longest Depth always equals to length of longest

path from root to leaves path from root to leaves

Page 29: Data structures

Complete BTreeComplete BTree

A complete Btree of Depth “D” is a strict A complete Btree of Depth “D” is a strict Btree whose all leaf nodes must exist at Btree whose all leaf nodes must exist at level”D”.level”D”.

Page 30: Data structures

REPRESENTATION OF B TREE REPRESENTATION OF B TREE USING ARRAYSUSING ARRAYS

Page 31: Data structures

Suppose T is a Binary tree that is complete.Suppose T is a Binary tree that is complete.

there is an efficient way of maintaining T in there is an efficient way of maintaining T in memory called Sequential representation of memory called Sequential representation of T.T.

The representation uses only a single linear The representation uses only a single linear array tree as follows.array tree as follows.

a)The root R of tree T is stored in Tree[i]a)The root R of tree T is stored in Tree[i]

b)If a node N occupies Tree[K],then the left b)If a node N occupies Tree[K],then the left child is stored in Tree[2k+1] and right child child is stored in Tree[2k+1] and right child is stored in Tree[2k+2].is stored in Tree[2k+2].

Page 32: Data structures

CIS 068CIS 068

Complete TreesComplete Trees Storage of complete trees in arrays:Storage of complete trees in arrays:

0

1 2

3

7

4 5 6

8

0 1 2 3 4 5 6 7 8

2k+1, 2k+2k=3

Page 33: Data structures

Sequential Representation

AB--

C----

--D--.E

[1][2][3][4][5][6][7][8][9].[16]

[1][2][3][4][5][6][7][8][9]

A

B

C

D

E

FH

I

A

B

E

C

D

A

B C

GE

I

D

H

F

(1) waste space(2) insertion/deletion problem

Page 34: Data structures

REPRESENTATION OF B TREE REPRESENTATION OF B TREE USING LINKED LISTUSING LINKED LIST

Page 35: Data structures
Page 36: Data structures

Data Structures: A Data Structures: A Pseudocode Approach Pseudocode Approach with Cwith C 3636

Page 37: Data structures

Operations on BTOperations on BT

There are 3 important operations There are 3 important operations on BT.on BT.

InsertionInsertion

DeletionDeletion

Searching Searching