0% found this document useful (0 votes)
16 views56 pages

CMP2006 Lecture9 Trees

This lecture covers the fundamentals of tree data structures, including terminology, types of trees, and operations such as insertion and deletion. It discusses binary search trees (BST), tree traversal algorithms, and expression trees, detailing how to represent and manipulate these structures. Students are expected to demonstrate understanding through practical applications and algorithm implementations.

Uploaded by

divinephoenix65
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views56 pages

CMP2006 Lecture9 Trees

This lecture covers the fundamentals of tree data structures, including terminology, types of trees, and operations such as insertion and deletion. It discusses binary search trees (BST), tree traversal algorithms, and expression trees, detailing how to represent and manipulate these structures. Students are expected to demonstrate understanding through practical applications and algorithm implementations.

Uploaded by

divinephoenix65
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 56

DATA STRUCTURES

LECTURE 9 - TREES
EXPECTED OUTCOMES

At the end of this lecture, the student should be able to:


◼ Explain the basic terminologies associated with trees
◼ Identify and discuss the various tree structures such as general and binary trees
◼ Demonstrate an understanding of the insertion and deletion operations of the general tree
◼ Demonstrate knowledge and understanding of the various binary tree traversal operations
◼ Demonstrate inorder, postorder and preorder traversal of expression trees
◼ Explain the specific terminologies associated with Binary Search tree and AVL tree
◼ Identify and discuss the structures of both search trees
◼ Demonstrate an understanding of the search, insertion and deletion operations of the both search trees
◼ Explain the concept of the Heap
◼ Explain the various operations of the Heap
TOPICS

◼ Introduction to Trees
◼ Common Tree terminologies
◼ Types of Trees
◼ General Trees
◼ Binary Trees
◼ Pictorial Representation of a Tree
◼ BST Tree Operations
◼ Implementation of a BST Tree
◼ Applications of Trees
◼ Tree Traversal
◼ AVL Trees
◼ Expression Trees
◼ Heaps
TREE’S OVERVIEW
TREE’S OVERVIEW

◼ A Tree ADT is a powerful hierarchical non-linear list data structure


◼ It consists of a set of nodes (called vertices) connected by a set of directed lines (called
edges)
◼ The first node of a tree is called the root node if the tree is not empty (i.e. the tree
contains at least one node)
◼ All other nodes in the tree extend directly or indirectly from the root node
◼ Most trees are dynamic – insertion and deletion are not restricted
KEY TREE TERMINOLOGY

◼ Node: an element in the tree, containing a data portion and zero or more links to
child nodes (link portions), which are below it in the tree (by convention, trees are drawn
growing downwards).
◼ Root node: the first element (top-most element) in the tree and which can never have a
parent node
◼ Child node: a node that has a parent
◼ Internal/Parent node: is a node that has at least one branch. The root is an internal node,
except in the special case of a tree that consists of just one node (and no edges)
◼ Leaf node: (also called external node) any node that has no child nodes. A root cannot be
a leaf node
◼ Size: refers to the number of nodes a tree has
TYPES OF TREES

◼ A general tree is a tree in which any node in the tree can have any number of child nodes
(a node can have an unlimited outdegree)
◼ In a binary tree, each node can have at most two child nodes (have a maximum
outdegree of 2). So a node may have only 0, 1 or 2 children.
◼ A node in a binary tree cannot have more than two subtrees (link portions)
◼ All binary trees are also general trees, but not all general trees are binary trees
KEY TREE TERMINOLOGY

◼ Sub tree: Any structure of connected nodes below the root node
◼ Branch: the edges or directed lines that connect nodes
◼ Degree of a node: the number of branches associated with a node
◼ Indegree branch: a branch which is directed toward a particular node
◼ Outdegree branch: a branch that is directed away from or outward from a particular node
◼ Level of a node: The distance from a node B to the root node is the level of B. The root is
at level 0, the root’s children are at level 1, and so on.
◼ Height/Depth of a tree: the distance from the root node to the leaf in the longest path
from the root node, plus one. It is one plus the level of the node that is farthest away from
the root node.
PICTORIAL REPRESENTATION OF A TREE
2
3

