Chapter 5
Tree
Introduction to Trees:
◦ Basic Definitions
◦ Rooted Trees
Applications of Trees:
◦ Trees as Models
◦ Binary Search Trees
◦ Tree Traversal
A tree:
◦ Is a connected graph with no cycles
◦ It can not contain multiple edges or loops.
Theorems:
◦ A graph is a tree if and only if there is a unique
simple path between any two of its vertices. (Why?)
◦ |V|=|E|+1 (Why?)
 Examples:
◦ G1 and G2 are trees.
◦ G3 and G4 are not a tree, G3 contains cycle, G4 not connected.
A forest:
◦ Is a graph where each of its connected components is a tree.
A graph with three connected components
A rooted tree:
◦ Is a directed graph T satisfying two conditions:
 It is a tree if the directions of the edges are ignored.
 There is a unique vertex
r
such that the indegree ofr
is 0 and the indegree of any other vertex is 1. This vertex r is called the root of the rooted tree.
◦ We can change any unrooted tree into a rooted tree
by choosing any vertex as the root.
Examples:
◦
Different choices of the root produce different rooted trees.
T With root a With root c
Rooted tree terminology:
◦ For a directed edge from vertex u to vertex v:
 u is a parent of v, v is a child of u.
◦ For a vertex u:
 Any vertices (excluding vertex u) on the path from the root to the vertex u are the ancestors of u.
 Vertex u is the descendant of these vertices.
◦ A terminal vertex or a leaf is a vertex that has no children out degree = 0 . (There must be at least one leaf. Why?)
◦ An internal vertex or branch node is one that has children.
◦ The length of path from root to the vertex is vertex level
◦ A subtree is a subgraph of a tree.
◦ Two vertices of a common parent are referred to as siblings
Examples:
◦
The left child of d is f.
◦
The right child of d is g.
◦
(b) and (c) show the left and right subtrees of c.
Rooted tree terminology (continued):
◦
An m-ary tree:
 Is a rooted tree in which each internal vertex has at most m children.
 Special case: binary tree where m = 2.
◦
A complete m-ary tree:
 Is a rooted tree in which each internal vertex has exactly m children.
◦
A balanced m-ary tree with height h :
 Is a m-ary tree with all leaves being at levels h or h-1.
 Level: distance from root
 Height: maximum level
Examples:
◦ T1 is a complete binary tree. T1 is not balanced.
◦ T2 is a balanced complete 5-ary tree.
◦ T3 is a balanced 3-ary but not a complete m-ary tree for any m.
T3 T1 T2
Ordered rooted trees are often used to store information.
A binary search tree:
◦
Is a binary tree in which each vertex is labeled with a key such that:
 No two vertices have the same key.
 If vertex u belongs to the left subtree of vertex v, then u  v.
 If vertex w belongs to the right subtree of vertex v, then v  w.
Binary search tree construction algorithm:
◦ Start with a tree containing just one vertex (the root).
◦ Let v = the root.
◦ To add a new item a, do the following until stop:
 If a < v:
 If v has a left child then v = left child of v (move to the left)
 Else add a new left child to v with this item as its key. Stop.
 If a > v:
 If v has a right child then v = right child of v (move to the right).
 Else add a new right child to v with this item as its key. Stop.
Examples:
◦
Construct a binary search tree for the list 5, 9, 8, 1, 2,
4, 10, 6.
Binary search tree search algorithm:
◦ Let v = the root.
◦ To search the item a, do the following until stop:
 If a = v then stop.
 If a < v:
 If v has a left child then v = left child of v (move to the left)
 Else stop.
 If a > v:
 If v has a right child then v = right child of v (move to the right).
 Else stop.
Examples:
◦
Search for 7 in the following binary tree.
◦
If not found, add it to the tree.
◦ An ordered rooted tree:
 Is a rooted tree where the children of each internal vertex are ordered.
◦ In an ordered binary tree, if an internal vertex u has two children:
 The first child is called the left child.
 The tree rooted at the left child is called the left subtree of vertex u.
 The second child is called the right child.
 The tree rooted at the right child is called the right subtree of vertex u.
Tree traversal:
◦
Is a procedure that systematically visits every vertex of an ordered rooted tree.
 Visiting a vertex = processing the data at the vertex.
◦
Three most commonly used algorithms:
 Preorder traversal.
 Inorder traversal.
 Postorder traversal.
Let T be an ordered rooted tree with root r .
Suppose that T
1, T
2, , T
nare the subtrees at r from left to right in T .
T2
T1
T3
Preorder traversal:
◦
Begins by visiting r .
◦
Next traverses T
1in preorder, then T
2in preorder, , then T
nin preorder.
◦
Ends after T
nhas been traversed.
Preorder traversal
In binary tree the traversal is characterized by
◦
Visiting a parent before its children and a left child before a right child.
◦
Algorithm:
 Step 1: Visit the root.
 Step 2: Go to the left subtree.
