0% found this document useful (0 votes)
78 views64 pages

Data Structure Presentation

Materi struktur data

Uploaded by

Raga Maulana
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
78 views64 pages

Data Structure Presentation

Materi struktur data

Uploaded by

Raga Maulana
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 64

Data Structures

Data Structures and Algorithms in C++, Second Edition by Adam Drozdek, published by Brooks/Cole
Thomson Learning, © 2001, ISBN 0-534-37597-9

KRISNA ADIYARTA
PASCA SARJANA (MAGISTER KOMPUTER)
UNIVERSITAS BUDI LUHUR
JAKARTA
Definitions
 A data structure is a scheme for organizing data in
the memory of a computer.
 The way in which the data is organized affects the
performance of a program for different tasks.
 Computer programmers decide which data
structures to use based on the nature of the data
and the processes that need to be performed on
that data.
 Some of the more commonly used data structures
include : primitive data, arrays, lists, stacks,
queues, trees, graphs
Primitive (Atomic) Structures

 Primitive (or Basic) Data Types


 User-Defined Ordinal Types
 Character String Types
 Pointer Types
Primitive Data Types

 Number
 Integer
 Floating-Point
 Boolean
 Character
 ASCII, ISO-8859-x, JIS, UNICODE
User-Defined Ordinal Type

 Range of possible values mapped to positive


integers
 Examples
 Enumeration types (C, C++, Pascal, Ada)
 Subrange types (Pascal, Modula-2, Ada)
Character String Type

 Design issues:
 Should strings be a special kind of character
array? Or a primitive type?
 Static or dynamic length?
Pointer Type

 Range of values of memory address or “null”


 Provide explicit support for indirect referencing
 Available in: C, C++, Pascal, Ada

int *pi = new int;


float *pf=new float;
*pi =1024;
*pf =3.14;
Record Structure

 Possibly heterogeneous aggregation of named data


elements

struct {
Person
char name[10];
int age;
int salary;
} person;
strcpy(person.name, “james”);
person.age=10; Nam e Salary
Age
person.salary=3000;
Array Structure

 A collection of pairs <index,value>


where index is an ordered set of integers
and are values of some data type that is
constant for the array.
 not all languages require index to be
continuous or contiguous or start at 0 or
1.
 In C arrays are zero based and are
contiguous from 0 to size-1 and can
contain any simple or aggregate data
type
Array Structure

 Homogenous aggregation of data elements


 Constant-time access to elements
 Design issues:
 Subscript types
 Bounds checking?
 Subscript dimension, subscript order
 Size static or dynamic? User-definable?
Single Linked List
 A singly linked list is a concrete data structure
consisting of a series of nodes
 Each node stores next
 Data item
 Link to the next node

Data item NODE

HEAD CURRENT

A B C D 
Insertion

A B C 

X
2
A B C 

3 1
X

A B X C 
Deletion

A B X
C D 

1
A B D 

2
C

A B D 
Double Linked List
 A doubly linked list provides a natural
implementation of the List
 Special trailer and header nodes
 Nodes implement Position and store:
 element prev next
 link to the previous node
 link to the next node
elem node

header nodes/positions trailer


Insertion

A B C

p
A B C
q
X

p q
A B X C
Deletion

A B C D

A B C
p
D

A B C
Tree
 A tree is a finite nonempty set
of elements.
 It is an abstract model of a
hierarchical structure.
 consists of nodes with a
parent-child relation. Computers”R”Us
 Applications:
 Organization charts
 File systems Sales Manufacturing R&D
 Programming environments

US International Laptops Desktops

Europe Asia Canada


Tree Terminology
 Root: node without parent
 Siblings: nodes share the same parent
 Internal node: node with at least one child
 External node (leaf ): node without children
 Ancestors of a node: parent, grandparent, grand-grandparent, etc.
 Descendant of a node: child,
A
grandchild, grand-grandchild, etc.
 Depth of a node: number of ancestors
 Height of a tree: maximum depth of any
B C D
node
 Degree of a node: the number of its
children
E F G H
 Degree of a tree: the maximum number
of its node.
 Subtree: tree consisting of a node and
I J K
its descendants subtree
Tree Property
Property Value A
Number of nodes
Height
B C
Root Node
Leaves
Interior nodes D E F
Ancestors of H
Descendants of B
Siblings of E
Right subtree of A G
Degree of this tree
H I
Intuitive Representation of Tree Node
 List Representation
 ( A ( B ( E ( K, L ), F ), C ( G ), D ( H ( M ), I, J ) ) )
 The root comes first, followed by a list of links to sub-
