05-Tree-01 - Google Drive

Download Report

Transcript 05-Tree-01 - Google Drive

179
struktur
Peng-alokasi-an
memory
Stack
linear
Queue
Array
(statis)
Tree
non
linear
Graph
LinkedList
(dinamis)
Satu
dimensi
Stack
Queue
Tree
Dua
dimensi
Graph
Linear
Stack
Queue
Non
Linear
Tree
Graph
5.1. Tree,
Gambar-5.1
A
B
E
J
Contoh sebuah TREE
C
F
K
D
G
L
M-ary Tree dan Binary Tree
H
M
I
N
179
Gambar-5.1
A
B
E
C
F
J
K
D
G
L
a. Tree
dan Graph
T =  G
H
M
I
N
Tree
merupakan bagian dari
Graph.
179
v0
Gambar-5.1
e0
v1 B
A
e1
e2
v2 C
b. Simpul (Vertex, Node),
dan Busur (Edge, Arc)
v3 D
T = ( V,E)
E
F
G
H
I
e12
J
K
L
M
N
v13
V = { v0, v1, v2, . . . . . . , v13 }
E = { e0, e1, e2, . . . . . , e12 }
179
Gambar-5.1
B
E
C
F
J
c. Superordinat dan
Subordinat,
A
K
G
L
Father dan Son
D
H
M
I
N
Untuk contoh pohon diatas :
Simpul B
merupakan superordinat
simpul E dan F.
Simpul E dan F mempunyai
superordinat yang sama
yaitu simpul B.
Simpul B mempunyai 2
subordinat yaitu
simpul E dan simpul F
179
Root
Gambar-5.1
B
E
d. Akar (Root)
dan Daun (Leaf/Leaves)
A
C
F
D
G
Akar =
H
I
Simpul yang tak
mempunya superordinat.
Daun =
J
K
L
M
N
simpul yang tak
mempunyai subordinat
Dari pohon diatas :
Akar = Simpul : A.
Daun = Simpul : C, E, G, I, J, K, L, M, N
179
Root
Gambar-5.1
C
F
J
0
A
B
E
Level
K
D
G
L
1
H
M
e. Level
dan Depth
I
2
N
3
Depth = 3
Akar berada di Level : 0
Subordinat Level i adalah Level : i+1
179
Tingkat
dan
Kedalaman
Gambar-5.1
A
B
E
C
F
J
f. Degree (Derajat) Simpul
dan Degree Pohon
K
D
G
L
H
M
I
N
Degree
Simpul A = 3
B=2
C=0
Simpul daun, degree = 0
Degree Pohon :
Untuk pohon ini degree pohon dapat diduga = 3,
Tapi masih mungkin 4, 5 dan seterusnya
Yang pasti bukan = 2
179
Pohon M-Ary
dan
Pohon Binary
179
Pohon M-Ary
M=1
2
3
4
5
dan setreusnya
M menyatakan
derajat pohon
Khusus untuk
M=2,
Disebut
Binary Tree
(Pohon Biner)
179
Contoh sebuah TREE
dengan derajat = 3
(3-Ary Tree)
A
A
B
D
C
E
F
G
B
H
C
I
J
K
Gambar-5.2 b
Gambar-5.2 a
179
D
Sebuah simpul pohon M-ary dimana M=3
digambarkan dengan Linked-List
Strukturnya dapat dibuat dengan :
INFO
Link1
Link2
Link3
typedef struct Node {
int INFO;
struct Node *Link1;
struct Node *Link2;
struct Node *Link3;
};
typedef struct Node Simpul;
Gambar-5.3
179
Pohon Binary
179
Sebuah simpul
Pohon Biner digambarkan dalam bentuk Linked-List
Strukturnya dapat dibuat dengan :
INFO
LEFT
typedef struct Node {
struct Node *LEFT;
int INFO;
struct Node *RIGHT;
};
typedef struct Node Simpul;
RIGHT
Gambar-5.4
atau
typedef struct Node {
int INFO;
struct Node *LEFT;
struct Node *RIGHT;
};
typedef struct Node Simpul;
typedef struct Node {
struct Node *LEFT;
int INFO;
struct Node *RIGHT;
};
typedef struct Node Simpul;
Simpul *First, *Last, *P, *Q, *Root;
// membuat Simpul Akar
P = (Simpul * …………………);
P->INFO = X; //misal X = 25
Root = P;
P->Left = NULL;
P->Right = NULL;
P
Root
25
Contoh sebuah Pohon Biner
A
A
B
D
E
G
C
B
C
D
F
H
I
A
F
E
G
H
I
H
H
Gambar-5.5 b
Gambar-5.5 a
Gambar-5.5 c
182
h. Link,
Null-Link
dan
Bukan Null-Link
Contoh Soal.
Soal-1. Sebuah pohon M-ary dengan 10 buah simpul
Bila M = 3 , maka Ditanya berapa jumlah Null-Link:
A
B
C
1
3
E
6
D
5
F
9
G
11
H
14
I
J
19
16
Gambar 5.6 a
179
21
A
1
B
2
3
C
4
5
D
7
E
9
F
11
G
13
Gambar-5.6 b
H
15
Pohon 3-ary Skewed Right
(Skewed to the right)
J
17
J
19
179
21
A
B
C
1
E
6
A
F
9
I
1
D
3
G
11
16
3
19
C
4
5
5
H
14
B
2
E
7
9
J
F
11
G
13
21
H
15
Jawab :
J
J
17
Pohon dengan M = 3
Jumlah simpul 10, jadi : n = 10
Jumlah Null-Link =
=
=
=
D
19
n * (M-1) + 1
10 * (3-1) + 1
10 *
2
+ 1
21
179
21
Soal-2.
Sebuah Pohon Biner dengan 10 buah simpul
Ditanya berapa jumlah Null-Link:
A
A
1
B
2
C
2
E
3
5
F
4
G
6
C
3
1
D
B
4
H
I
9
H
10
E
5
8
7
D
F
6
G
7
11
H
8
I
9
J
10
11
Soal-2.
Sebuah Pohon Biner dengan 10 buah simpul
Ditanya berapa jumlah Null-Link:
A
Jawab :
B
Pohon Biner, berarti M = 2
Jumlah simpul 10, jadi : n = 10
C
1
D
2
E
3
4
G
5
F
6
H
I
8
7
9
H
10
11
Jumlah Null-Link
= n * ( M - 1 ) + 1
= 10 * ( 2 - 1 ) + 1
= 10 *
1
+ 1
= 11
5.2
Konversi Pohon M-ary ke Pohon Biner
Soal :
Konversikan pohon M-Ary berikut ini menjadi Pohon Biner
Level
0
A
B
E
C
F
J
K
D
G
L
1
H
M
I
N
2
3
Depth = 3
Level
0
A
B
C
Level
1
D
A
0
C
1
B
E
F
J
G
K
L
H
M
2
I
N
E
F
3
J
A
C
D
3
G
K
B
2
D
4
H
L
M
I
N
E
F
J
G
K
L
H
M
I
Gambar-5.10 c
Pohon Biner hasil konversi
dari pohon
M-ary
Gambar-5.10 a
N
Bentuk Transformasi
belum membentuk Pohon Biner
186
5
6