1 Copyright M.R.K. Krishna Rao 2003 Ch 9 - Trees Definition: A tree is a connected undirected graph...

38
1 Copyright M.R.K. Krishna Rao 2003 Ch 9 - Trees Ch 9 - Trees Definition: Definition: A A tree tree is a connected is a connected undirected graph with no simple circuits. undirected graph with no simple circuits. Since a tree cannot have a simple circuit, Since a tree cannot have a simple circuit, a tree cannot contain multiple edges or a tree cannot contain multiple edges or loops. loops. Therefore, any tree must be a Therefore, any tree must be a simple simple graph graph . . Computer-Based Applications? Computer-Based Applications? Build data structures Build data structures Perform encoding Perform encoding Study formal computer languages Study formal computer languages Searching and sorting Searching and sorting

Transcript of 1 Copyright M.R.K. Krishna Rao 2003 Ch 9 - Trees Definition: A tree is a connected undirected graph...

1Copyright M.R.K. Krishna Rao 2003

Ch 9 - TreesCh 9 - Trees

Definition:Definition: A A treetree is a connected is a connected undirected graph with no simple circuits.undirected graph with no simple circuits.

Since a tree cannot have a simple circuit, a Since a tree cannot have a simple circuit, a tree cannot contain multiple edges or loops.tree cannot contain multiple edges or loops.

Therefore, any tree must be a Therefore, any tree must be a simple simple graphgraph..

Computer-Based Applications?Computer-Based Applications?– Build data structuresBuild data structures– Perform encodingPerform encoding– Study formal computer languagesStudy formal computer languages– Searching and sortingSearching and sorting

2Copyright M.R.K. Krishna Rao 2003

TreesTrees

Example:Example: Are the following graphs trees? Are the following graphs trees?

No.No.

Yes.Yes.

Yes.Yes.

No.No.

3Copyright M.R.K. Krishna Rao 2003

TreesTrees

Theorem:Theorem: An undirected graph is a tree if An undirected graph is a tree if and only if there is a and only if there is a unique simple unique simple pathpath between any of its vertices. between any of its vertices.

Proof:Proof: To prove necessary condition: Suppose T is a tree. Since T is connected,

there is a simple path between two vertices.

If there are two paths between two vertices, then there is a simple circuit in the graph, contradicting that T is a tree.

So there is a unique simple path between any two of its vertices.

4Copyright M.R.K. Krishna Rao 2003

TreesTreesProof (cont.):Proof (cont.): To prove sufficient condition: Suppose there is a unique simple path between

any two vertices of an undirected graph T. First, obviously, T is connected. Second, T cannot have any simple circuit, for

otherwise there exist two paths between two vertices on a simple circuit, which is a contradiction.

So T is connected and does not have any simple circuits.

Hence T is a tree.

5Copyright M.R.K. Krishna Rao 2003

TreesTrees

Definition:Definition: An undirected graph that does An undirected graph that does not contain simple circuits and is not not contain simple circuits and is not necessarily connected is called a necessarily connected is called a forestforest..

In general, we use trees to represent In general, we use trees to represent hierarchical structureshierarchical structures..

We often designate a particular vertex of a We often designate a particular vertex of a tree as the tree as the rootroot. Since there is a unique . Since there is a unique path from the root to each vertex of the path from the root to each vertex of the graph, we direct each edge away from the graph, we direct each edge away from the root.root.

Thus, a tree together with its root produces a Thus, a tree together with its root produces a directed graphdirected graph called a called a rooted treerooted tree..

6Copyright M.R.K. Krishna Rao 2003

Tree TerminologyTree Terminology

If v is a vertex in a rooted tree other than the If v is a vertex in a rooted tree other than the root, the root, the parentparent of v is the unique vertex u of v is the unique vertex u such that there is an edge from u to v.such that there is an edge from u to v.

When u is the parent of v, v is called the When u is the parent of v, v is called the childchild of u. of u.

Vertices with the same parent are called Vertices with the same parent are called siblingssiblings..

The The ancestorsancestors of a vertex other than the of a vertex other than the root are the vertices in the path from the root are the vertices in the path from the root to this vertex, excluding the vertex root to this vertex, excluding the vertex itself and including the root.itself and including the root.

7Copyright M.R.K. Krishna Rao 2003

Tree TerminologyTree Terminology

The The descendantsdescendants of a vertex v are those of a vertex v are those vertices that have v as an ancestor.vertices that have v as an ancestor.