1 4 5 9
5 0 1 5

1 1 5 10
4 7 0 0
Can you identify the
following:
6. InDegree of 51 -
1. Parent nodes -
7. Level of 14 -
2. Child nodes -
8. Height (or depth)
3. Leaf nodes -
of tree -
4. Degree of 15 -
PICTORIAL REPRESENTATION OF A TREE
2
3

1 4 5 9
5 0 1 5

1 1 5 10
4 7 0 0
Can you identify the following:
1. Parent nodes – 15, 23, 51, 95
6. InDegree of 51 - 1
2. Child nodes – 14, 15, 17, 40, 50, 51, 95, 100
7. Level of 14 - 2
3. Leaf nodes – 14, 17, 40, 50, 100
8. Height (or depth) of
4. Degree of 15 - 3
tree - 3
5. OutDegree of 15 - 2
THE BINARY TREE STRUCTURE

A Typical Node

points to the start of the Structure of a Tree node


Root
list

NODE
Data <data type>
LeftSubTree <NODE>
1
RightSubTree <NODE>
7
END NODE
Empty node pointer
LeftSubTree Data RightSubTr
Portion ee
KEY FORMULA’S FOR BINARY TREES

◼ The balance factor of a binary tree is the difference between the height of its left subtree and its right
subtree.
◼ A binary tree is considered balanced if its balance factor (and that of its subtrees) is zero (0)
◼ Balance Factor = Hlst – Hrst

◼ A binary tree is said to be complete if it has all the requisite nodes given the height of the tree.
◼ The following formula is used to calculate the number of nodes in a complete binary tree of height h: nodesmax=
2h−1
◼ The minimum height a binary tree with n nodes can have is: Heightmin = 1 + log2(n)
◼ The maximum height a binary tree with n nodes can have is: Heightmax = n
◼ The minimum number of nodes a binary tree of height h can have is: nodesmin = Height
◼ The maximum number of nodes a binary tree of height h can have is: nodesmax = 2Height – 1
PICTORIAL REPRESENTATION
2
3

1 5
5 1

1 1 5
4 7 0

Can you identify the following:


1. Balance Factor of this tree:

2. Calculate the max number of nodes the tree may have given the height:

3. Is the tree complete:


PICTORIAL REPRESENTATION
2
3

1 5
5 1

1 1 5
4 7 0

Can you identify the following:


1. Balance Factor of this tree: Hlst – Hrst = 2 – 2 = 0

2. Calculate the max number of nodes the tree may have given the height: 2h – 1 = 23
–1=7
BINARY SEARCH TREES
BINARY SEARCH TREES

A Binary Search Tree (BST)


◼ Is a binary tree
◼ Elements are added to tree based on the value

Inserting elements in a BST


◼ The first element added to the list will become your root node
◼ All nodes in the left sub tree are less than (<) the root of the tree and parent nodes
◼ All nodes in the right sub tree are greater than or equal to (>=) the root and parent
nodes
◼ All sub trees in a BST are also binary trees and binary search trees
BINARY SEARCH TREE – INSERT ELEMENTS

◼ Let us now build a binary search tree. It will contain the integers: 31, 68, 7, 21, 47, 19, 43,
5, 45 Roo
t

nu
ll
BINARY SEARCH TREE – INSERT ELEMENTS

◼ Let us now build a binary search tree. It will contain the integers: 31, 68, 7, 21, 47, 19, 43,
5, 45 Roo
t
31
BINARY SEARCH TREE – INSERT ELEMENTS

◼ Let us now build a binary search tree. It will contain the integers: 31, 68, 7, 21, 47, 19, 43,
5, 45 Roo
t
31

