You are on page 1of 38

CAB301 Algorithms and Complexity

Lecture 6 – Tree algorithms

Maolin Tang
Science and Engineering Faculty
Queensland University of Technology

Queensland University of Technology


CRICOS No. 00213J
Aims of this lecture

• This lecture introduces tree data structures and algorithms


– Binary tree and algorithms
– Binary search tree and algorithms
– Tree and algorithms
• This lecture also introduces two important data structures
– Stacks
– Queues

real world 2
R
a university for the

CRICOS No. 00213J


References

• A. Levitin. Introduction to the Design and Analysis of


Algorithms. Addison-Wesley, third edition, 2012. Section 5.3
• T. H. Cormen, C. E. Leiserson, R. L. Rivest and C. Stein,
Introduction to Algorithms, third edition, 2009. Chapter 12

real world 3
R
a university for the

CRICOS No. 00213J


Binary tree

 A binary tree is a non-linear data structure in which each node


has at most two children.

real world 4
R
a university for the

CRICOS No. 00213J


Binary tree

 A binary tree is a set T of nodes


such that either
– T is empty, or
– T is partitioned into three
disjoint subsets:
• A single node root, the root
• Two possibly empty sets
that are binary trees, called
left and right subtrees of
root.

real world 5
R
a university for the

CRICOS No. 00213J


Binary tree implementation

The structure of a binary tree node:

item lchild rchild

lchild: left child pointer


rchild: right child pointer

real world 6
R
a university for the

CRICOS No. 00213J


Binary tree implementation

root
A

B C ^

D ^^ E ^^ F ^

G ^

H ^^
^  null

real world 7
R
a university for the

CRICOS No. 00213J


Binary search tree
 A binary search tree is a binary tree, where every node’s left subtree
contains values that are less than or equal to the node’s value,
every node’s right subtree contains values that are greater than or
equal to the node’s value, and both left and right binary trees and
binary search trees.
60 60

50 70 50 70

80 30 55 75
30 55 65 65

75 80

binary search tree not a binary search tree

real world 8
R
a university for the

CRICOS No. 00213J


Traversal of a binary tree

• Often one wishes to visit each of the nodes in a binary tree and
examine the value there.
• There are three common orders in which the nodes can be
visited, and each has useful properties that are exploited in
algorithms based on binary trees.
– Pre-order
– In-order
– Post-order

real world 9
R
a university for the

CRICOS No. 00213J


Pre-order traversal

• Pre-order traversal is recursively


defined as follows: 
– visit the root first, then
– traverse the left subtree in  
pre-order, and then
– traverse the right subtree in   
pre-order.

ABDECFGH

real world 10
R
a university for the

CRICOS No. 00213J


In-order traversal

• In-order traversal is recursively


defined as follows: 
– traverse the left subtree in in-
order, then  
– visit the root, and then
  
– traverse the right subtree in
in-order.

DBEACGHF

real world 11
R
a university for the

CRICOS No. 00213J


Post-order traversal

• Post-order traversal is
recursively defined as follows: 
– traverse the left subtree
in post-order, and then  
– traverse the right subtree
in post-order, and then
  
– visit the root.

DEBHGFCA

real world 12
R
a university for the

CRICOS No. 00213J


Search

 Searching a binary search tree for a specific item is a recursive


process due to the property of binary search trees.
 We begin by examining the root.
– If the given item equals to the item at the root, return true;
– If it is less than the item at the root, we recursively search
the left subtree in the same manner.
– Similarly, if it is greater than the item at the root, we
recursively search the right subtree in the same manner.

real world 13
R
a university for the

CRICOS No. 00213J


Search

Search for 80
 60

50  70

30 55  80
65

75

real world 14
R
a university for the

CRICOS No. 00213J


Search

Search for 79
 60

50  70

30 55  80
65

 75

real world 15
R
a university for the

CRICOS No. 00213J


Insert

Insert 45

 60

 50 70

 30 55 80
65

45 75

real world 16
R
a university for the

CRICOS No. 00213J


Delete

 There are three cases that need to be considered:


– the node to be deleted is a leaf
– the node to be deleted has only one child
– the node to be deleted has two children

real world 17
R
a university for the

CRICOS No. 00213J


Delete

 Case 1: the node to be deleted is a leaf

parent parent

ptr

real world 18
R
a university for the

CRICOS No. 00213J


Delete

 Case 2: the node to be deleted has only one child

parent
parent
ptr

c
c

real world 19
R
a university for the

CRICOS No. 00213J


Delete
 Case 3: the node to be deleted has two children

Transform it into the problem of deleting a


node that has only one child or deleting a leaf:
1. find the right-most node in the left subtree of ptr;
ptr 2. copy the item of the right-most node to ptr
3. delete the right-most node

The right-most
node in the left
subtree of ptr

real world 20
R
a university for the

CRICOS No. 00213J


Tree

 Tree is an important non-linear data structure


 It is often used to represent a hierarchy
 It has many applications

The structure of a file folder A family tree

real world 21
R
a university for the

CRICOS No. 00213J