A vertex of a tree is called a A vertex of a tree is called a leafleaf if it has no if it has no children.children.

Vertices that have children are called Vertices that have children are called internal verticesinternal vertices..

If a is a vertex in a tree, then the If a is a vertex in a tree, then the subtreesubtree with a as its root is the subgraph of the with a as its root is the subgraph of the tree consisting of a and its descendants tree consisting of a and its descendants and all edges incident to these and all edges incident to these descendants.descendants.

8Copyright M.R.K. Krishna Rao 2003

Tree TerminologyTree Terminology

The The levellevel of vertex v in a rooted tree is the of vertex v in a rooted tree is the length of the unique path from the root to this length of the unique path from the root to this vertex.vertex.

The level of the root is defined to be zero.The level of the root is defined to be zero.

The The heightheight of a rooted tree is the maximum of of a rooted tree is the maximum of the levels of vertices.the levels of vertices.

Theorem:Theorem: Every tree is a bipartite graph. Every tree is a bipartite graph.

Theorem:Theorem: A tree with n vertices has A tree with n vertices has nn - 1 edges. - 1 edges.

Theorem:Theorem: The minimum number of nodes of a The minimum number of nodes of a tree of height tree of height hh is is hh+1.+1.

9Copyright M.R.K. Krishna Rao 2003

Trees as modelsTrees as models

Example:Example: Family tree - Bernoulli Family tree - Bernoulli

NikolausNikolaus

Johann IJohann I Jacob IJacob I

Nikolaus IINikolaus II Johann IIJohann II

10Copyright M.R.K. Krishna Rao 2003

Trees as modelsTrees as models

Example:Example: File system File system

//

usrusr temptemp

binbin spoolspool lsls

binbin

11Copyright M.R.K. Krishna Rao 2003

Trees as modelsTrees as models

Example:Example: Arithmetic expressions Arithmetic expressions

++ --

yy zz xx yy

This tree represents the expression (y + z)This tree represents the expression (y + z)(x - y).(x - y).

12Copyright M.R.K. Krishna Rao 2003

TreesTreesDefinition:Definition: A rooted tree is called an A rooted tree is called an m-ary treem-ary tree if if

every internal vertex has no more than every internal vertex has no more than m m childrenchildren. .

The tree is called a The tree is called a full m-ary treefull m-ary tree if every internal if every internal vertex has exactly m children.vertex has exactly m children.

Theorem:Theorem: There are at most There are at most mmhh leaves an m-ary tree leaves an m-ary tree of height of height hh..

An m-ary tree with m = 2 is called a An m-ary tree with m = 2 is called a binary treebinary tree..

A rooted A rooted mm-ary tree of height -ary tree of height hh is called a is called a balanced balanced tree tree if all if all leavesleaves are at level are at level hh or or h h – 1.– 1.

13Copyright M.R.K. Krishna Rao 2003

TreesTreesTheorem:Theorem: A full m-ary tree with A full m-ary tree with

– i internal vertices (have children) has n=mi+1 i internal vertices (have children) has n=mi+1 verticesvertices

– nn vertices has vertices has ii=(=(nn-1)/m internal vertices and -1)/m internal vertices and ll=[(=[(mm-1)-1)nn+1]/+1]/mm leaves leaves

– ii internal vertices has internal vertices has n = mi+1n = mi+1 vertices and vertices and ll = = ((mm-1)-1)ii+ 1 leaves+ 1 leaves

– ll leaves has leaves has nn = ( = (mlml-1)/(-1)/(mm-1) vertices and -1) vertices and ii=(=(ll-1)/(-1)/(mm-1) internal vertices-1) internal vertices

P1P2 P3

P4 P5 P6 P7

14Copyright M.R.K. Krishna Rao 2003

Application of TreesApplication of Trees How should items in a list be stored so How should items in a list be stored so

that an item can be easily located?that an item can be easily located? binary search treesbinary search trees

What series of decisions should be made What series of decisions should be made to find an object with a certain property in to find an object with a certain property in a collection of objects of a certain type?a collection of objects of a certain type? decision treesdecision trees

How should a set of characters be How should a set of characters be efficiently coded by bit strings?efficiently coded by bit strings? Prefix codesPrefix codes

15Copyright M.R.K. Krishna Rao 2003

Binary Search TreesBinary Search Trees

