Data Structures
KRISNA ADIYARTA
PASCA SARJANA (MAGISTER KOMPUTER)UNIVERSITAS BUDI LUHUR
JAKARTA
Data Structures and Algorithms in C++, Second Edition by Adam Drozdek, published by Brooks/Cole Thomson Learning, 2001, ISBN 0-534-37597-9
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
Program C++
#include #include #include main(){int keliling, panjang, lebar;cout panjang;cout lebar;keliling = (2 * panjang) + ( 2 * lebar)cout
Primitive (Atomic) Structures
Primitive (or Basic) Data Types User-Defined Ordinal Types Character String Types Pointer Types
Primitive Data Types
Number IntegerFloating-Point
Boolean CharacterASCII, ISO-8859-x, JIS, UNICODE
User-Defined Ordinal Type
Range of possible values mapped to positive integers
ExamplesEnumeration 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 {char name[10];int age;int salary;} person;
strcpy(person.name, james);person.age=10;person.salary=3000;
Name SalaryAge
Person
Array Structure
A collection of pairs 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 typesBounds checking?Subscript dimension, subscript orderSize 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 storesData itemLink to the next node
next
Data item NODE
A B C D
HEAD CURRENT
Insertion
A B C
X
A B
X
C
1
2
3
A B CX
Deletion
C
A B D
2
1
A B D CX
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 link to the previous node link to the next node
prev next
elem node
trailerheader nodes/positions
Insertion
A B X C
A B C
A B Cp
Xq
p q
Deletion
A B C D
A B C
Dp
A B C
Stack
Stores a set of elements in a particular order Stack principle: LAST IN FIRST OUT It means: the last element inserted is the first one
to be removed Basic Algorithm : Push & Pop
BA
DCBA
CBA
DCBA
EDCBAtop
toptop
toptop
A
Queue
Stores a set of elements in a particular order Queue principle: FIRST IN FIRST OUT It means: the first element inserted is the first
one to be removed Basic Algorithm : Enqueue & Dequeue
ABA
CBA
DCBA
DCBrear
front
rearfront
rear
front
rear
front
rear
front
Tree
ComputersRUs
Sales R&DManufacturing
Laptops DesktopsUS International
Europe Asia Canada
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.
Applications: Organization charts File systems Programming environments
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.
subtree
A
B DC
G HE F
I J K
Descendant of a node: child, grandchild, grand-grandchild, etc.
Depth of a node: number of ancestors Height of a tree: maximum depth of any
node Degree of a node: the number of its
children Degree of a tree: the maximum number
of its node. Subtree: tree consisting of a node and
its descendants
Tree Property
A
B C
D
G
E F
IH
Property ValueNumber of nodesHeightRoot NodeLeavesInterior nodesAncestors of HDescendants of BSiblings of ERight subtree of ADegree of this tree
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
Data Link 1 Link 2 Link n
How many link fields are needed in such a representation?
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 Parent node Sequence of
children nodes
B
A D F
C
E
B
DA
C E
F
Left Child, Right Sibling Representation
Data
Left Child
Right Sibling A
B C D
IHGFE
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
Algorithm preOrder(v)visit(v)for each child w of v
preorder (w)
A traversal visits the nodes of a tree in a systematic manner
In a preorder traversal, a node is visited before its descendants
Application: print a structured document
Become Rich
1. Motivations 3. Success Stories2. Methods
2.1 Get a CS PhD
2.2 Start a Web Site
1.1 Enjoy Life
1.2 Help Poor Friends
2.3 Acquired by Google
1
2
3
5
4 6 7 8
9
Postorder Traversal In a postorder traversal, a
node is visited after its descendants
Application: compute space used by files in a directory and its subdirectories
Algorithm postOrder(v)for each child w of v
postOrder (w)visit(v)
cs16/
homeworks/ todo.txt1Kprograms/
DDR.java10K
Stocks.java25K
h1c.doc3K
h1nc.doc2K
Robot.java20K
9
3
1
7
2 4 5 6
8
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?
How about coffee? On expense account?
Starbucks Spikes Al Forno Caf Paragon
Yes No
Yes No Yes No
Binary Tree
A binary tree is a tree with the following properties: Each internal node has at most two
children (degree of two) The children of a node are an
ordered pair
We call the children of an internal node left child and right child
Alternative recursive definition: a binary tree is either a tree consisting of a single node,
OR a tree whose root has an ordered
pair of children, each of which is a binary tree
Applications: arithmetic expressions decision processes searching
A
B C
F GD E
H I
Examples of the Binary Tree
A
B C
GE
I
D
H
F
Complete Binary Tree
1
2
3
4
A
B
A
B
Skewed Binary Tree
E
C
D
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
B
A
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 Left child node Right child node
B
DA
C E
B
A D
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))
+
2a 1
3 b
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.
122 10
= += kki
i
Full Binary Tree A full binary tree of a given height k has 2k+11
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.
1
2 3
4 5 6 7
8 9 10 11 12 13 14 15
Node Number Properties
Parent of node i is node i / 2, unless i = 1. Node 1 is the root and has no parent.
1
2 3
4 5 6 7
8 9 10 11 12 13 14 15
Node Number Properties
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.
1
2 3
4 5 6 7
8 9 10 11 12 13 14 15
Node Number Properties
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.
1
2 3
4 5 6 7
8 9 10 11 12 13 14 15
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
2 3
75
11
4
10
6
98 15141312
Full binary tree of depth 3
1
2 3
75
9
4
8
6
Complete binary tree
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
node is visited after its left subtree and before its right subtree
Algorithm inOrder(v)if isInternal (v)
inOrder (leftChild (v))visit(v)if isInternal (v)
inOrder (rightChild (v))
3
1
2
5
6
7 9
8
4
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. represents an edge, v0 is the tail and v1 is the
head
Graph0
1 2
3
0
1
2
0
1 2
3 4 5 6G1
G2 G3V(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)={,,}
complete undirected graph: n(n-1)/2 edgescomplete directed graph: n(n-1) edges
complete graph incomplete graph
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
Adjacent and Incident
If (v0, v1) is an edge in an undirected graph, v0 and v1 are adjacentThe edge (v0, v1) is incident on vertices v0 and v1
If is an edge in a directed graphv0 is adjacent to v1, and v1 is adjacent from v0The edge is incident on v0 and v1
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
1 2 3
1 2 0
1 2
3
(i) (ii) (iii) (iv)(a) Some of the subgraph of G1
0
1 2
3
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
0
1 2
3
0
1 2
3 4 5 6G1
G2
connected
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
edgesthat have v as the head
the out-degree of a vertex v is the number of edgesthat have v as the tail
if di is the degree of a vertex i in a graph G with n vertices and e edges, the number of edges is
e d in
=( ) /0
1
2
Degree
undirected graphdegree
G1
30
1 2
333
3
directed graphin-degreeout-degree
0
1
2
in:1, out: 1
in: 1, out: 2
in: 1, out: 0
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
0111
1011
1101
1110
010
100
010
01100000
10010000
10010000
01100000
00000100
00001010
00000101
00000010
G1 G2
0
1 2
3
0
1
2
1
0
2
3
4
5
6
7
symmetricundirected: n2/2
directed: n2
G3
Adjacency lists N linked list
1
2 0
3 1 2
2 3 0
1 3 0
2102
1
0
G1
0
1 2
3
G2
Graph Operations Traversal
Given G=(V,E) and vertex v, find all wV, such that w connects v.Depth First Search (DFS)
preorder tree traversalBreadth First Search (BFS)
level order tree traversal Spanning Trees
Graph Operations
depth first search: v0, v1, v3, v7, v4, v5, v2, v6breadth 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 treeWhen dfs is used, the resulting spanning tree is
known as a depth first spanning treeWhen 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 methodKruskals algorithm
Edge by edgePrims algorithm
Span out from one vertexSollins algorithm
Hint: Construct from connected components Leave as a homework
01
2
34
5 6
0
1
2
34
5 6
28
16
121824
22
25
1014
0
1
2
34
5 6
10
0 5
2 3
1 6
1 2
3 6
3 4
4 6
4 5
0 1
10
12
14
16
18
22
24
25
28
Examples for Kruskals Algorithm
Examples for Kruskals Algorithm
0
1
2
34
5 6
10
12
0
1
2
34
5 6
10
12
14
0
1
2
34
5 6
10
12
14
0 5
2 3
1 6
1 2
3 6
3 4
4 6
4 5
0 1
10
12
14
16
18
22
24
25
28
Examples for Kruskals Algorithm0 5
2 3
1 6
1 2
3 6
3 4
4 6
4 5
0 1
10
12
14
16
18
22
24
25
28
0
1
2
34
5 6
10
12
14 16
22
0
1
2
34
5 6
10
12
14 16
22
25
cycle cost = 10 +25+22+12+16+14
Examples for Prims Algorithm
0
1
2
34
5 6
0
1
2
34
5 6
10
0
1
2
34
5 6
10
10
25 25
22
0
1
2
34
5 6
28
16
121824
22
25
1014
Examples for Sollins Algorithm
0
1
2
34
5 6
10
22
12
1614
0
1
2
34
5 6
10
22
12
14
0
1
2
34
5 6
0
1
2
34
5 6
28
16
121824
22
25
1014
25
vertex edge0 0 -- 10 --> 5, 0 -- 28 --> 11 1 -- 14 --> 6, 1-- 16 --> 2, 1 -- 28 --> 02 2 -- 12 --> 3, 2 -- 16 --> 13 3 -- 12 --> 2, 3 -- 18 --> 6, 3 -- 22 --> 44 4 -- 22 --> 3, 4 -- 24 --> 6, 5 -- 25 --> 55 5 -- 10 --> 0, 5 -- 25 --> 46 6 -- 14 --> 1, 6 -- 18 --> 3, 6 -- 24 --> 4