trees

How many link fields are needed in


such a representation?

Data Link 1 Link 2 … Link n


Tree
 Every tree node:
 object – useful information
 children – pointers to its children

Data

Data   Data  Data  

Data   Data   Data  


A Tree Representation
 A node is represented 
by an object storing
 Element B
 Parent node
 Sequence of  
children nodes
A D F
B

A D F
 
C E
C E
Left Child, Right Sibling Representation

Data
Left Right
Child Sibling A

B C D

E F G H I

J K L
Tree Traversal
 Two main methods:
 Preorder
 Postorder

 Preorder
 visit the root
 traverse in preorder the children (subtrees)

 Postorder:
 traverse in postorder the children (subtrees)
 visit the root
Preorder Traversal
 A traversal visits the nodes of a Algorithm preOrder(v)
tree in a systematic manner visit(v)
 In a preorder traversal, a node is for each child w of v
visited before its descendants preorder (w)
 Application: print a structured
document
1
Become Rich

2 5 9
1. Motivations 2. Methods 3. Success Stories

3 4 6 7 8
1.1 Enjoy 1.2 Help 2.1 Get a 2.2 Start a 2.3 Acquired
Life Poor Friends CS PhD Web Site by Google
Postorder Traversal
 In a postorder traversal, a Algorithm postOrder(v)
node is visited after its for each child w of v
descendants postOrder (w)
 Application: compute space visit(v)
used by files in a directory and
its subdirectories
9
cs16/
8
3 7
todo.txt
homeworks/ programs/
1K

1 2 4 5 6
h1c.doc h1nc.doc DDR.java Stocks.java Robot.java
3K 2K 10K 25K 20K
Decision Tree
 Binary tree associated with a decision process
 internal nodes: questions with yes/no answer
 external nodes: decisions

 Example: dining decision

Want a fast meal?


Yes No

How about coffee? On expense account?


Yes No Yes No

Starbucks Spike’s Al Forno Café Paragon


Binary Tree
 A binary tree is a tree with the  Applications:
following properties:  arithmetic expressions
 Each internal node has at most two
 decision processes
children (degree of two)
 The children of a node are an  searching
ordered pair
A
 We call the children of an internal
node left child and right child
 Alternative recursive definition: a B C
binary tree is either
 a tree consisting of a single node,
OR
 a tree whose root has an ordered D E F G
pair of children, each of which is a
binary tree
H I
Examples of the Binary Tree
Skewed Binary Tree Complete Binary Tree

A 1 A
A

B B 2 B C

C
3 D E F G
D

4 H I
E 5
Differences Between A Tree and A Binary Tree

 The subtrees of a binary tree are ordered; those


of a tree are not ordered.

A A

B B

• Are different when viewed as binary trees.


• Are the same when viewed as trees.
Data Structure for Binary Trees
 A node is represented

by an object storing
 Element
 Parent node B
 Left child node
 Right child node  

B A D

A D    

C E
C E
Arithmetic Expression Tree

 Binary tree associated with an arithmetic expression


 internal nodes: operators
 external nodes: operands
 Example: arithmetic expression tree for the
expression (2  (a - 1) + (3  b))

+
 

2 - 3 b

a 1
Maximum Number of Nodes in a Binary Tree

 The maximum number of nodes on depth i of


a binary tree is 2i, i>=0.
 The maximum nubmer of nodes in a binary
tree of height k is 2k+1-1, k>=0.

Prove by induction.
k

 2
i 0
i
 2 k +1
-1
Full Binary Tree
 A full binary tree of a given height k has 2k+1–1
nodes.

Height 3 full binary tree.


Labeling Nodes In A Full Binary Tree
 Label the nodes 1 through 2k+1 – 1.
 Label by levels from top to bottom.
 Within a level, label from left to right.

2 3

4 5 6 7
8 9 10 11 12 13 14 15
Node Number Properties
1

2 3

4 5 6 7

8 9 10 11 12 13 14 15

 Parent of node i is node i / 2, unless i = 1.


 Node 1 is the root and has no parent.
Node Number Properties
1

2 3

4 5 6 7

