0% found this document useful (0 votes)
48 views

10 Trees

This document covers key topics related to trees including: - Definitions of tree terminology like root, leaf, parent, child, etc. - Applications of trees such as binary search trees, decision trees, prefix codes. - Algorithms for traversing trees including preorder, inorder and postorder traversal. - Representing mathematical expressions using infix, prefix and postfix notation and converting between them using expression trees. - Spanning trees and algorithms for finding them such as depth-first search and breadth-first search.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
48 views

10 Trees

This document covers key topics related to trees including: - Definitions of tree terminology like root, leaf, parent, child, etc. - Applications of trees such as binary search trees, decision trees, prefix codes. - Algorithms for traversing trees including preorder, inorder and postorder traversal. - Representing mathematical expressions using infix, prefix and postfix notation and converting between them using expression trees. - Spanning trees and algorithms for finding them such as depth-first search and breadth-first search.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 57

Chapter 10

Trees
Objectives
⚫ 10.1- Introduction to Trees
⚫ 10.2- Applications of Trees
⚫ 10.3- Tree Traversal
10.1- Introduction to Trees
Introduction to Trees…
Introduction to Trees

circuit disconnected
Introduction to Trees…
Proof: page 684
Suppose that T is a rooted tree. If v is a vertex in T other
than the root, the parent of v is the unique vertex u such
that there is a directed edge from u to v (the reader
should show that such a vertex is unique). When u is the
parent of v, v is called a child of u. Vertices with the same
parent are called siblings. The ancestors of a vertex other
than the root are the vertices in the path from the root to
this vertex, excluding the vertex itself and including the
root (that is, its parent, its parent’s parent, and so on, until
the root is reached). The descendants of a vertex v are
those vertices that have v as an ancestor. A vertex of a
rooted tree is called a leaf if it has no children. Vertices
that have children are called internal vertices. The root is
an internal vertex unless it is the only vertex in the graph,
in which case it is a leaf.
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
ORDERED ROOTED TREES
An ordered rooted tree is a rooted tree where the
children of each internal vertex are ordered.
Ordered rooted trees are drawn so that the
children of each internal vertex are shown in
order from left to right. Note that a representation
of a rooted tree in the conventional way
determines an ordering for its edges. We will use
such orderings of edges in drawings without
explicitly mentioning that we are considering a
rooted tree to be ordered.
In an ordered binary tree (usually called just a binary tree),
if an internal vertex has two children, the first child is called
the left child and the second child is called the right child.
The tree rooted at the left child of a vertex is called the left
subtree of this vertex, and the tree rooted at the right child
of a vertex is called the right subtree of the vertex. The
reader should note that for some applications every vertex
of a binary tree, other than the root, is designated as a right
or a left child of its parent. This is done even when some
vertices have only one child. We will make such
designations whenever it is necessary, but not otherwise.
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
n −1
n = mi + 1  i =
m
n =l +i
 l + i = mi + 1
(m − 1)( n − 1)
 l = (m − 1)i + 1 = +1
m
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 of height h is called balanced if all
leaves are at levels h or h-1.

h=4 h=4 h=3


All leafs are at levels All leafs are at levels All leafs are at levels
3, 4 2,3, 4 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

?
Inorder Traversal

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

?
Postorder Traversal

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

?
Traverse Algorithms
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

x+y 2+x–4/3
Infix, Prefix, and Postfix Notation
10.4- Spanning Trees
DEFINITION 1: 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
10.4- Spanning Trees
THEOREM 1 : A simple graph is connected if and only if it has a
spanning tree.
10.4.2 Depth-First Search
11.4.3 Breadth-First Search

You might also like