You are on page 1of 81

TREES

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

 In linked lists, most operations like insert,


delete and find take O(N).
 In contrast, trees take O(log N) on an
average for most operations.
 An common application of trees is in storing
the directory structure of computer file
systems.

Struktur Data & Algoritma


Introduction

 The tree is a nonlinear data structure used to store data


in hierarchical form.
 Trees can be used to store arithmetic expressions, store
data in hierarchical form such as organization chart and
also used in the field of artificial intelligence to store a
particular data.
 It is suitable to store big and dynamic data to enable fast
access to data.
 There are a variety of trees, such as general tree, binary
tree, binary search tree, expression tree and AVL tree.
 Examples of application that usually uses tree structure
are information retrieval and file management system.

Struktur Data & Algoritma


Tree: Definitions

 A tree is a collection of zero or more nodes.


 A tree with zero nodes is called an empty tree.
 Every tree has a special or distinguished node called the root.
 The root again can have zero or more nodes under it. These
nodes are called the subtrees of the root.
 The root of each subtree under the tree’s root ‘r’ is a called the
child of the root ‘r’.
 ‘r’ is the called the parent of each child of ‘r’.
 The definition of trees is recursive: The subtrees of the root
again have subtrees and so on.

4
Struktur Data & Algoritma
Definition of tree

 A tree may be a null tree (contains no node) or non-null


tree.
 A non-null tree must have the following characteristics:
1. Has a root node.
2. Every node may have one or more other nodes known as
child node.
3. Each node except the root node must have one parent
node.

Struktur Data & Algoritma


Terminologies in Trees

 There are many terminologies in the tree


structure. For the following terminologies,
refer to Figure 1.
A Level 0

Level 1
B C

D E F G Level 2

H I Level 3
Figure 1
Struktur Data & Algoritma
Root node

 Root node – Node A is the root node

A Root node

B C

D E F G

H I Level 3

Struktur Data & Algoritma


Parent node

 Parent node – Node A is also the parent


node to node B and node C. Node C is the
parent node to node E, F and G.
A Parent to B,C

B C Parent to E,F,G

D E F G

H I Level 3

Struktur Data & Algoritma


Child node

 Child node – Node B and C are children to


node A. Node D is child to node B. Node E, F
and G are children to node C.
A

B C B,C children to A

child to B D E F G

H I Level 3

Struktur Data & Algoritma


Leaf node

 Leaf node – A node that has no child is


called a leaf node or terminal node.
(Examples are node E, F, G, H and I)
A

B C

D E F G

Leaf nodes
H I

Struktur Data & Algoritma


Non-leaf node

 Non-leaf node – Non-leaf nodes are nodes that have


child or subtrees. (Examples are node A, B, C and D)

Non-leaf nodes
Non-leaf nodes B C

D E F G

H I

Struktur Data & Algoritma


Sibling nodes

 Sibling nodes – Siblings are nodes with the same parent.


Node B and C are sibling nodes because they have the same
parent, i.e. node A. Nod H and I are sibling nodes because they
have the same parent, node D.

B,C are siblings


B C

D E F G
E,F,G are siblings

H I H,I are siblings

Struktur Data & Algoritma


Ancestor node

 Ancestor node – Ancestor nodes are nodes in the


path starting from the root node until the particular
node. e.g. the ancestor nodes for node H are nodes
A, B are D.
A

B C

D E F G

H I

Struktur Data & Algoritma


Descendent node

 Descendent node – Descendent nodes are all


nodes below a particular node starting from the node
until the leaf nodes. e.g. the descendent nodes for
node B are node D, H and I.
A

B C

D E F G

H I

Struktur Data & Algoritma


Path
 Path – A path is a sequence of nodes connected by
branches from the root node to the particular node. e.g.
the path to node H is ABDH.

B C

D E F G

H I

Struktur Data & Algoritma


Branch

 Branch – The straight line that


connects each node is called a branch

A
branches

B C

D E F G

H I Level 3

Struktur Data & Algoritma


Level

 Level – Every node in a tree can be put in a


particular level. The root node is always in
Level 0. According to Figure 1, node B and C
are in Level 1.
A Level 0

Level 1
B C

D E F G Level 2

H I Level 3

Struktur Data & Algoritma


Height
 Height – The height or depth of a tree is calculated based
on the farthest level of a node from the root plus 1. The
height of a null tree is -1. Figure 1 has level 0, 1, 2 and 3.
So the height of the tree is 4.

A Level 0

Level 1
B C

D E F G Level 2

H I Level 3

Struktur Data & Algoritma


Degree
 Degree – The degree of a node can be calculated from