8 9 10 11 12 13 14 15

 Left child of node i is node 2i, unless 2i > n,


where n is the number of nodes.
 If 2i > n, node i has no left child.
Node Number Properties
1

2 3

4 5 6 7

8 9 10 11 12 13 14 15

 Right child of node i is node 2i+1, unless 2i+1 >


n, where n is the number of nodes.
 If 2i+1 > n, node i has no right child.
Complete Binary Trees
 A labeled binary tree containing the labels 1 to n with root 1,
branches leading to nodes labeled 2 and 3, branches from these
leading to 4, 5 and 6, 7, respectively, and so on.
 A binary tree with n nodes and level k is complete iff its nodes
correspond to the nodes numbered from 1 to n in the full binary
tree of level k.

1 1

2 2 3
3

4 5 6 7 4 5 6 7

8 9 10 11 12 13 14 15
8 9
Complete binary tree Full binary tree of depth 3
Binary Tree Traversals
 Let l, R, and r stand for moving left, visiting
the node, and moving right.

 There are six possible combinations of traversal


 lRr, lrR, Rlr, Rrl, rRl, rlR

 Adopt convention that we traverse left before


right, only 3 traversals remain
 lRr, lrR, Rlr
 inorder, postorder, preorder
Inorder Traversal
 In an inorder traversal a Algorithm inOrder(v)
node is visited after its left
subtree and before its right if isInternal (v)
subtree inOrder (leftChild (v))
visit(v)
if isInternal (v)
inOrder (rightChild (v))
6

2 8

1 4 7 9

3 5
Graph
 A graph, G=(V, E), consists of two sets:
 a finite set of vertices(V), and
 a finite, possibly empty set of edges(E)
 V(G) and E(G) represent the sets of vertices and edges of G,
respectively
 Undirected graph
 The pairs of vertices representing any edges is unordered
 e.g., (v0, v1) and (v1, v0) represent the same edge
 Directed graph
 Each edge as a directed pair of vertices
 e.g. <v0, v1> represents an edge, v0 is the tail and v1 is the
head
Graph
0 0 0

1 2 1 2
1
3
3 4 5 6
G1 2
G2 incomplete graph
complete graph G3
V(G1)={0,1,2,3} E(G1)={(0,1),(0,2),(0,3),(1,2),(1,3),(2,3)}
V(G2)={0,1,2,3,4,5,6} E(G2)={(0,1),(0,2),(1,3),(1,4),(2,5),(2,6)}
V(G3)={0,1,2} E(G3)={<0,1>,<1,0>,<1,2>}
complete undirected graph: n(n-1)/2 edges
complete directed graph: n(n-1) edges
Complete Graph

 A complete graph is a graph that has the


maximum number of edges
 for undirected graph with n vertices, the
maximum number of edges is n(n-1)/2
 for directed graph with n vertices, the
maximum
number of edges is n(n-1)
 example: G1 is a complete graph
Subgraph and Path
 A subgraph of G is a graph G’ such that V(G’)
is a subset of V(G) and E(G’) is a subset of E(G)
 A path from vertex vp to vertex vq in a graph G,
is a sequence of vertices, vp, vi1, vi2, ..., vin, vq,
such that (vp, vi1), (vi1, vi2), ..., (vin, vq) are
edges
in an undirected graph
 The length of a path is the number of edges on it
Subgraph and Path

0 0 0 1 2 0

1 2 1 2 3 1 2
3
3
G1
(i) (ii) (iii) (iv)
(a) Some of the subgraph of G1
Simple Path and Style
 A simple path is a path in which all vertices,
except possibly the first and the last, are distinct
 A cycle is a simple path in which the first and
the last vertices are the same
 In an undirected graph G, two vertices, v0 and v1,
are connected if there is a path in G from v0 to v1
 An undirected graph is connected if, for every
pair of distinct vertices vi, vj, there is a path
from vi to vj
Simple Path and Style
connected

0 0

1 2 1 2
3
3 4 5 6
G1
G2
tree (acyclic graph)
Connected Component

 A connected component of an undirected graph


is a maximal connected subgraph.
 A tree is a graph that is connected and acyclic.
 A directed graph is strongly connected if there
is a directed path from vi to vj and also
from vj to vi.
 A strongly connected component is a maximal
subgraph that is strongly connected.
Degree
 The degree of a vertex is the number of edges
