0% found this document useful (0 votes)
56 views66 pages

10 - Trees

This document covers trees and their applications in computer science. It introduces trees and tree terminology. It discusses applications of trees like binary search trees, decision trees, and prefix codes. It covers tree traversal algorithms like preorder, inorder and postorder traversal. It also discusses spanning trees, minimum spanning trees, and algorithms for finding minimum spanning trees like Prim's algorithm and Kruskal's algorithm.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
56 views66 pages

10 - Trees

This document covers trees and their applications in computer science. It introduces trees and tree terminology. It discusses applications of trees like binary search trees, decision trees, and prefix codes. It covers tree traversal algorithms like preorder, inorder and postorder traversal. It also discusses spanning trees, minimum spanning trees, and algorithms for finding minimum spanning trees like Prim's algorithm and Kruskal's algorithm.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 66

Chapter 10

Trees
Objectives
⚫ 10.1- Introduction to Trees
⚫ 10.2- Applications of Trees
⚫ 10.3- Tree Traversal
⚫ 10.4 - Spanning Trees
⚫ 10.5 - Minimum Spanning Trees
10.1- Introduction to Trees
Introduction to Trees…
Introduction to Trees

circuit disconnected
Introduction to Trees…
Proof: page 684
Introduction to Trees…

Some Page 686


terminologies:

Subtree
Root node (vertex)
Internal node
Leaf
Parent
Child
Siblings
Descendants
Ancestors
Introduction to Trees…

Full Full Full


3-ary
Binary Ternary 5-ary

Some terminologies on binary tree:


Left child, right child, left subtree, right subtree
Introduction to Trees…

Ordered rooted tree


Some Tree Models

A Organizational Tree
Some Tree Models

A Computer File System


Some Tree Models

A Tree-connected Network of seven Processors


Properties of Trees

Using Mathematic Induction.


Let nE be number of edges.
P(n): If the tree T having n vertices then nE=n-1
Basic step:
P(1): n=1, tree has the root node only → nE= 0 = n-1 → P(1) true
Induction step:
Suppose that P(k) is true for all k>=1, ie nE=k-1
Add a leaf vertex v to the tree T so that T having k+1 verteices still is a tree.
Let w be the parent of v.
Because T is connected and has no simple circuit → there is only one new edge
between u and v → nE= (k-1)+1 = k .
→ P(k+1) true
Proved.
Introduction to Trees…

m=3
i=4
n= 3.4+1=13

m=5
i=3
n= 3.5+1=16
Introduction to Trees…

m=3
n=13
i=12/3= 4
l= [(3-1)13+1]/3=9

m=5
n=16
i= 15/5=3
l= (4.16+1)/5= 13
Introduction to Trees…
⚫ Level of a vertex: The length of the path from the
root to this vertex.
⚫ Height of Tree: The maximum of levels of vertices =
The length of the longest path from the root to any
vertex.
Introduction to Trees…
A m-ary tree is called balanced if all leaves are
at levels h or h-1.

h=4 h=4 h=3


All leaves are at All leaves are at All leaves are at
levels 3, 4 levels 2,3, 4 levels 3
→ Balanced → Not Balanced → Balanced
Introduction to Trees…
THEOREM 5: (Proof: page 692)
There are at most mh leaves in an m-ary tree of height h.

Proof: page 693


10.2- Applications of Trees

⚫ Binary Search Trees


⚫ Decision Trees
⚫ Prefix Codes
Constructing a Binary Search Tree

Construct a binary search tree for numbers: 23, 16, 43, 5, 9, 1, 6, 2, 33, 27.
Algorithm for inserting an element to BST

Complexity: O(logn)
Proof: page 698
Decision Trees

The Counterfeit Coin Problem


Decision Trees:
Sorting based on Binary Comparisons
Prefix Codes

⚫ Introduction to Prefix Codes


⚫ Huffman Coding Algorithm
Prefix Codes: Introduction

⚫ English word “sane” is stores 1 byte/character


➔ 4-byte memory block is needed (32 bits).
⚫ There are 26 characters → We can use 5 bit
only to store a character ( 25=32)
⚫ The word “sane” can be stored in 20 bits
⚫ May we can store this word in fewer bit?
Prefix Codes: Introduction

⚫ Construct a binary tree with a


prefix code.
⚫ “sane” will be store as
11111011100 ➔ 11 bits
11111011100 : s
11111011100 : a
11111011100 : n
11111011100 : e
➔ Compression factor: 32/11 ~ 3
Prefix Codes: Huffman Coding Algorithm

