Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
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
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();
}
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
Person
Name
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
Stack
B
top A
C
top B
A
D
top C
B
A
top
E
D
C
B
A
top
D
C
B
A
Queue
rear
front
B
A
C
rear B
front A
D
rear C
B
front A
rear
D
C
front B
rear
front
Tree
Organization charts
File systems
Programming environments
US
Europe
ComputersRUs
Sales
Manufacturing
International
Asia
Laptops
Canada
Desktops
R&D
Tree Terminology
subtree
J
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
B
D
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
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
D
A
C
F
E
Right
Sibling
Tree Traversal
Preorder
visit
the root
traverse in preorder the children (subtrees)
Postorder:
traverse
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
C
3
D
E
A node is represented
by an object storing
Element
Parent node
Left child node
Right child node
B
A
D
C
2
a
3
1
Prove by induction.
k
i
k +1
2
=
2
1
i =0
4
8
5
9
10
11
12
13
7
14
15
4
8
5
9
10
11
12
13
7
14
15
4
8
5
9
10
11
12
13
7
14
15
4
8
5
9
10
11
12
13
7
14
15
1
2
4
8
3
5
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
8
4
1
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
0
1
1
2
2
3
0
1
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
e = (
n 1
di) / 2
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
1 1 1
0 1 1
1 0 1
1 1 0
G
1
undirected: n2/2
directed: n2
G3
2
0
1
0
1
0
0
3
0
1
1
0
G2
symmetric
0
1
0
0
0
0
1
0
0
1
0
0
0
0
1
0
0
1
0
0
0
0
0
1
1
0
0
0
0
0
7
0
0
0
0
0
1
0
0
0
0
0
0
1
0
1
0
0
0
0
0
0
1
0
1
0
0
0
0
0
0
1
0
Adjacency lists
N linked list
0
0
1
1
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
Sollins
algorithm
0
2
1
1
3
3
10
12
14
16
18
22
25
28
0 28 1
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
3 18 6
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 6
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
14
16
24
18 12
4
22
10
0
1
10
25
4
3
10
5
25
4
3
22
28
vertex
0
1
2
3
4
5
6
10
14
16
24
25
18 12
4
22
edge
0 -- 10 --> 5, 0 -- 28 --> 1
1 -- 14 --> 6, 1-- 16 --> 2, 1 -- 28 --> 0
2 -- 12 --> 3, 2 -- 16 --> 1
3 -- 12 --> 2, 3 -- 18 --> 6, 3 -- 22 --> 4
4 -- 22 --> 3, 4 -- 24 --> 6, 5 -- 25 --> 5
5 -- 10 --> 0, 5 -- 25 --> 4
6 -- 14 --> 1, 6 -- 18 --> 3, 6 -- 24 --> 4
0
10
10
14
14
16
25
12
4
3
22
12
4
22