Data Structure
Data Structure
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
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
Number
Integer
Floating-Point
Boolean
Character
ASCII, ISO-8859-x, JIS, UNICODE
Design issues:
Should strings be a special kind of character
array? Or a primitive type?
Static or dynamic length?
Pointer Type
Record Structure
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
Array Structure
HEAD
A
NODE
CURRENT
B
Insertion
A
X
A
B
3
X
A
Deletion
A
D
2
C
A
next
elem
header
nodes/positions
node
trailer
Insertion
A
p
B
p
B
q
X
q
X
Deletion
A
p
D
A
Tree
ComputersRUs
Organization charts
File systems
Programming environments
US
Europe
Sales
Manufacturing
International
Asia
Laptops
Canada
Desktops
R&D
Tree Terminology
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
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
Link 1
Link 2
Link n
Tree
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
Right
Sibling
Tree Traversal
Preorder
visit the root
traverse in preorder
Postorder:
traverse in postorder
visit the root
Preorder Traversal
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
Yes
On expense account?
Yes
No
Yes
No
Starbucks
Spikes
Al Forno
Caf Paragon
Binary Tree
Applications:
arithmetic expressions
decision processes
searching
A
A
B
C
3
D
E
A node is represented
by an object storing
Element
Parent node
Left child node
Right child node
B
A
2
a
3
1
Prove by induction.
k
i
k +1
2
2
-1
i 0
4
8
5
9
10
11
12
7
13
14
15
4
8
5
9
10
11
12
7
13
14
15
4
8
10
11
12
13
7
14
15
4
8
5
9
10
11
12
7
13
14
15
2
4
8
10
11
12 13 14 15
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
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 Graph
2
3
G1
(i)
(ii)
(iii)
(a) Some of the subgraph of G1
(iv)
0
1
0
2
3
3
G1
G2
tree (acyclic graph)
Connected Component
Degree
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
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
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
Spanning Trees
Kruskals
Edge by edge
Prims
algorithm
algorithm
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
10
5
4
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
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
10
5
25
22
28
10
5
25
14
16
24
18 12
4
22
10
10
25
2
12
4
22
5
25
2
12
4
22
16
14
16
10
5
25
2
12
4
22