If we want to perform a large number of searches If we want to perform a large number of searches in a particular list of items, it can be worthwhile in a particular list of items, it can be worthwhile to arrange these items in a to arrange these items in a binary search binary search treetree to facilitate the subsequent searches. to facilitate the subsequent searches.

A binary search tree is a binary tree in which A binary search tree is a binary tree in which each child of a vertex is designated as a right each child of a vertex is designated as a right or left child, and each vertex is labeled with a or left child, and each vertex is labeled with a key, which is one of the items.key, which is one of the items.

When we construct the tree, vertices are When we construct the tree, vertices are assigned keys so that the key of a vertex is assigned keys so that the key of a vertex is both larger than the keys of all vertices in its both larger than the keys of all vertices in its left subtree and smaller than the keys of all left subtree and smaller than the keys of all vertices in its right subtree.vertices in its right subtree.

16Copyright M.R.K. Krishna Rao 2003

Binary Search Binary Search TreesTrees

Recursive procedure used to form the binary Recursive procedure used to form the binary search tree for a list of items:search tree for a list of items:

Start with tree with one vertex (the root)Start with tree with one vertex (the root) First item is assigned as the key of the rootFirst item is assigned as the key of the root To add a new item, compare it with the keys of To add a new item, compare it with the keys of

vertices already in the tree starting at the rootvertices already in the tree starting at the root move to the left if the item is less than the key of move to the left if the item is less than the key of

the respective vertex and this vertex has a left the respective vertex and this vertex has a left child (if no left child, add this item as left child), or child (if no left child, add this item as left child), or

move to the right if the item is greater than the move to the right if the item is greater than the key of the respective vertex and this vertex has a key of the respective vertex and this vertex has a right child (if no right child, add this item as right right child (if no right child, add this item as right child).child).

17Copyright M.R.K. Krishna Rao 2003

Binary Search TreesBinary Search TreesExample:Example: Construct a binary search tree for Construct a binary search tree for the strings the strings math, computer, power, north, math, computer, power, north, zoo, dentist, bookzoo, dentist, book..

mathmath

18Copyright M.R.K. Krishna Rao 2003

Binary Search TreesBinary Search TreesExample:Example: Construct a binary search tree for Construct a binary search tree for the strings the strings math, computer, power, north, math, computer, power, north, zoo, dentist, bookzoo, dentist, book..

mathmath

computecomputerr

19Copyright M.R.K. Krishna Rao 2003

Binary Search TreesBinary Search TreesExample:Example: Construct a binary search tree for Construct a binary search tree for the strings the strings math, computer, power, north, math, computer, power, north, zoo, dentist, bookzoo, dentist, book..

mathmath

computecomputerr

powerpower

20Copyright M.R.K. Krishna Rao 2003

Binary Search TreesBinary Search TreesExample:Example: Construct a binary search tree for Construct a binary search tree for the strings the strings math, computer, power, north, math, computer, power, north, zoo, dentist, bookzoo, dentist, book..

mathmath

computecomputerr

powerpower

northnorth

21Copyright M.R.K. Krishna Rao 2003

Binary Search TreesBinary Search TreesExample:Example: Construct a binary search tree for Construct a binary search tree for the strings the strings math, computer, power, north, math, computer, power, north, zoo, dentist, bookzoo, dentist, book..

mathmath

computecomputerr

powerpower

northnorth zoozoo

22Copyright M.R.K. Krishna Rao 2003

Binary Search TreesBinary Search TreesExample:Example: Construct a binary search tree for Construct a binary search tree for the strings the strings math, computer, power, north, math, computer, power, north, zoo, dentist, bookzoo, dentist, book..

mathmath

computecomputerr

powerpower

northnorth zoozoodentistdentist

23Copyright M.R.K. Krishna Rao 2003

Binary Search TreesBinary Search TreesExample:Example: Construct a binary search tree for Construct a binary search tree for the strings the strings math, computer, power, north, math, computer, power, north, zoo, dentist, bookzoo, dentist, book..

mathmath

computecomputerr

powerpower

northnorth zoozoodentistdentistbookbook

24Copyright M.R.K. Krishna Rao 2003

Binary Search TreesBinary Search Trees

To perform a search in such a tree for an item To perform a search in such a tree for an item x, we can start at the root and compare its x, we can start at the root and compare its key to x. If x is less than the key, we proceed key to x. If x is less than the key, we proceed to the left child of the current vertex, and if x to the left child of the current vertex, and if x is greater than the key, we proceed to the is greater than the key, we proceed to the right one.right one.

