Professional Documents
Culture Documents
Objective:
Identifying the properties of a tree.
Identifying the characteristics and basic
operations of a tree.
Applying tree operations in problem
solving.
Applying the concept of trees in
programming.
1 12/17/21
Why tree
4
Struktur Data & Algoritma
Definition of tree
Level 1
B C
D E F G Level 2
H I Level 3
Figure 1
Struktur Data & Algoritma
Root node
A Root node
B C
D E F G
H I Level 3
B C Parent to E,F,G
D E F G
H I Level 3
B C B,C children to A
child to B D E F G
H I Level 3
B C
D E F G
Leaf nodes
H I
Non-leaf nodes
Non-leaf nodes B C
D E F G
H I
D E F G
E,F,G are siblings
B C
D E F G
H I
B C
D E F G
H I
B C
D E F G
H I
A
branches
B C
D E F G
H I Level 3
Level 1
B C
D E F G Level 2
H I Level 3
A Level 0
Level 1
B C
D E F G Level 2
H I Level 3
B C
H I
Tree height =
Degree of node 7 =
Level 1 nodes =
Parent of node 8 =
Child(s) of node 5 =
Leaf nodes =
Sibling of node 6 =
Ancestor(s) of node 10 =
Descendent(s) of node 4 =
Subtree of node 2 =
null tree
(e)
L R
E
6 9
O N
Preorder: EON, 2 7
Inorder: OEN,
Postorder: ONE Preorder : 8 6 2 7 9
Inorder : 2 6 7 8 9
Postorder : 2 7 6 9 8
Struktur Data & Algoritma
Exercise
2
a *
3
b c
4 5
Preorder : - a * b c Preorder : 1 2 3 4 5
Inorder : a – b * c Inorder : 1 4 3 5 2
Postorder : a b c * - Postorder : 4 5 3 2 1
A E
B C F
Breadth First Traversal : D A E B C F
Struktur Data & Algoritma
Binary Search Trees
17 17
10 26 10 26
6 14 20 34 6 14 15 34
11 31 37 11 31 37
7 97
6 11 16 72
30 48 41
39 31 90
F J
A H K
Delete node H.
G
G
F J
F J
A K
A H K
E
E
C
C
G
G
F J
F J
A H K
E H K
E
C
C
F J F J
A H K A K
E E
C C
#include <iostream>
using namespace std;
#include "Tree.h"
void main ( )
{
Tree <int> BST;
int itemI, itemD, itemR;
bool found = true;
cout << “Enter data, 0 to stop : " << endl;
cin >> itemI;
while ( itemI != 0 )
{
BST.InsertItem ( itemI );
cin >> itemI;
}
}//close main()
Data found
10 20 35 46 55 61
a b x a b
a or b sin x (a * b) - c
a *
b c
b c
a +
b c
* d
a +
b c
Make sure that the expression order does not change when
read from the tree.
Example 2
(c+d+a*b)*(e+f)
Example 3
- a + (( c * b) / (a + 5)) - d
Example 4
(- b + ( b | 2 - 4 * a * c ) | 0.5) / ( 2 * a )
In a balanced binary tree, the right subtree and the left subtree
has the same height.
An AVL tree is a binary search tree in which the heights of the
left and right subtree are same or differ by 1.
The AVL tree uses the concept of balance factor.
Equal height the heights of the left and right subtrees are
same.
Left higher the left subtree has a greater height than than the
right subtree with a balance factor of 1.
Right higher the left subtree has a lesser height than than the
right subtree with a balance factor of -1.
2 1
0 8
1
8 1 2
2 0
1
2
1 2
1 1
2 8
1
8 1 2
2 0
2
1 0 2
1 1
2 2 8
4 8
4 1
2
4
8
1 2 3
1 1
2 2 1
8
4 1
4 8 1 4
2 4
1 4
1 8 2 4 3
1 1
8 8
1 1 2
2 2 0
1 2
7 97
6 11 16 72
30 48 41
39 31 90