the number of child for the node. e.g. the degree of node
C is 3. The degree of a tree is the maximum degree on
the tree, i.e. 3 for our tree in Figure 1.

B C

D E F G The degree of node C is 3

H I

Struktur Data & Algoritma


Subtree

 Subtree– The subtree is a structure of


nodes in a tree that forms another tree.
 The characteristics of a subtree is similar
to a tree where it has the root node,
parent, child, leaf, etc.

Struktur Data & Algoritma


 A subtree can be formed from node B, D, H
and I that form a left subtree for the root
node. Whereas node C, E, F and G form a
right subtree for the root node.
 The subtree can also contain a subtree e.g.
node D, H and I are a subtree for node B.
 Each leaf node is also a subtree.

Struktur Data & Algoritma


Exercise

 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 =

Struktur Data & Algoritma


Binary Trees

 Definition : A null tree OR a tree that contains the


root node and a left subtree or/and a right subtree,
each has no more than 2 children.
 Examples of binary trees

null tree

(a) (b) (c) (d)

Struktur Data & Algoritma


Ctd: examples of binary trees

(e)

(f) (g) (h)

Struktur Data & Algoritma


 Height of binary trees –
– Maximum height, H = Number of Nodes, N
– Minimum height, H = [ log2 N ] + 1
– Minimum number of Node = Height, H
– Maximum number of Node = 2H - 1
 Balanced binary tree –
– Balance Factor = H left – H right
– A binary tree is balanced if the balance factor is -1, 0 or 1;
AND the subtrees of the binary tree are also balanced.

Struktur Data & Algoritma


Complete binary trees –

– A complete binary tree must have the maximum


number of nodes.

Struktur Data & Algoritma


Nearly Complete Binary Trees

– A nearly complete binary tree has the minimum


height for its nodes and the last node is on the left
of the subtree.

Struktur Data & Algoritma


Exercise

 Exercise : Draw all the possible binary trees


with 4 nodes.

Struktur Data & Algoritma


Binary Tree Traversals
 Traversing a tree is to visit all its nodes starting from the root

 One operation that is usually done on binary trees is tree


traversal.

 In this traversal operation, every node in a binary tree will be


visited or traversed at least once.

 There are two types of traversal sequences :-


– Depth First Traversal
– Breadth First Traversal

 There are three types of Breadth First Traversal sequence :-


– Preorder Traversal
– Inorder Traversal
– Postorder Traversal

Struktur Data & Algoritma


N

L R

N – root node L – left subtree node


R – right subtree node
 Preorder : NLR
 Inorder : LNR
 Postorder : LRN

Struktur Data & Algoritma


Traversals

Preorder Traversal – NLR


 The preorder traversal needs the root node to be processed
first, followed by the left subtree nodes, then the right subtree
nodes.
Inorder Traversal – LNR
 The inorder traversal needs the left subtree nodes to be
processed first, followed by the root node, then the right subtree
nodes.
Postorder Traversal – LRN
 The postorder traversal needs the left subtree nodes to be
processed first, followed by the right subtree nodes, then the
root node.

Struktur Data & Algoritma


 The diagrams show examples of traversal.
8

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

 Exercise : What are the results of Preorder, Inorder


and Postorder traversals for the following trees.
1
-

2
a *

3
b c

4 5

Struktur Data & Algoritma


Answer

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

Create a tree from the following statement : 3+8*4/2

Struktur Data & Algoritma


Breadth First Traversals

 Needs all nodes in a level to be processed


first from the root node in Level 0, to the left
subtree to the right subtree in level 1,
proceeding to the deeper levels.
D
Example of traversal for the diagram :-

A E

B C F
Breadth First Traversal : D A E B C F
Struktur Data & Algoritma
Binary Search Trees

 To find an item in a linked list, every node must be examined


one by one sequentally.
 This method is inefficient because it takes a long time.
 A more efficient method is using the binary search tree.
 The binary search tree has the following properties:
– The value of all item in the left subtree < the value of the root
node item.
– The value of all item in the right subtree > the value of the root
node item.
– The left and right subtrees ARE ALSO binary search trees.
– All data in the tree nodes are unique.

Struktur Data & Algoritma


Examples:

17 17

10 26 10 26

6 14 20 34 6 14 15 34

11 31 37 11 31 37

BST Non BST

BST=binary search tree

Struktur Data & Algoritma


Implementation of Binary Search
trees

 Every node in a tree must have at least 3 fields i.e.


the data field, the field that points to the left child,
and the field that points to the right child.

left info right


Pointer to left child data Pointer to right child

Struktur Data & Algoritma