Trees

 A tree T is a set of one or more nodes such that T is partitioned


into disjointed subsets:
– A single node r, the root
– Sets that are trees, called subtrees of r.

root
edge

node

real world 22
R
a university for the

CRICOS No. 00213J


Tree
• Parent of node n: the node directly above node n in the tree.
– e.g., B is the parent of H.
• Child of node n: a node directly below node n in the tree.
– e.g., H is a child of B.
• Root: the only node in the tree with no parent.
• Leaf: a child with no child.
– e.g., C F G H I and J are leaves.
• Siblings: nodes with a common parent.
– e.g., F, G and H are siblings.

real world 23
R
a university for the

CRICOS No. 00213J


Tree

• Subtree: any node in a tree together with all of its descendants.


• Height: the number of nodes on the longest path from root to a
leaf.

Depth = 0
subtree

Height = 3 Depth = 1

Depth = 2

real world 24
R
a university for the

CRICOS No. 00213J


Tree implementation

The structure of a node in trees:

key firstchild firstsibling

firstchild: a pointer to the head of its child linked list


firstsibling: a pointer to next sibling

real world 25
R
a university for the

CRICOS No. 00213J


Tree implementation

root

A ^ ^  null

B C ^ E ^

F ^ G ^ H ^^ I ^ J ^^

real world 26
R
a university for the

CRICOS No. 00213J


Breadth-first traversal

• Breadth-first traversal visits the nodes in the tree in the order of


their depth.
• It first visits all the nodes at depth zero (i.e., the root), then all
the nodes at depth one, and so on.
• At each depth, the nodes are visited from left to right.

real world 27
R
a university for the

CRICOS No. 00213J


Breadth-first traversal

1
depth = 0

2 3 4
depth = 1

5 6 7 8 9

depth = 2

Output: A B C E F G H I J

real world 28
R
a university for the

CRICOS No. 00213J


Queues

 A queue is a linear data structure that allows certain operations


on both ends
 It provides one operation, Enqueue, to add an item to the data
structure, and an operation, Dequeue, to remove an item from the
data structure.
 Items are added into a queue at one end of the queue (tail) and
items are removed out of a queue at the other end of the queue
(head).
 A queue is an FIFO (First-In-First-Out) container.

a university for the real world


R
29
29
CRICOS No. 00213J
Queues

• Basic operations
Enqueue(5) Enqueue(9)
1 3 2 1 3 2 5 1 3 2 5 9

head tail head tail head tail

Dequeue()  1

Dequeue()  3
2 5 9 3 2 5 9

head tail head tail

a university for the real world


R
30
30
CRICOS No. 00213J
Breadth-first traversal

ALGORITHM BreadthFirstTraveral(root)
q ← Φ //q is a queue; initially it is empty
q.enqueue(root)
while q ≠ Φ do
r ← q.dequeue()
visit r;
// add r’s all children to q one by one from left to right
r ← r.firstchild; //r.firstchild is the first child of r
while r ≠ null do
q.enqueue(r)
r ← r.firstsibling

real world 31
R
a university for the

CRICOS No. 00213J


Queue q
initially A

after visiting A B C E

after visiting B C E F G H

after visiting C E F G H

after visiting E F G H I J

after visiting F G H I J

after visiting G H I J

after visiting H I J

after visiting I J

after visiting J

real world 32
R
a university for the

CRICOS No. 00213J


Depth-first traversal

• It visits the root, and then


• It traverses its subtrees in depth-first order from left to right.

real world 33
R
a university for the

CRICOS No. 00213J


Depth-first traversal

2 6 7

3 4 5 8 9

Output: A B F G H C E I J

real world 34
R
a university for the

CRICOS No. 00213J


Stacks

 A stack is a linear data structure that stores items sequentially


and allows access to the top only.
 Adding an item
– Referred to as pushing it onto the stack
 Removing an item
– Referred to as popping it from the stack
 Items stored in a stack are kept in a pile.
– When an item is pushed into a stack, it is placed at the top of
the pile.
– When an item is taken from a stack, it is always the top item.
 A stack is a LIFO (Last In First Out) container.

a university for the real world


R
35
35
CRICOS No. 00213J
Stacks

 Basic operations: Push and Pop

Push(1) Push(3)

3
1 1
empty stack
Push(2)

Pop()  3 Pop()  2
2
3 3
1 1 1

a university for the real world


R
36
36
CRICOS No. 00213J
Depth-first traversal

ALGORITHM DepthFirstTraversal(root)
s ← Φ // s is a stack; initially it is empty
s.push(root)
while s ≠ Φ do
r = s.pop()
repeat
visit r
if r.firstsibling ≠ null s.push(r.firstsibling)
r ← r.firstchild;
until r = null

real world 37
R
a university for the

CRICOS No. 00213J


Stack s
initially A

after visiting A

after visiting B C

after visiting F C G

after visiting G C H
A after visiting H C

after visiting C E
B C E
after visiting E

after visiting I J
F G H I J
after visiting J

real world 38
R
a university for the

CRICOS No. 00213J

You might also like