This procedure is repeated until we either found This procedure is repeated until we either found the item we were looking for, or we cannot the item we were looking for, or we cannot proceed any further.proceed any further.

In a balanced tree representing a list of n items, In a balanced tree representing a list of n items, search can be performed with a maximum of search can be performed with a maximum of log(n + 1)log(n + 1) steps steps (compare with binary (compare with binary search)search)..

25Copyright M.R.K. Krishna Rao 2003

Decision TreesDecision Trees• Rooted trees can be used to model problems in Rooted trees can be used to model problems in

which a series of decisions leads to a solutionwhich a series of decisions leads to a solution• Binary search tree can be used to locate items Binary search tree can be used to locate items

based on a series of comparisons, where each based on a series of comparisons, where each comparison tells us whether we have located comparison tells us whether we have located the item, or whether we should go right or left the item, or whether we should go right or left in a subtreein a subtree

• A rooted tree in which each internal vertex A rooted tree in which each internal vertex corresponds to a decision, with a subtree at corresponds to a decision, with a subtree at these vertices for each possible outcome of the these vertices for each possible outcome of the decision is called a decision is called a DECISION TREEDECISION TREE

• Possible solutions correspond to the paths from Possible solutions correspond to the paths from the root to the leaves of this rooted treethe root to the leaves of this rooted tree

26Copyright M.R.K. Krishna Rao 2003

Application of TreesApplication of TreesDecision Trees - exampleDecision Trees - example

– 7 coins, all with same weight, and a counterfeit 7 coins, all with same weight, and a counterfeit coin.coin.

– How many weighings are necessary using a How many weighings are necessary using a balance scale to determine which of the eight is balance scale to determine which of the eight is the counterfeit one (assuming it is lighter)?the counterfeit one (assuming it is lighter)?

– Three possibilities for each weighing:Three possibilities for each weighing:• Coins have equal weightCoins have equal weight• First pan is heavierFirst pan is heavier• Second pan is heavierSecond pan is heavier

– Thus, we have a 3-ary treeThus, we have a 3-ary tree– There are at least 8 leaves because there are 8 There are at least 8 leaves because there are 8

possible outcomes (each of the 8 coins could be possible outcomes (each of the 8 coins could be the counterfeit lighter coin), and each possible the counterfeit lighter coin), and each possible outcome must be represented by at least one leafoutcome must be represented by at least one leaf

27Copyright M.R.K. Krishna Rao 2003

Application of TreesApplication of Trees

Prefix CodesPrefix Codes– Consider using bit strings to encode the letters of Consider using bit strings to encode the letters of

the English alphabet (no distinction between the English alphabet (no distinction between uppercase and lowercase)uppercase and lowercase)

– Each letter can be represented with a bit string of Each letter can be represented with a bit string of length 5, since there are 26 letters and 32 bit length 5, since there are 26 letters and 32 bit strings of length 5.strings of length 5.

– The total number of bits used to encode the data The total number of bits used to encode the data is 5 times the number of characters in the text is 5 times the number of characters in the text when each character is encoded with 5 bitswhen each character is encoded with 5 bits

– Is it possible to find a coding scheme of these Is it possible to find a coding scheme of these letters so that, when data are coded, fewer bits letters so that, when data are coded, fewer bits are used? (this helps saving memory and are used? (this helps saving memory and reducing transmittal time)reducing transmittal time)

28Copyright M.R.K. Krishna Rao 2003

Application of TreesApplication of TreesPrefix CodesPrefix Codes

– Consider using bit strings of different lengths Consider using bit strings of different lengths to encode lettersto encode letters• Letters occurring more frequently should be Letters occurring more frequently should be

encoded using short bit strings; less frequent = encoded using short bit strings; less frequent = longer strings –-- used in Huffman coding, longer strings –-- used in Huffman coding,

• Need some method to denote where a string Need some method to denote where a string starts and endsstarts and ends

• One way to ensure that no bit string corresponds One way to ensure that no bit string corresponds to more than one sequence of letters, the bit to more than one sequence of letters, the bit string for a letter must never occur as the first string for a letter must never occur as the first part of the bit string for another letterpart of the bit string for another letter

• Codes with this property are called Codes with this property are called PREFIX PREFIX CODESCODES

