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
Electronic Data Processing
DATA as The Input and INFORMATION as The Output
Computer Architecture
Memory System
Program C++
#include <conio.h>
#include <iostream.h>
#include <math.h>
main()
{
int keliling, panjang, lebar;
cout <<"Panjang : "; cin >> panjang;
cout <<"Lebar : "; cin >> lebar;
keliling = (2 * panjang) + ( 2 * lebar)
cout <<"\nKeliling : " << keliling;
cout <<"\n";
getch();
}
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 {
char name[10];
int age;
int salary;
} person;
strcpy(person.name, james);
person.age=10;
person.salary=3000;
Person
Nam e
Age
Salary
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
HEAD
A
NODE
CURRENT
B
Insertion
A
X
A
B
3
X
A
Deletion
A
D
2
C
A
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
link to the previous node
link to the next node
next
elem
header
nodes/positions
node
trailer
Insertion
A
p
B
p
B
q
X
q
X
Deletion
A
p
D
A
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.
Applications:
ComputersRUs
Organization charts
File systems
Programming environments
US
Europe
Sales
Manufacturing
International
Asia
Laptops
Canada
Desktops
R&D
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,
grandchild, grand-grandchild, etc.
Depth of a node: number of ancestors
Height of a tree: maximum depth of any
B
node
Degree of a node: the number of its
children
E
Degree of a tree: the maximum number
of its node.
Subtree: tree consisting of a node and
I
its descendants
subtree
Tree Property
Property
Value
Number of nodes
Height
Root Node
Leaves
Interior nodes
Ancestors of H
Descendants of B
Siblings of E
Right subtree of A
Degree of this tree
A
C
G
H
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 subtrees
How many link fields are needed in
such a representation?
Data
Link 1
Link 2
Link n
Tree
Every tree node:
object
children
useful information
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
A
C
F
E
Left Child, Right Sibling Representation
Data
Left
Child
Right
Sibling
Tree Traversal
Two main methods:
Preorder
Postorder
Preorder
visit the root
traverse in preorder
the children (subtrees)
Postorder:
traverse in postorder
visit the root
the children (subtrees)
Preorder Traversal
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
1
Algorithm preOrder(v)
visit(v)
for each child w of v
preorder (w)
Become Rich
1. Motivations
9
2. Methods
1.1 Enjoy
Life
1.2 Help
Poor Friends
6
2.1 Get a
CS PhD
7
2.2 Start a
Web Site
3. Success Stories
8
2.3 Acquired
by Google
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
9
Algorithm postOrder(v)
for each child w of v
postOrder (w)
visit(v)
cs16/
homeworks/
todo.txt
1K
programs/
h1c.doc
3K
h1nc.doc
2K
4
DDR.java
10K
5
Stocks.java
25K
6
Robot.java
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?
No
Yes
How about coffee?
On expense account?
Yes
No
Yes
No
Starbucks
Spikes
Al Forno
Caf Paragon
Binary Tree
A binary tree is a tree with the
following properties:
Applications:
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
arithmetic expressions
decision processes
searching
A
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
Examples of the Binary Tree
Complete Binary Tree
Skewed Binary Tree
A
B
C
3
D
E
Differences Between A Tree and A Binary Tree
The subtrees of a binary tree are ordered; those
of a tree are not ordered.
A
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
A
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
a
3
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
i
k +1
2
2
-1
i 0
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.
4
8
5
9
10
11
12
7
13
14
15
Node Number Properties
1
4
8
5
9
10
11
12
7
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
4
8
10
11
12
13
7
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
4
8
5
9
10
11
12
7
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
2
4
8
Complete binary tree
10
11
12 13 14 15
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
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))
6
2
4
3
7
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
1
0
2
3
3
G1
complete graph
V(G1)={0,1,2,3}
V(G2)={0,1,2,3,4,5,6}
V(G3)={0,1,2}
4
G2
incomplete graph
2
G3
E(G1)={(0,1),(0,2),(0,3),(1,2),(1,3),(2,3)}
E(G2)={(0,1),(0,2),(1,3),(1,4),(2,5),(2,6)}
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
1
2
3
G1
(i)
(ii)
(iii)
(a) Some of the subgraph of G1
(iv)
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
1
0
2
3
3
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
degree
3
0
3 1
2 3
3
G1
directed graph
in-degree
out-degree
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
0
0
1
G
1
undirected: n2/2
directed: n2
G3
2
0
1
0
0
3
1 1 1
0 1 1
1 0 1
1 1 0
0
1
1
0
G2
symmetric
0
1
0
0
0
0
1
0
0
1
0
0
0
1
0
0
1
0
0
0
0
1
1
0
0
0
0
0
0
0
0
0
1
0
0
0
0
0
1
0
1
0
0
0
0
0
1
0
0
0
0
0
0
1
Adjacency lists
N linked list
0
0
2
G1
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 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
Kruskals
Edge by edge
Prims
algorithm
algorithm
Span out from one vertex
0
2
1
1
3
10
12
14
16
18
22
25
28
0 28 1
5
25
14
16
24
18 12
4
22
2
0
10
4 24 6
4
Examples for Kruskals Algorithm
10
5
4
Examples for Kruskals Algorithm
0 10 5
2 12 3
1 14 6
18
3 22 4
4 24 6
4 25 5
0 28 1
1 16 2
10
10
14
14
2
12
4
3
2
12
4
3
10
2
12
4
3
Examples for Kruskals Algorithm
0 10 5
2 12 3
1 14 6
1 16 2
10
3 18 6
3 22 4
4
24
4 25 5
0 28 1
14
16
22
14
16
25
12
10
12
4
22
3
cycle
cost = 10 +25+22+12+16+14
28
10
5
25
1
6
5
25
0
10
1
6
16
18 12
22
14
24
10
Examples for Prims
Algorithm
10
5
25
22
28
10
5
25
14
16
24
18 12
4
22
10
Examples for Prims
Algorithm
10
25
2
12
4
22
5
25
2
12
4
22
16
14
16
10
5
25
2
12
4
22