Birla Institute of Technology
Trees
TreesA very important type of graph in CS is
called a tree:
Real
Tree
L23 2
transformation
Trees – Their Definition
Let A be a set and let T be a relation on A. We say that T is a tree if there is a vertex v0 with the property that there exists a unique path in T from v0 to every other vertex in A, but no path from v0 to v0.
Trees – Our Definition
We need a way to describe a tree, specifically a “rooted” tree.
First, a rooted tree has a single root, v0, which is a vertex with absolutely no edges coming into it. (in-degree of v0 = 0)
Every other vertex, v, in the tree has exactly one path to it from v0. (in-degree of v = 1)
There may be any number of paths coming out from any vertex.
Denoted (T,v0) is a rooted tree
DefinitionsLevels – all of the vertices located n-edges from v0 are said to be at level n.
Level 0
Level 1
Level 2
Level 3
More Definitions
A vertex, v, is considered the parent of all of the vertices connected to it by edges leaving v.
A vertex, v, is considered the offspringof the vertex connected to the single edge entering v.
A vertex, v, is considered the sibling of all vertices at the same level with the same parent.
More Definitions
A vertex v2 is considered a descendantof a vertex v1 if there is a path from v1to v2.
The height of a tree is the number of the largest level.
The vertices of a tree that have no offspring are considered leaves.
If the vertices of a level of a tree can be ordered from left to right, then the tree is an ordered tree.
More Definitions
If every vertex of a tree has at most n offspring, then the tree is considered an n-tree.
If every vertex of a tree with offspring has exactly n offspring, then the tree is considered a complete n-tree.
When n=2, this is called a binary tree.
Giving Meaning to Vertices and Edges
Trees implied that a vertex is simply an entity with parents and offspring much like a family tree.
What if the position of a vertex relative to its siblings or the vertex itself represented an operation.
Examples:
Edges from a vertex represent cases from a switch statement in software
Vertex represented a mathematical operation
Mathematical Order of Precedence Represented with Trees
Consider the equation:
(3 – (2 x)) + ((x – 2) – (3 + x))
Each element is combined with another using an operator, i.e., this expression can be broken down into a hierarchy of (a b) where “” represents an operation used to combine two elements.
We can use a binary tree to represent this equation with the elements as the leaves.
Precedence Example Tree
3 – +
2 x 3x 2 x
– –
+
Positional Tree
A positional tree is an n-tree that relates the direction/angle an edge comes out of a vertex to a characteristic of that vertex. For example:
When n=2, then we have a positional binary tree.
YesNo
Maybe Left Right X=0
X=1
X=2
Tree to Convert Base-2 to Base-10
Starting with the first digit, take the left or right edge to follow the path to the base-10 value.
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
0 1
0 1 0 1
0 1 0 1 0 1 0 1
0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1
First digit
Second digit
3rd
digit
4th
For-Loop Represented with Tree
for i = 1 to 3
for j = 1 to 5
array[i,j] = 10*i + j
next j
next i
For Loop Positional Tree
11 12 13 14 15 21 22 23 24 25 31 32 33 34 35
i = 1
i = 2
i = 3
j=1
j=2
j=3
j=4
j=5
Storing Binary Trees in Computer
“linked lists”. Each item in the list was comprised of two components: Data
Pointer to next item in list
Positional binary trees require two links, one following the right edge and one following the left edge. This is referred to as a “doubly linked list.”
DataLeft Pointer Right
Represent in computer using Linked List
3 (4) (5) – (9) + (12)
2 (6) x (10) 3 (13)x (7) 2 (11) x (14)
– (3) – (8)
+ (2)
The numbers in parenthesis represent the index from which
they Can be shown in the linked list
Doubly Linked ListIndex Left Data Right
1 2 ------- 0
2 3 + 8
3 4 – 5
4 0 3 0
5 6 7
6 0 2 0
7 0 x 0
8 9 – 12
9 10 – 11
10 0 x 0
11 0 2 0
12 13 + 14
13 0 3 0
14 0 x 0
root
Huffman Code
Depending on the frequency of the letters occurring in a string, the Huffman Code assigns patterns of varying lengths of 1’s and 0’s to different letters.
These patterns are based on the paths taken in a binary tree.
A Huffman Code Generator can be found at:
http://www.inf.puc-rio.br/~sardinha/Huffman/Huffman.html
Searching Trees
Terminology
“Visiting” a vertex – the act of performing a task at a vertex, e.g., perform a computation or make a decision.
“Searching” the tree – the process of visiting each vertex in a specific order or path. The term “searching” can be misleading. Just think of it as “traversing” the tree.
Tree Search
The application of some trees involves traversing the tree in a methodical pattern so as to address every vertex.
Our book uses the term “search”, but sometimes search can imply we’re looking for a particular vertex. This is not the case.
Example:Assume we want to compute the average age, maximum age, and minimum age of all of the children from five families. (Tree is on next slide.)
Search ExampleNeighborhood
families
A. Jones Halls Smith Taylor B. Jones
Katy
age 3
Tommy
age 5
Phil
age 8
Taylor
age 1
Lori
age 4
Lexi
age 2
Karen
age 14
Bart
age 12
Mike
age 6
Ben
age 2
Search Example (continued)
To calculate the average, max, and min ages for all of the children, we need to have a method for going through the tree so that we don’t miss a child.
By defining a rigorous process, not only can a human be sure not to miss a vertex, but also an algorithm can be defined for a computer
A Suggested Process for Searching a Tree
1.Starting at the root, repeatedly take the leftmost “untraveled” edge until you arrive at a leaf which should be a child.
2.Include this child in the average, max, and min calculations.
3.One at a time, go back up the edges until you reach a vertex that hasn’t had all of its outgoing edges traveled.
4.If you get back to the root and cannot find an untraveled edge, you are done. Otherwise, return to step 1.
Vertices Numbered in Order of Visits
Neighborhood
families
A. Jones Halls Smith Taylor B. Jones
Katy
age 3
Tommy
age 5
Phil
age 8
Taylor
age 1
Lori
age 4
Lexi
age 2
Karen
age 14
Bart
age 12
Mike
age 6
Ben
age 2
1
2
3
5
6
7
8
9
10
11
12
13
14
15
164
Preorder SearchThis methodical pattern of traversing a tree is called a preorder search.
Assume v is the root of a binary positional tree T. Each vertex of this tree has at most a left vertex,
vL, and a right vertex, vR.
If either vL or vR have offspring, then they are subtrees of T, and a search can be performed of them too.
By viewing a tree this way, then the search method we described in earlier slides can be performed using a recursive algorithm applied to each vertex.
The recursive algorithm is repeatedly applied until every leaf has been reached.
Preorder Search Algorithm
A preorder search of a tree has the following three steps:
1. Visit the root
2. Search the left subtree if it exists
3. Search the right subtree if it exists
The term “search” in steps 2 and 3 implies that we apply all three steps to the subtree beginning with step 1.
Vertices Visited in Alphabetical Order Using Preorder Search
A
B
C
D F J LG
I KE
H
Prefix or Polish Form
x
–
a
d e
c ÷b
+
Preorder search produces: × – a b + c ÷ d e
Binary tree representing: (a – b) × (c + (d ÷ e))
Polish Form (continued)
Allows us to write complex arithmetic expressions without using parenthesis
Expression is evaluated by performing following steps:
Move left to right until you find a string of the form Fxy, where F is the symbol for a binary operation and x and y are numbers.
Evaluate x F y and substitute answer for string Fxy.
Repeat starting at beginning of string again.
Polish Form Example
1. × – 6 4 + 5 ÷ 2 2 1st pattern: – 6 4
2. × 2 + 5 ÷ 2 2 2nd pattern: ÷ 2 2
3. × 2 + 5 1 3rd pattern: + 5 1
4. × 2 6 4th pattern: × 2 6
5. 12
(6 – 4) × (5 + (2 ÷ 2)) = 12
Inorder and Postorder Searches
Preorder search gets its name from the fact that the operator that joins two items is evaluated first, e.g., the binary operation 6 –4 is visited in the order – 6 4.
Inorder search evaluates the expression as it is written, e.g., the binary operation 6 – 4 is visited in the order 6 – 4.
Postorder search evaluates the operator after the elements are read, e.g., the binary operation 6 – 4 is visited in the order 6 4 –.
Inorder Search Algorithm
An inorder search of a tree has the following three steps:
1. Search the left subtree if it exists
2. Visit the root
3. Search the right subtree if it exists
Postorder Search Algorithm
A postorder search of a tree has the following three steps:
1. Search the left subtree if it exists
2. Search the right subtree if it exists
3. Visit the root
Evaluation of Tree UsingInorder Search
A
B
C
D F J LG
I KE
H
Resulting string: DCBFEGAIJHKL
Evaluation of Tree UsingPostorder Search
A
B
C
D F J LG
I KE
H
Resulting string: DCFGEBJILKHA
Infix Form
x
–
a
d e
c ÷b
+
Inorder search produces: a – b × c + d ÷ e
Unfortunately, without parenthesis, we can’t do
anything with this expression.
Binary tree representing: (a – b) × (c + (d ÷ e))
Postfix or Reverse Polish Form
x
–
a
d e
c ÷b
+
Inorder search produces: a b – c d e ÷ + ×
Binary tree representing: (a – b) × (c + (d ÷ e))
Reverse Polish Form (continued)Allows us to write complex arithmetic expressions without using parenthesis
Expression is evaluated by performing following steps:
Move left to right until you find a string of the form xyF, where F is the symbol for a binary operation and x and y are numbers.
Evaluate x F y and substitute answer for string xyF.
Repeat starting at beginning of string again.
Reverse Polish Form Example
From left-to-right evaluate xyF first.
1. 2 1 – 3 4 2 ÷ + × 1st pattern: 2 1 –
2. 1 3 4 2 ÷ + × 2nd pattern: 4 2 ÷
3. 1 3 2 + × 3rd pattern: 3 2 +
4. 1 5 × 4th pattern: 1 5 ×
5. 5(2 – 1) × (3 + (4 ÷ 2)) = 5
Converting an Ordered n-tree to a Positional Binary Tree
An ordered n-tree where some vertices have more than two offspring can be converted to a positional binary tree.
This allows easier computer representation with methods such as linked lists.
A process exists for this conversion that works on any finite tree.
Process to Convert Ordered n-tree to Positional Binary Tree
Starting at the root, the first orleftmost offspring of a vertex remains the leftmost vertex in the binary treeThe first sibling to the right of the leftmost vertex becomes the right offspring of the leftmost vertexSubsequent siblings become the right offspring in succession until last sibling is converted.
A
B C D E
A
BC
D
E
Conversion Example
A
B C D
EGF H I
J K L
Conversion ExampleA
B C D
EGF H I
J K L
A
B
C
D
E
GF
H
I
J
K
L
Preorder search:
Left tree – ABEFCGJKLHID
Right tree – ABEFCGJKLHID
Lets see the problem.
City 1 City 2 Mileage
Cleveland Philadelp
hi
400
Cleveland Detroit 200
Cleveland Chicago 350
Cleveland Pittsburg 150
Philadelphi Detroit 600
Philadelphi Chicago 700
Philadelphi Pittsburg 300
Detroit Chicago 300
Detroit Pittsburg 300
Chicago Pittsburg 450
A small startup airline wants to provide service to the 5 cities in the table to the right. Allowing for multiple connecting flights, determine all of the direct flights that would be needed in order to service all five cities.
Source: http://www.usembassy
malaysia.org.my/distance.html
Minimal Spanning Trees
Undirected Tree
An undirected tree is simply the symmetric closure of a tree.
It is relation that results from a tree where all edge are made bidirectional, i.e., there is no defined direction.
L23 48
Connected Relation
A relation is connected if for every a and b in R, there is a path from a to b.
It is easier to see a connected relation using a digraph than it is to describe in using words.
A
B
D
C
E
Connected
A
B
D
C
E
Not Connected
Spanning Tree of connected relations
Textbook definition: “If R is a symmetric, connected relation on a set A, we say that a tree T on A is a spanning tree for R if T is a tree with exactly the same vertices as R and which can be obtained from R by deleting some edges of R.”
Basically, a undirected spanning tree is one that connects all n elements of A with n-1 edges.
To make a cycle connecting n elements, more than n-1 edges will be needed. Therefore, there are no cycles.
Weighted Graph
In the past, we have represented a undirected graph with unlabeled edges. It can also be represented with a symmetric binary matrix.
0 1 1 0
1 0 1 0
1 1 0 1
0 0 1 0
MT =
A
B
D
C
Weighted Graph (continued)
By giving the edges a numeric value indicating some parameter in the relation between two vertices, we can create a weighted tree.
A
B
D
C5
3
4
7
Weighted Graph (continued)
We can still use matrix notation to represent a weighted graph. Replace the 1’s used to represent an edge with the edge’s weight. A 0 indicates no edge.
0 5 3 0
5 0 4 0
3 4 0 7
0 0 7 0
MT =
Exercise(Not drawn to scale)
Philadelphia
Pittsburg
Chicago
Cleveland
Detroit
400
200
350
150
600
700300
300300
450
Note R relation has 5 vertices .
Minimal Spanning Tree
Assume T represents a spanning tree for an undirected graph.
The total weight of the spanning tree T is the sum of all of the weights of all of the edges of T.
The one(s) with the minimum total weight are called the minimal spanning tree(s).
As suggested by the “(s)” in the above definition, there may be a number of minimal spanning trees for a particular undirected graph with the same total weight.
Nearest neighbour of a vertex
A vertex u is nearest neighbour of a vertex v if u and v are adjacent and no other vertex is joined to v by an edge of lesser weight.
L23 56
Algorithms for Determining the Minimal Spanning Tree
There are two algorithms presented in our textbook for determining the minimal spanning tree of an undirected graph that is connected and weighted.
Prim’s Algorithm: process of stepping from vertex to vertex
Kruskal’s Algoritm: searching through edges for minimum weights
Prim’s Algorithm
Let R be a symmetric, connected relation with n vertices.
1. Choose a vertex v1 of R. Let V = {v1} and E = { }.
2. Choose a nearest neighbor vi of V that is adjacent to vj, vj V, and for which the edge (vi, vj) does not form a cycle with members of E. Add vi to V and add (vi, vj) to E.
3. Repeat Step 2 until |E| = n – 1. Then Vcontains all n vertices of R, and E contains the edges of a minimal spanning tree for R.
Prim’s Algorithm in English
The goal is to one at a time include a new vertex by adding a new edge without creating a cycle
Pick any vertex to start. From it, pick the edge with the lowest weight.
As you add vertices, you will add possible edges to follow to new vertices.
Pick the edge with the lowest weight to go to a new vertex without creating a cycle.
Kruskal’s Algorithm
Let R be a symmetric, connected relation with n vertices and let S = {e1, e2, e3, …ek} be the set of all weighted edges of R.
1. Choose an edge e1 in S of least weight. Let E = {e1}. Replace S with S – {e1}.
2. Select an edge ei in S of least weight that will not make a cycle with members of E. Replace E with E {ei} and S with S –{ei}.
3. Repeat Step 2 until |E| = n – 1.
Kruskal’s Algorithm in English
The goal is to one at a time include a new edge without creating a cycle.
Start by picking the edge with the lowest weight.
Continue to pick new edges without creating a cycle. Edges do not necessarily have to be connected.
Stop when you have n-1 edges as R have n vertices.
Answer: Kruskal to Find MST(Not drawn to scale)
H
G
AE
D
3
F
2
4
3 2
2C
B
5
Sequence of edge selections (D,E), (D,H)(A,C)(A,B)(E,G)(E,F)(C,E)
=2+2+2+3+3+4+5=21,