incident to that vertex
 For directed graph,
 the in-degree of a vertex v is the number of
edges
that have v as the head
 the out-degree of a vertex v is the number of
edges
that have v as the tail
Degree

undirected graph
directed graph
degree
3 in-degree 0 in:1, out: 1
0 out-degree

3 1 2 3 1 in: 1, out: 2

3
3 2 in: 1, out: 0

G1 G2
Adjacency Matrix
 Let G=(V,E) be a graph with n vertices.
 The adjacency matrix of G is a two-dimensional
n by n array, say adj_mat
 If the edge (vi, vj) is in E(G), adj_mat[i][j]=1
 If there is no such edge in E(G), adj_mat[i][j]=0
 The adjacency matrix for an undirected graph is
symmetric; the adjacency matrix for a digraph
need not be symmetric
Adjacency Matrix
0 0 0 4

2 1 5
1 2
1 3 6
3
0 1 1 1 2 G3 7
1 0 1 1 

1 1 0 1 0 1 0 0 1 1 0 0 0 0 0
    1
1 1 1 0 1 0 1
 0 0 1 0 0 0 0


0 0 0
 1 0 0 1 0 0 0 0
 
0 1 1 0 0 0 0 0
G 0 0 0 0 0 1 0 0
1 G2 
0 0 0 0 1 0 1

0
0 0 0 0 0 1 0 1
 
undirected: n2/2 
0 0 0 0 0 0 1 0

symmetric
directed: n2
Adjacency lists
 N linked list

0 3 1 2
1 0
2 3 0
1
2 0 1 2
1 3 0
3
2 0 1 2
G2
G1
Graph Operations
 Traversal
Given G=(V,E) and vertex v, find all wV,
such that w connects v.
 Depth First Search (DFS)
preorder tree traversal
 Breadth First Search (BFS)
level order tree traversal
 Spanning Trees
Graph Operations

depth first search: v0, v1, v3, v7, v4, v5, v2, v6
breadth first search: v0, v1, v2, v3, v4, v5, v6, v7
Weighted Graph
 In Many applications,each edge of a graph has an
associated numerical value, called a weight
 Usually, the edge weights are non negative
integers
 Weight graphs may either directed or undirected
Spanning Trees
 A spanning tree is a minimal subgraph G’, such
that V(G’)=V(G) and G’ is connected
 Weight and MST
 Either dfs or bfs can be used to create a
spanning tree
 When dfs is used, the resulting spanning tree is
known as a depth first spanning tree
 When bfs is used, the resulting spanning tree is
known as a breadth first spanning tree
Minimum-Cost Spanning Trees

 A minimum-cost spanning tree is a spanning


tree of least cost
 Design strategy – greedy method
 Kruskal’s algorithm
 Edge by edge
 Prim’s algorithm
 Span out from one vertex
0 5
10 Examples for Kruskal’s Algorithm
2 3 0
12

1 6 1
14

1 2 5 6 2
16
0 0
3 6 28
4
18
10 1 10 1
3 4 14 16 3
22
5 6 2 5 6 2
4 24 6 24
25 18 12
4 5 4 4
25 22 3 3
0 28 1
0 10 5 Examples for Kruskal’s Algorithm
2 12 3

1 14 6

1 16 2 0 0 0

18 10 1 10 1 10 1
3 6
14 14

3 22 4 5 6 2 5 6 2 5 6 2

12 12 12
4 24 6 4 4 4
3 3 3
4 25 5

0 28 1
0 10 5 Examples for Kruskal’s Algorithm
2 12 3

1 14 6
0 0
1 16 2
10 1 10 1
3 18 6 14 16 14 16

5 6 2 5 6 2
3 22 4
25
12 12
4 4
4 24 6 22
22 3
3
4 25 5 cycle
cost = 10 +25+22+12+16+14

0 28 1
0 Examples for Prim’s
28
10 1
Algorithm
14 16

5 6 2
24
25
18 12
4
22 3
0 0 0
10 1 10 1 10 1

5 6 2 5 6 2 5 6 2
25 25
4 4 4
3 22 3
3
0 Examples for Prim’s
28
10 1
Algorithm
14 16

5 6 2
24
25
18 12
4
0 22 3 0 0
10 1 10 1 10 1
16 14 16

5 6 2 5 6 2 5 6 2
25 25 25
12 12 12
4 4 4
22 22 22 3
3 3

You might also like