68
BINARY SEARCH TREE – INSERT ELEMENTS

◼ Let us now build a binary search tree. It will contain the integers: 31, 68, 7, 21, 47, 19, 43,
5, 45 Roo
t
31

7 68
BINARY SEARCH TREE – INSERT ELEMENTS

◼ Let us now build a binary search tree. It will contain the integers: 31, 68, 7, 21, 47, 19, 43,
5, 54 Roo
t
31

7 68

21
BINARY SEARCH TREE – INSERT ELEMENTS

◼ Let us now build a binary search tree. It will contain the integers: 31, 68, 7, 21, 47, 19, 43,
5, 54 Roo
t
31

7 68

21 47
BINARY SEARCH TREE – INSERT ELEMENTS

◼ Let us now build a binary search tree. It will contain the integers: 31, 68, 7, 21, 47, 19, 43,
5, 45 Roo
t
31

7 68

21 47

19
BINARY SEARCH TREE – INSERT ELEMENTS

◼ Let us now build a binary search tree. It will contain the integers: 31, 68, 7, 21, 47, 19, 43,
5, 45 Roo
t
31

7 68

21 47

19 43
BINARY SEARCH TREE – INSERT ELEMENTS

◼ Let us now build a binary search tree. It will contain the integers: 31, 68, 7, 21, 47, 19, 43,
5, 45 Roo
t
31

7 68

5 21 47

19 43
BINARY SEARCH TREE – INSERT ELEMENTS

◼ Let us now build a binary search tree. It will contain the integers: 31, 68, 7, 21, 47, 19, 43,
5, 45 Roo
t
31

7 68

5 21 47

19 43

45
TREE TRAVERSAL ALGORITHMS
TREE TRAVERSAL

◼ Tree traversal (also known as tree search and walking the tree) is a form of
graph traversal and refers to the process of visiting (checking and/or updating) each node
in a tree data structure, exactly once.
◼ These traversals are classified by the order in which the nodes are visited. The traversal of
the tree is done iteratively or recursively until the entire tree is traversed
◼ There are three traversal algorithms:
◼ Preorder Traversal – Processes elements in the following order: Root -> LeftSubTree ->
RightSubTree (Rlr)
◼ Inorder Traversal – Processes elements in the following order: LeftSubTree-> Root -> RightSubTree
(lRr)
◼ Postorder Traversal – Processes elements in the following order: LeftSubtree -> RightSubTree ->
Root (lrR)
PREORDER ALGORITHM

Algorithm PreOrder(val Root <NODE pointer>)


Traverses a tree in a preorder manner (root first, then left child, then right child)

Pre: Root must point to the first node of the tree or the empty node pointer if its empty
Post: The tree is traversed in a preorder manner
Return: Nothing

If(not IsTreeEmpty(Root))
Process(Root->Data) //R
PreOrder(Root->Left) //l
PreOrder(Root->Right) //r
End if
End PreOrder
INORDER ALGORITHM

Algorithm InOrder(val Root <NODE pointer>)


Traverses a tree in a inorder manner (left child first, then root, then right child)

Pre: Root must point to the first node of the tree or the empty node pointer if its empty
Post: The tree is traversed in a inorder manner
Return: Nothing

If(not IsTreeEmpty(Root))
InOrder(Root->Left)
Process(Root->Data)
InOrder(Root->Right)
End if
End InOrder
POSTORDER ALGORITHM

Algorithm PostOrder(val Root <NODE pointer>)


Traverses a tree in a postorder manner (left child first, then right child, then root)

Pre: Root must point to the first node of the tree or the empty node pointer if its empty
Post: The tree is traversed in a postorder manner
Return: Nothing

If(not IsTreeEmpty(Root))
PostOrder(Root->Left)
PostOrder(Root->Right)
Process(Root->Data)
End if
End PostOrder
TREE TRAVERSAL ALGORITHMS
Roo
t
31