Basic Operations of Binary Search
trees

 Create a binary search tree


 Determine whether a tree is empty
 Find the number of nodes in a tree
 Search for an item in a tree
 Insert an item into a tree
 Delete an item from a tree
 Print the content of a tree

Struktur Data & Algoritma


Creating a Binary Search Tree

 The tree structure and class must be declared when


beginning to create a tree. The variable root that
represents the root must be set to NULL.
 Declaring the data struture of a tree.

template < class T >


class Node
{
public:
T info;
Node *left;
Node *right;
};

Struktur Data & Algoritma


Declaring the tree class

template <class T> bool IsEmpty ( ) const;


class Tree int NumberOfNodes ( ) const;
{ int CountNodes ( Node *root );
private : void InsertItem ( T );
class Node void Insert ( Node*& root, T item );
{ void DeleteItem ( T ) ;
public: void Delete ( Node*& root, T item );
T info; void DeleteNode ( Node *& root );
Node *left; void RetrieveItem ( T&, bool& );
Node *right; void Retrieve(Node *root,T item, bool& found
}; );
Node *root; void GetPredecessor(Node*& root,T& data);
public : void PrintTree ( );
Tree ( ); void Print ( Node* root );
~Tree ( ); };

Struktur Data & Algoritma


Constructor & IsEmpty()

template < class T >


Tree <T> : : Tree ( )
{ <<< Constructor
root = NULL;
}

template < class T >


bool Tree <T> : : IsEmpty ( )
{
IsEmpty() – Determining if ( root = = NULL )
whether the tree is empty return true;
else
return false;
}

Struktur Data & Algoritma


Finding the number of nodes in the
tree

 The process of counting the number of nodes in a


tree is done by calling a function that counts nodes
recursively.
 The recursive function will count the number of
nodes in the left subtree, the number of node in the
right subtree, and the root node.
template < class T >
int Tree <T> : : NumberOfNodes ( ) const int CountNodes ( Node *root )
{ {
return CountNodes ( root ); if ( root = = NULL )
} return 0;
else
return CountNodes ( root -> left )
+ CountNodes ( root -> right ) + 1;
}

Struktur Data & Algoritma


Inserting an item into a tree

 The process of inserting a new node into a tree will


determine whether the tree is empty or not.
 If the tree is not empty, the insert operation will make
the new node as a leaf node in the left or right
subtree.

Struktur Data & Algoritma


template < class T >
void Tree <T> : : InsertItem ( T item )
{
Insert ( root, item );
}

void Insert ( Node*& root, T item )


{
if ( root == NULL )
{
root = new Node;
root -> right = NULL;
root -> left = NULL;
root -> info = item;
}
else if ( item < root -> info )
Insert ( root -> left, item );
else
Insert ( root -> right, item );
}
Struktur Data & Algoritma
Deleting an item from a tree

 Consider three cases for the node to be deleted


– Deleting a node that has no children.
 Assign a NULL value to the parent node pointer field that points to
the node to be deleted.
– Deleting a node that has 1 child.
 Connect the parent node to the child of the node to be deleted.
(Move the child reference to the parent reference).
– Deleting a node that has 2 children.
 Find the node with the biggest value in the left subtree OR the node
with the smallest value in the right subtree of the node to be deleted.
 Replace the node to be deleted with the value of the chosen node.

Struktur Data & Algoritma


12

7 97

6 11 16 72

30 48 41

39 31 90

Struktur Data & Algoritma


template < class T >
template < class T > void Tree<T>::DeleteNode(Node *& root )
void Tree <T>::DeleteItem (T item) {
{ T data;
Delete ( root, item ); Node *temPtr;
} temPtr = root;
if ( root->left == NULL && root->right ==
NULL )
void Delete ( Node*& root, T item ) {
{ root = NULL;
if (root == NULL) return; delete temPtr;
if (item < root->info) }
Delete(root->left, item); else if ( root -> left == NULL )
else if (item > root->info) {
Delete(root->right, item); root = root -> right;
delete temPtr;
else }
DeleteNode(root); else if ( root -> right == NULL )
} {
root = root -> left;
void GetPredecessor(Node*& root,T& data) delete temPtr;
{ }
Node *Ptree = root; else
{
while ( Ptree -> right != NULL )
GetPredecessor ( root-> left, data );
Ptree = Ptree -> right; root -> info = data;
Data = Ptree -> info; Delete ( root -> left, data );
} }
}
Struktur Data & Algoritma
Example of Delete Operation in a
Binary Search Tree

 If the node to be deleted is a leaf node (has


no children), the process is simple.
G

F J

A H K

Delete node H.

Struktur Data & Algoritma


 As H is a leaf node, the properties of a binary search