If one exists, do a preorder traversal.
 Step 3: Go to the right subtree.
If one exists, do a preorder traversal.
Preorder traversal:
◦
An example of recursive algorithm.
 Examples:
◦ Preorder traversal:
 Visit root, visit subtrees left to right.
Applications: print a structured document
Make Money Fast!
1. Motivations 2. Methods References
2.1 Stock
Fraud 2.2 Ponzi Scheme
1.1 Greed 1.2 Avidity 2.3 Bank
Robbery
1
2
3
5
4 6 7 8
9
Inorder traversal:
◦
Begins by traversing T
1in inorder.
◦
Next visits r , then traverses T
2in inorder, , then T
nin inorder.
◦
Ends after T
nhas been traversed.
In order traversal
Examples:
◦ Inorder traversal:
 Visit leftmost subtree, visit root, visit other subtrees left to right.
Applications: print out information stored in a binary search tree.
3 1
2
5 6
7 9
8 4
Postorder traversal:
◦
Begins by traversing T
1in postorder.
◦
Next traverses T
2in postorder, then traverses T
3in postorder, , then T
nin postorder.
◦
Ends by visiting r .
Postorder traversal
Examples:
◦ Postorder traversal:
 Visit subtrees left to right, visit root.
Applications: compute space used by files in
a directory and its subdirectories.
cs16/
homeworks/ todo.txt
programs/ 1K
DDR.java
10K Stocks.java h1c.doc 25K
3K h1nc.doc
2K Robot.java
20K
9
3
1
7
2 4 5 6
8
Shortcut to list the vertices in preorder, inorder, postorder:
◦
Draw a curve around the ordered rooted tree starting
at the root and move along the edges.
◦ Preorder list is obtained by
 listing each vertex the first time this curve passes it.
 Result: a, b, d, h, e, i, j, c, f, g, k.
◦ Inorder list is obtained by
 listing a leaf the first time the curve passes it and
 listing each internal vertex the second time the curve passes it.
 Result: h, d, b, i, e, j, a, f, c, k, g.
◦ Postorder list is obtained by
 listing a vertex the last time it is passes on the way back up to its parent.
 Result: h, d, i, j, e, b, f, k, g, c, a.
Spanning Trees
Minimum Spanning Trees
◦ Prim’s Algorithm
A spanning tree of a graph G :
◦
Is a tree (formed by using edges and vertices of G) containing all the vertices of G .
◦
Examples:
 A graph (of road system) and its spanning tree.
Examples:
◦
Find a spanning tree of this graph G :
◦
Solution:
 The graph G is connected, but is not a tree because it contains simple circuits.
 Remove an edge from each circuit until we produce a graph with no simple circuits.
 The resulting spanning tree is not unique.
Examples:
◦
Spanning trees play an important role in transportation systems
 Highway construction
Examples:
◦
Broadcast in Internet
Depth-first search: Strategy (for digraph,trees)
◦ choose a starting vertex, distance d = 0
◦ vertices are visited in order of increasing distance from the starting vertex,
◦ examine One edges leading from vertices (at distance d) to adjacent vertices (at distance d+1)
◦ then, examine One edges leading from vertices at distance d+1 to distance d+2, and so on,
◦ until no new vertex is discovered, or dead end
◦ then, backtrack one distance back up, and try other edges, and so on
◦ until finally backtrack to starting vertex, with no more new vertex to be discovered.
and so on…
Breadth-first search: Strategy (for digraph)
◦ choose a starting vertex, distance d = 0
◦ vertices are visited in order of increasing distance from the starting vertex,
◦ examine all edges leading from vertices (at distance d) to adjacent vertices (at distance d+1)
◦ then, examine all edges leading from vertices at distance d+1 to distance d+2, and so on,
◦ until no new vertex is discovered
e.g. Start from vertex A, at d = 0
◦ visit B, C, F; at d = 1
◦ visit D; at d = 2
e.g. Start from vertex E, at d = 0
◦ visit G; at d = 1
Minimum spanning tree in a weighted graph :
◦ Is a spanning tree that has the smallest possible sum of the weights of its edges.
Prim’s algorithm for constructing minimum spanning tree:
◦ Choose an arbitrary vertex as a start of the spanning tree.
◦ Successively add to the tree edges of minimum weight that are incident to a vertex already in the tree
 Break ties arbitrarily
◦ Stop when n – 1 edges have been added.
 n = number of vertices in the weighted graph
◦ Note: The resulting tree may not be unique, but with same total weight
Examples:
◦
Use Prim’s algorithm to design a minimum-cost communications network connecting all the
computers in the following graph.
Introduction to Trees:
◦ Basic Definitions : Section 12.1.
◦ Rooted Trees : Section 12.2.
Applications of Trees:
◦ Tree Traversal : Section 12.2.
Minimum Spanning Trees : Section 13.2