7 68

5 21 47

19 43

45

◼ Preorder (Rlr) =
◼ Inorder (lRr) =
◼ Postorder (lrR) =
TREE TRAVERSAL ALGORITHMS
Roo
t
31

7 68

5 21 47

19 43

45

◼ Preorder (Rlr) = 31, 7, 5, 21, 19, 68, 47, 43, 45


◼ Inorder (lRr) = 5, 7, 19, 21, 31, 43, 45, 47, 68
◼ Postorder (lrR) = 5, 19, 21, 7, 45, 43, 47, 68, 31
EXPRESSION TREES
EXPRESSION TREES

◼ One popular application of binary trees is to represent arithmetic expressions


◼ These binary trees are called expression trees. In this setup, the operators are stored in
parent nodes and the operands are stored in leaf nodes
◼ Once the tree is built for a particular expression, traversing the tree preorder, inorder, or
postorder gives the prefix, infix, or postfix representation of the expression respectively,
used in Reverse Polish Notation.
◼ How to represent an expression using trees
◼ First, we must properly bracket the given mathematical expression using BOMDAS. E.g. Given the
expression A * B / C + D should be bracketed as (((A * B) / C) + D).
◼ We would then work from the outer bracket inward, making the outer most operator our root node
then the left and right operand would form part of the left and right subtrees respectively. Continue
working your way inward and add the remaining elements to the appropriate subtree.
POPULATE AN EXPRESSION TREE

◼ Let us now build a expression tree. Start with the outer brackets (((A * B) / C) + D)
Roo
t
+
POPULATE AN EXPRESSION TREE

◼ Let us now build a expression tree. Start with the outer brackets (((A * B) / C) + D)
Roo
t
+

D
POPULATE AN EXPRESSION TREE

◼ Let us now build a expression tree. Start with the outer brackets (((A * B) / C) + D)
Roo
t
+

/ D
POPULATE AN EXPRESSION TREE

◼ Let us now build a expression tree. Start with the outer brackets (((A * B) / C) + D)
Roo
t
+

/ D

C
POPULATE AN EXPRESSION TREE

◼ Let us now build a expression tree. Start with the outer brackets (((A * B) / C) + D)
Roo
t
+

/ D

* C
POPULATE AN EXPRESSION TREE

◼ Let us now build a expression tree. Start with the outer brackets (((A * B) / C) + D)
Roo
t
+

/ D

* C

B
POPULATE AN EXPRESSION TREE

◼ Let us now build a expression tree. Start with the outer brackets (((A * B) / C) + D)
Roo
t
+

/ D

* C

A B
TRAVERSAL OF THE EXPRESSION TREE

◼ We can traverse an expression tree to get various representations of the mathematical


expressions Roo
t
+

/ D

* C

A B

Prefix Representation (Preorder) =


Infix Representation (Inorder) =
Postfix Representation (Postorder) =
TRAVERSAL OF THE EXPRESSION TREE

◼ We can traverse an expression tree to get various representations of the mathematical


expressions Roo
t
+

/ D

* C

A B

Prefix Representation (Preorder) = + / * A B C D


Infix Representation (Inorder) = A * B / C + D
Postfix Representation (Postorder) = A B * C / D +
EXPRESSION CONVERSION WITHOUT A TREE - PREFIX

To convert to prefix we need to move the operator in front of the two operands; ensuring
to keep the operator within the bracket where it belongs.
How do we convert the following infix expression to prefix: A * B / C + D
◼ (((A*B)/C)+D)

◼ (+ ( ( A * B ) / C ) D )

◼ (+ ( / ( A * B ) C ) D )

◼ (+ ( / ( * A B ) C ) D )