tree are preserved after node H is deleted (no
change in structure).

G
G
F J
F J
A K
A H K
E
E
C
C

Struktur Data & Algoritma


Deleting a node that has 1 child.

 Example : Delete node A


 The subtree for its child will be pushed up.

G
G
F J
F J
A H K
E H K
E
C
C

Struktur Data & Algoritma


Deleting a node that has 2 children.

 Example : Delete node G


 Move up the smallest node in the right subtree to
replace the node to be deleted.
 In this example, node H is moved up to replace node
G.
G H

F J F J

A H K A K

E E

C C

Struktur Data & Algoritma


Printing the content of a tree

 The printed result is in ascending order.


template < class T >
void Tree <T> : : PrintTree ( )
{
Print ( root );
}

void Print ( Node* root )


{
if ( root ! = NULL )
{
Print ( root -> left );
cout << root -> info;
Print ( root -> right );
}
}

Struktur Data & Algoritma


Searching for an item in a tree

 Steps to search for a node in a binary search tree :-


 The Retrieve function uses a recursive concept that
calls itself until the searched key/item is found or
until the tree has a value of NULL (Key/item not
found).
 If the key is smaller than the node to be compared,
the search will be focused on the left subtree.
 If the key is greater than the node to be compared,
the search will be focused on the right subtree.

Struktur Data & Algoritma


Searching for an item in a tree
template < class T >
void Tree <T> : : RetrieveItem ( T item, bool&
found )
{
Retrieve ( root, item, found );
}

void Insert ( Node *root, T item, bool& found )


{
if ( root = = NULL )
found = false;
else if ( item < root -> info )
Retrieve ( root -> left, item, found );
else if ( item > root -> info )
Retrieve ( root -> right, item, found );
else
found = true;
}

Struktur Data & Algoritma


The Tree Class Implementation

 The tree class implementation allows calls to


functions that have beend declared above.
 Following are examples of implementation of
the function main for the tree class.

Struktur Data & Algoritma


main(): insert data

#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;
}

Struktur Data & Algoritma


main(): delete node

cout << “Enter data to be deleted :";


cin >> itemD;
if ( BST.IsEmpty ( ) )
cout << “Tree is empty" << endl;
else
BST.DeleteItem ( itemD );

Struktur Data & Algoritma


main(): node search & print tree

cout << "\n Enter data to be searched :";


cin >> itemR;
BST.RetrieveItem ( itemR, found );
if ( found == true )
cout << "Data found" << endl;
else
cout << "Data not found" << endl;

cout << "Data in order" << endl;


BST.PrintTree ( );

}//close main()

Struktur Data & Algoritma


Example of the Output

Enter data, 0 to stop :


35 20 10 5 46 55 61 0

Enter data to be deleted : 5

Enter data to be searched : 10

Data found

10 20 35 46 55 61

Struktur Data & Algoritma


Expression Trees

 An example of the application of binary tree


is the expression trees.
 One use of the binary tree is as a compiler.
 Expression trees are also used to represent
arithmetic expressions, comparison
expressions and logical expressions.
 Hence, the binary trees for these types are
also known as Expression trees.

Struktur Data & Algoritma


Depth First Traversal
Preorder : NLR
Inorder : LNR
Postorder : LRN

 An arithmetic expression tree is a binary tree with the following


properties :-
– Each leaf node is an operand (e.g. a, b, 1, 2, 1000, etc).
– The root and non-leaf nodes are operators (+,-,*,/,sin, cos, etc).
– Subtrees are subexpressions to the arithmetic expression.
orab,sinx,-*abc or bsin x(a * b) - c
 The following are examples of expression trees.
-
sin
or
* c

a b x a b
a or b sin x (a * b) - c

Struktur Data & Algoritma


 Note that the leaf nodes in the above examples are operands
(constants or variables)
 The nodes other than the leaf nodes represent operators or
operations.
 From the same example, if the trees are traversed, hence:
Preorder : or a b sin x -*a b c
Inorder : a or b sin x a*b - c
Postorder : a b or x sin a b*c -
 For an expression tree, the order of traversal produces prefix,
infix and postfix expressions.
 Note that the infix expression is the true expression the most
commonly used.

Struktur Data & Algoritma


Creating an Expression Tree

 The method to build an expression tree from an infix expression is like


the following (if the priority of operation is from left to right):
– Take the middle operator as the root node.
– All operators and operands to the left of the nod become the left subtree.
– All operators and operands to the right of the nod become the right
subtree.
– Note: Operands must become leaf nodes, and operators must become
subtrees’ root nodes.
– In this case, operands or leaf nodes are drawn with the symbol
and other nodes with the symbol .
 e.g. : a - b * c

