CMP2006 Lecture9 Trees
CMP2006 Lecture9 Trees
LECTURE 9 - TREES
EXPECTED OUTCOMES
◼ 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
◼ 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
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
2. Calculate the max number of nodes the tree may have given the height:
1 5
5 1
1 1 5
4 7 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
◼ 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
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
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
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
◼ 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
/ D
* C
A B
/ D
* C
A B
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
Roo
t
5
7 9
25 21 47
80 90 93
BINARY TREE OPERATIONS
BINARY TREE OPERATIONS
Algorithm CreateTree()
Create a new Tree
Pre: Nothing
Post: An empty Tree is created
Return: The Root of the tree