◼ +/*ABCD
EXPRESSION CONVERSION WITHOUT A TREE - POSTFIX

To convert to postfix we need to move the operator after the two operands; ensuring to
keep the operator within the bracket where it belongs.
How do we convert the following infix expression to postfix: A * B / C + D
◼ (((A*B)/C)+D)

◼ (((A*B)/C)D+)

◼ (((A*B)C/)D+)

◼ (((AB*)C/)D+)

◼ AB*C/D+
AVL TREES

◼ AVL Trees were invented by computer scientists Adelson-Velsky and Evgenii Landis
◼ AVL trees are self-balancing binary search trees.
◼ It can either be empty of have two AVL subtrees which differs in height by no more than one.
◼ Because the tree is balanced, to complete a search, insert or delete operations it would
take O(log n) steps.
◼ To guarantee an O(log n) number of steps when searching, AVL trees are dynamically
rebalanced
◼ Re-balancing a AVL tree requires at most O(log n) steps
◼ AVL trees don’t have to be perfectly balanced, the heights of the sub trees can differ
by at most 1 (-1, 0, 1)
AVL TREES

Roo
t
31

7 68

5 21 47

19 43
60
HEAPS

◼ A heap is a binary tree which is complete or almost complete.


◼ There are two types of heaps
◼ Min-heap
◼ each node contains a data value which is less than or equal to the data in its children nodes
◼ Max-heap
◼ Each node contains a data value which is greater than or equal to the data in its children nodes
◼ When the term “heap” is used, it usually refers to max-heap. The data value of the left child does not have
to be less than the key value of the right child, but both have to be less than or equal to the data value of
their parent node
Heaps have a variety of uses, including:
◼ Dijkstra's shortest path algorithm, Prim's minimal spanning tree algorithm and some other graph algorithms
◼ Priority queues
◼ Heap sort and some other sorting algorithms
◼ Selection algorithms (where a heap causes the minimum or maximum value to be found in constant time –
O(1) steps)
HEAPS

Roo
t
5

7 9

25 21 47

80 90 93
BINARY TREE OPERATIONS
BINARY TREE OPERATIONS

◼ Create the Tree


◼ Destroy the Tree
◼ Insert Node
◼ Delete Node
◼ Traverse PreOrder
◼ Traverse InOrder
◼ Traverse PostOrder
◼ Count Nodes
◼ Check if the Tree is full
◼ Check if the Tree is empty
CREATE EMPTY TREE

Algorithm CreateTree()
Create a new Tree

Pre: Nothing
Post: An empty Tree is created
Return: The Root of the tree

Declare Root as NODE pointer


Root ← Empty Node Pointer
Return Root
End CreateTree
INSERT A NODE
Algorithm InsertNode(ref Root <NODE pointer>, val D <integer>)
Insert a node in its correct position in a binary search tree
Pre: Root must point to the first node of the tree or the empty node pointer if its empty
Post: The node is inserted in its correct position in a binary search tree
Return: Nothing

Declare N, T as Node pointers


Allocate memory for N
If memory allocation was successful then
N->Data ← D
N->LeftChild ← Empty
N->RightChild ← Empty
if Root = Empty
Root← N
else
T← Root
while( True )
if N->Data < T->Data
if T->LeftChild = Empty
T->LeftChild ← N
exit loop
else
T ← T->LeftChild
endif
else
if T->RightChild = Empty
T->RightChild ← N
exit loop
else
T ← T->RightChild endif
Endif
endif
end while
end if
else
Write “Error – out of memory, cannot add new node”
endif
APPLICATIONS OF TREES

◼ Represent phrase structure of sentences in natural languages such as English


◼ Represent algebraic formulae
◼ Represent structured hierarchical objects such as a family tree or an organizational chart
◼ Searching large dynamic lists such as the world wide web
◼ Sorting large dynamic lists
◼ Artificial intelligence e.g. robot exploration or solving optimal path problems
◼ Games e.g. chess
◼ File system maintained by an operating system
◼ Compiler Design
QUESTIONS?

You might also like