a *

b c

Struktur Data & Algoritma


 Given the following arithmetic expression:-
 a*(b+c) +d
– Place parentheses at the right place according to the
priority of operation.
 ((a*(b+c))+d)
– Make the operator that has the first priority as the root node
of the first subexpression.

b c

Struktur Data & Algoritma


 Append the operator at the left of the
expression as a subexpression placed at
the upper left of the first subexpression.
*

a +

b c

Struktur Data & Algoritma


 Finally, append the operator that has the last priority as the
root node for the tree.

* d

a +

b c
 Make sure that the expression order does not change when
read from the tree.

Struktur Data & Algoritma


Exercise

 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 )

Struktur Data & Algoritma


AVL Trees (Adelson-Velskii &
Landis)

 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.

Struktur Data & Algoritma


 The use of symbols to represent the AVL trees:
/ : left higher
- : equal height
\ : right higher
 If a new node is inserted (appended) to an AVL tree,
the balance of the tree must be examined.
 If the tree become unbalanced, rebalancing must be
done through the operation rotation.

Struktur Data & Algoritma


 Four cases require rebalancing.
 left of left – A left high tree’s subtree is also
left high. To balance, rotate the out of
balance node to the right (right rotation).

2 1
0 8

1
8 1 2
2 0
1
2
1 2

Struktur Data & Algoritma


 When node 12 is appended, node 18 will be
made the root node, node 20 will be the right
subtree for the root node.

Struktur Data & Algoritma


right of right - A right high tree’s subtree is also right high. To
balance, rotate the out of balance node to the left (left
rotation).
When node 20 is appended, node 18 will be made the root
node, node 12 will be the left subtree for the root node.

1 1
2 8

1
8 1 2
2 0
2
1 0 2

Struktur Data & Algoritma


 right of left - A left high tree’s subtree is right
high. To balance is more complicated
because double rotation is needed.

1 1
2 2 8

4 8
4 1
2
4
8
1 2 3

Struktur Data & Algoritma


 When node 8 is appended, the first rotation i.e. left
rotation needs to be made to node 4. So node 12
will be made the root node, node 4 becomes the left
subtree, and node 8 must be placed in a suitable
location to fulfill the properties of a binary search
tree.
 Then do the second rotation i.e. right rotation to
node 12. Node 8 will be made the root node and
node 12 becomes the right subtree.

Struktur Data & Algoritma


 left of right - A right high tree’s subtree is left high.
Also needs double rotation.
– When node 8 is appended, the first rotation i.e. right
rotation needs to be made to node 44. So node 12 will be
made the root node, node 44 becomes the right subtree,
and node 18 must be placed in a suitable location to fulfill
the properties of a binary search tree.
– Then do the second rotation i.e. left rotation to node 12.
Node 18 will be made the root node and node 12
becomes the left subtree.

1 1
2 2 1
8

4 1
4 8 1 4
2 4
1 4
1 8 2 4 3

Struktur Data & Algoritma


The Node Insertion Process to AVL
Trees

 The following diagrams show the process of inserting


data to an AVL tree. The algorithm to insert/append
node to an AVL tree refers to page 637 - 641 of the
textbook.

1 1
8 8

1 1 2
2 2 0
1 2

Append value 20 to tree

Struktur Data & Algoritma


 Steps to append a node into an AVL tree :-
– Choose a suitable leaf node that fulfills the properties
of an AVL tree for the node to be appended.
– Compare the node to be appended with the root node
whether it is smaller are greater.
– If it smaller than the root node, hence the append
process will take place at the left subtree; and vice
versa.
– The search begins at the root node until the leaf
nodes. Append the particular node to a suitable leaf
node by connecting both of them.

Struktur Data & Algoritma


– Determine whether the tree is balanced after the append
process.
– Balance the AVL tree by doing the rotation operation
based on the 4 cases given.

Struktur Data & Algoritma


The Node Deletion Process in an
AVL Tree

 The node deletion process in an AVL tree is similar


to the node deletion process in a binary search tree.
 The algorithm to append node into an AVL tree can
be referred to pages 652 of the text book.
 Steps to delete a node from an AVL tree:-
– Determine whether the node is a leaf node.
– Follow the node deletion method as done in a binary search
tree.
– Determine whether the tree is still balanced after the
deletion
– If not, rebalance the AVL tree by doing the rotation
operation based on the 4 cases given.

Struktur Data & Algoritma


Exercise
12

7 97

6 11 16 72

30 48 41

39 31 90

Struktur Data & Algoritma

You might also like