29Copyright M.R.K. Krishna Rao 2003

Application of TreesApplication of TreesPrefix CodesPrefix Codes

– For example, you’ve coded e as 0, a as 10, For example, you’ve coded e as 0, a as 10, and t as 11:and t as 11:•What does 10110 denote?What does 10110 denote?•Ate -- can’t interpret this string of Ate -- can’t interpret this string of

numbers any other way!numbers any other way!– Prefix code can be represented using a Prefix code can be represented using a

binary tree, where characters are the labels binary tree, where characters are the labels of the leaves in the tree.of the leaves in the tree.

– Edges of tree are labeled so that an edge Edges of tree are labeled so that an edge leading to a left child is assigned a 0 and an leading to a left child is assigned a 0 and an edge leading to a right child is assigned a 1edge leading to a right child is assigned a 1

30Copyright M.R.K. Krishna Rao 2003

Application of TreesApplication of Trees

Prefix CodesPrefix Codes– Example: how are e,a,t,n,s encoded below?Example: how are e,a,t,n,s encoded below?

– 0, 10, 110, 1110, 11110, 10, 110, 1110, 1111

1

1

1

1

0

0

0

0

e

a

t

n s

31Copyright M.R.K. Krishna Rao 2003

Application of TreesApplication of Trees

Prefix CodesPrefix Codes– Example: what does 11111011100 denote?Example: what does 11111011100 denote?

1

1

1

1

0

0

0

0

e

a

t

n s

32Copyright M.R.K. Krishna Rao 2003

Application of TreesApplication of Trees

Prefix CodesPrefix Codes– Example: what does 11111011100 denote?Example: what does 11111011100 denote?

– Sane!Sane!

1

1

1

1

0

0

0

0

e

a

t

n s

33Copyright M.R.K. Krishna Rao 2003

Tree TraversalTree Traversal

• Ordered rooted trees are often Ordered rooted trees are often used to store information.used to store information.

• We need procedures for visiting We need procedures for visiting each vertex of an ordered each vertex of an ordered rooted tree to access data.rooted tree to access data.

34Copyright M.R.K. Krishna Rao 2003

Universal Address SystemsUniversal Address Systems

Procedures for traversing all vertices Procedures for traversing all vertices rely on the orderings of children.rely on the orderings of children.

1.1. Label the root with 0. Then label Label the root with 0. Then label its k children from left to right.its k children from left to right.

2.2. For each vertex v at level n with For each vertex v at level n with label A, label its kv children as label A, label its kv children as they drawn from left to right with they drawn from left to right with A.1, A.2, … , A.kv.A.1, A.2, … , A.kv.

35Copyright M.R.K. Krishna Rao 2003

Traversal AlgorithmsTraversal Algorithms

1.1.PreorderPreorder

2.2.InorderInorder

3.3.PostorderPostorder

36Copyright M.R.K. Krishna Rao 2003

Spanning TreesSpanning Trees

DEF: Let G be a simple graph. A DEF: Let G be a simple graph. A spanning tree of G is a subgraph spanning tree of G is a subgraph of G that is a tree containing of G that is a tree containing every vertex of G.every vertex of G.

Theorem: A simple graph is Theorem: A simple graph is connected iff it has a spanning connected iff it has a spanning tree.tree.

37Copyright M.R.K. Krishna Rao 2003

Depth-First SearchDepth-First Search

DFS(G)DFS(G)

T = tree consisting only of vertex v1T = tree consisting only of vertex v1

Visit(v1)Visit(v1)

endend

Visit(v)Visit(v)

For each vertex w adjacent to v and For each vertex w adjacent to v and not yet in Tnot yet in T– Add vertex w and edge {v, w} to TAdd vertex w and edge {v, w} to T– Visit(w)Visit(w)

38Copyright M.R.K. Krishna Rao 2003

Breadth-First SearchBreadth-First Search

BFS(G)BFS(G)

T = tree consisting only of vertex v1T = tree consisting only of vertex v1

L = empty listL = empty list

Put v1 in the list LPut v1 in the list L

While L is not emptyWhile L is not empty– Remove the first vertex, v, from LRemove the first vertex, v, from L– For each neighbor w of vFor each neighbor w of v

• If w is not in L and not in T thenIf w is not in L and not in T then– Add w to end of the list LAdd w to end of the list L– Add w and edge {v,w} to TAdd w and edge {v,w} to T