⚫ Counting occurrences of characters in a text ➔


frequencies (probabilities) of each character.
⚫ Constructing a binary tree representing prefix
codes of characters.
➔ The set of binary codes representing each
character.
➔ Coding source text
Prefix
Codes:
Huffman
Coding
Algorithm
Prefix Codes: Huffman Coding Algorithm
Game Trees: The Game Nim
There are
some piles
of stones (
ex: 2,2,1).
Two players
will take
turns
picking
stones from
one pile.
The player
picks the
last stones
is loser.
Game Trees : Tic-tac-toe
Game Trees…
Game Trees…
Traversal Algorithms

⚫ At a time, a vertex is visited


⚫ Operations are done:
– Process the visited vertex, e.g. list it’s information
– Traversing recursively subtree.
⚫ Bases on orders of tasks, traversals are
classified into:
– Preorder traversal. N L R
– Inorder traversal. L N R
– Postorder traversal. L R N
10.3- Tree Traversal

⚫ Traversal a tree: A way to visit all vertices of the


rooted tree.
– Universal Address Systems
– Traversal Algorithms
– Infix, Prefix, and Postfix Notation
Universal Address Systems
Preorder Traversal

a, b, e, f, c, d, g, h, i

?
Preorder traversal - example
a
Pre-order traversal: N L R •

• • •
a
• • • b c d

b c d • • • •
• • e f g h
• •
e f g h

a b e f c d g h

38
Inorder Traversal

e, b, f, a, c, g, d, h, i

?
Inorder traversal - example
a

in-order traversal: L N R
• • •
b c d
a
• • • • • • • •
b c d e f g h
• • • •
e f g h

e b f a c g d h

40
Postorder Traversal

e, f, b, c, g, h, i, d, a

?
Postorder traversals -
examples
a
post-order traversal: L R N •

• • •
a
• • • b c d

b c d • • • •
• • e f g h
• •
e f g h

e f b c g h d a

42
Infix, Prefix, and Postfix Notation
⚫ Expression Trees
Infix, Prefix, and Postfix Notation
⚫ Expression Trees
Infix, Prefix, and Postfix Notation

⚫ Infix form:
operand_1 operator operand_2 x+y
⚫ Prefix form:
operator(operand_1,operand_2) +xy
⚫ Postfix form:
(operand_1,operand_2)operator xy+
⚫ How to find prefix and postfix form from infix
form?
(1) Draw expression tree.
(2) Using Preorder traverse → Prefix form
Using Postorder traverse → Postfix form
Infix, Prefix, and Postfix Notation
Infix form

Prefix form

Postfix form
Infix, Prefix, and Postfix Notation
Spanning trees
⚫ Introduction

48
Spanning trees
Definition.
Let G be a simple graph. A spanning tree of
G is a subgraph of G that is a tree
containing every vertex of G.

49
Remov EdgesThat Form Simple Circuits.
50
THEOREM 1
A simple graph is connected if and only if it
has a spanning tree.

51
Depth-First Search

52
The edges selected by
depth-first search of a
graph are called tree
edges.
All other edges are
called back edges.

53
54
Breadth-First Search

55
56
Backtracking Applications
⚫ Graph Colorings
⚫ Then-Queens Problem
⚫ Sums of Subsets

57
Graph Colorings - example

58
The n-Queens Problem

59
Minimum Spanning Trees

Which links should be made to ensure that there is a path


between any two computer centers so that the total cost of
the network is minimized?
➔ minimum spanning tree: a spanning tree that has the
smallest possible sum of weights of its edges.

60
Algorithms for Minimum Spanning
Trees
⚫ Prim’s algorithm (1957 by Robert Prim) (originally
discovered by Vojtech Jarník in1930).
⚫ Choosing any edge with smallest weight, putting it into
the spanning tree.
⚫ Add to the tree edges of minimum weight that are
incident to a vertex already in the tree, never forming a
simple circuit with those edges already in the tree.
⚫ Stop when n−1 edges have been added.

61
62
Kruskal’s algorithm (by Joseph
Kruskal in 1956)
⚫ Choose an edge in the graph with minimum weight.
⚫ Add edges with minimum weight that do not form a
simple circuit with those edges already chosen.
⚫ Stop after n−1 edges have been selected.

63
64
Summary
⚫ Introduction to Trees
⚫ Applications of Trees
⚫ Tree Traversal
⚫ Spanning Trees
⚫ Minimum Spanning Trees
⚫ Thanks

You might also like