Your SlideShare is downloading. ×
Trees
Upcoming SlideShare
Loading in...5
×

Thanks for flagging this SlideShare!

Oops! An error has occurred.

×
Saving this for later? Get the SlideShare app to save on your phone or tablet. Read anywhere, anytime – even offline.
Text the download link to your phone
Standard text messaging rates apply
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total Views
565
On Slideshare
0
From Embeds
0
Number of Embeds
2
Actions
Shares
0
Downloads
15
Comments
0
Likes
0
Embeds 0
No embeds

Report content
Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
No notes for slide

Transcript

  • 1. Discrete MathematicsNOR AIDAWATI BINTI ABDILLAH TREES 1
  • 2. TreeDefinition 1. A tree is a connected undirected graph with no simple circuits.Theorem 1. An undirected graph is a tree if and only if there is a unique simple path between any two of its vertices. 2
  • 3. Which graphs are trees?a) b) c) 3
  • 4. Is it a tree? NO!yes! All the nodes are yes! (but not not connected a binary tree) NO! There is a cycle yes! (it’s actually and an extra the same graph as edge (5 nodes the blue one) – but and 5 edges) usually we draw tree by its “levels”4
  • 5. Rooted Treesq Rooted tree is a tree in which one vertex is distinguished and called a rootq Level of a vertex is the number of edges between the vertex and the rootq The height of a rooted tree is the maximum level of any vertexq Children, siblings and parent vertices in a rooted treeq Ancestor, descendant relationship between vertices 5
  • 6. q The parent of a non-root vertex is the unique vertex u with a directed edge from u to v.q A vertex is called a leaf if it has no children.q The ancestors of a non-root vertex are all the vertices in the path from root to this vertex.q The descendants of vertex v are all the vertices that have v as an ancestor.q The level of vertex v in a rooted tree is the length of the unique path from the root to v.q The height of a rooted tree is the maximum of the 6 levels of its vertices.
  • 7. A Tree Has a Root root node ainternal vertex parent of g b c d e f g leaf siblings h i 7
  • 8. a b cd e f g ancestors of h and i h i
  • 9. The level of a vertex v in a rooted tree isthe length of the unique path from the rootto this vertex. level 2 level 3
  • 10. a b cd e f g subtree with b as its h i root subtree with c as its root
  • 11. Tree Propertiesq There is one and only one path between every pair of vertices in a tree, T.q A tree with n vertices has n-1 edges.q Any connected graph with n vertices and n-1 edges is a tree.q A graph is a tree if and only if it is minimally connected. 11
  • 12. Tree PropertiesTheorem . There are at most 2 H leaves in a binary tree of height H.Corallary. If a binary tree with L leaves is full and balanced, then its height is H =  log2 L  . 12
  • 13. Properties of TreesThere are at most mh leaves in an m-arytree of height h.A rooted m-ary tree of height h is calledbalanced if all leaves are at levels h or h-1.
  • 14. Properties of Trees A full m-ary tree with(i) n vertices has i = (n-1)/m internalvertices and l = [(m-1)n+1]/m leaves.(ii) i internal vertices has n = mi + 1vertices and l = (m-1)i + 1 leaves.(iii) l leaves has n = (ml - 1)/(m-1) verticesand i = (l-1)/(m-1) internal vertices.
  • 15. Properties of TreesA full m-ary tree with i internal verticescontains n = mi+1 vertices.
  • 16. Proofq We know n = mi+1 (previous theorem) and n = l+i,q n – no. verticesq i – no. internal verticesq l – no. leavesq For example, i = (n-1)/m
  • 17. Binary TreeA rooted tree in which each vertex has eitherno children, one child or two children. The tree is called a full binary tree if everyinternal vertex has exactly 2 children. A B C right child of Aleft subtree of A D E F G right subtree of H C 17 I J
  • 18. Ordered Binary TreeDefinition 2’’. An ordered rooted tree is a rooted tree where the children of each internal vertex are ordered. In an ordered binary tree, the two possible children of a vertex are called the left child and the right child, if they exist. 18
  • 19. An Ordered Binary Tree A B E CK F G D L H M I J 19
  • 20. Is this binary tree balanced? A rooted binary tree of height Lou H is called balanced if all its leaves are at levels H or H-1. Hal MaxEd Ken Sue Joe Ted 20
  • 21. Searching takes time . . .So the goal in computer programs is to find any stored item efficiently when all stored items are ordered.A Binary Search Tree can be used to store items in its vertices. It enables efficient searches. 21
  • 22. A Binary Search Tree (BST) is . . .A special kind of binary tree in which:1. Each vertex contains a distinct key value,2. The key values in the tree can be compared using “greater than” and “less than”, and3. The key value of each vertex in the tree is less than every key value in its right subtree, and greater than every key value in its left subtree.
  • 23. A Binary Expression Tree is . . .A special kind of binary tree in which:l Each leaf node contains a single operand,l Each nonleaf node contains a single binary operator, andl The left and right subtrees of an operator node represent subexpressions that must be evaluated before applying the operator at the root of the subtree. 23
  • 24. Expression Treeq Each node contains an operator or an operandq Operands are stored in leaf nodesq Parentheses are not stored (x + y)*((a + b)/c) in the tree because the tree structure dictates the order of operand evaluationq Operators in nodes at higher levels are evaluated after operators in nodes at lower levels 24
  • 25. A Binary Expression Tree ‘*’ ‘+’ ‘3’ ‘4’ ‘2’What value does it have?( 4 + 2 ) * 3 = 18 25
  • 26. A Binary Expression Tree ‘*’ ‘+’ ‘3’ ‘4’ ‘2’Infix: ((4+2)*3)Prefix: * + 4 2 3 evaluate from rightPostfix: 4 2 + 3 * evaluate from left 26
  • 27. Levels Indicate PrecedenceWhen a binary expression tree is used to represent an expression, the levels of the nodes in the tree indicate their relative precedence of evaluation.Operations at higher levels of the tree are evaluated later than those below them. The operation at the root is always the last operation performed. 27
  • 28. Evaluatethis binary expression tree ‘*’ ‘-’ ‘/’ ‘8’ ‘5’ ‘+’ ‘3’ ‘4’ ‘2’ What expressions does it represent? 28
  • 29. A binary expression tree ‘*’ ‘-’ ‘/’ ‘8’ ‘5’ ‘+’ ‘3’ ‘4’ ‘2’Infix: ((8-5)*((4+2)/3))Prefix: *-85 /+423 evaluate from rightPostfix: 85- 42+3/* evaluate from left 29
  • 30. Binary Tree for Expressions 30
  • 31. Complete Binary TreeAlso be defined as a full binary tree in which allleaves are at depth n or n-1 for some n.In order for a tree to be the latter kind of completebinary tree, all the children on the last level mustoccupy the leftmost spots consecutively, with nospot left unoccupied in between any two A A B B C C D F G D E F G E H I J K L M N O H I 31 Complete binary tree Full binary tree of depth 4
  • 32. Difference between binary and complete binary tree BINARY TREE ISNT NECESSARY THAT ALL OF LEAF NODE IN SAME LEVEL BUT COMPLETE BINARY TREE MUST HAVE ALL LEAF NODE IN SAME LEVEL.Binary Tree 32 Complete Binary Tree
  • 33. SPANNING TREESq A spanning tree of a connected graph G is a sub graph that is a tree and that includes every vertex of G.q A minimum spanning tree of a weighted graph is a spanning tree of least weight (the sum of the weights of all its edges is least among all spanning tree).q Think: “smallest set of edges needed to connect everything together” 33
  • 34. A graph G and three of itsspanning tree We can delete any edge without deleting any vertex (to remove the cycle), but leave the graph connected. 34
  • 35. PRIM’S ALGORITHMq Choose any edge with smallest weight, putting it into the spanning tree.q Successively add to the tree edges of minimum weight that are incident to a vertex already in the tree and not forming a simple circuit with those edges already in the tree.q Stop when n – 1 edges have been added. 35
  • 36. EXAMPLE PRIM’S ALGORITHMUse Prim’s algorithm to find a minimum spanning treein the weighted graph below: a 2 b 3 c 1 d 3 1 2 5 e 4 f 3 g 3 h 4 2 4 3 i 3 j 3 k 1 l 36
  • 37. SOLUTION Choice Edge Weight 1 {b, f} 1 a 2 b c 1 d 2 {a, b} 2 3 {f, j} 23 1 2 4 {a, e} 3 f 3 g 3 h 5 {i, j} 3e 6 {f, g} 3 2 3 7 {c, g} 2 8 {c, d} 1 i j 9 {g, h} 3 3 k 1 l 10 {h, l} 3 11 {k, l} 1 Total: 24 37
  • 38. KRUSKAL’S ALGORITHMq Choose an edge in the graph with minimum weight.q Successively add edges with minimum weight that do not form a simple circuit with those edges already chosen.q Stop after n – 1 edges have been selected. 38
  • 39. Kruskal’s Algorithmq Pick the cheapest link (edge) available and mark itq Pick the next cheapest link available and mark it againq Continue picking and marking link that does not create the circuit***Kruskal’s algorithm is efficient and optimal 39
  • 40. EXAMPLE KRUSKAL’S ALGORITHMUse Kruskal’s algorithm to find a minimum spanningtree in the weighted graph below: a 2 b 3 c 1 d 3 1 2 5 e 4 f 3 g 3 h 4 2 4 3 i 3 j 3 k 1 l 40
  • 41. SOLUTION Choice Edge Weight 1 {c, d} 1 2 {k, l} 1 a 2 b 3 c 1 d 3 {b, f} 1 4 {c, g} 23 1 2 5 {a, b} 2 f g 3 h 6 {f, j} 2e 7 {b, c} 3 2 8 {j, k} 3 9 {g, h} 3 i j 3 10 {i, j} 3 3 k 1 l 11 {a, e} 3 Total: 24 41
  • 42. TRAVELLING SALESMAN PROBLEM (TSP)The goal of the Traveling Salesman Problem(TSP) is to find the “cheapest” tour of a selectnumber of “cities” with the followingrestrictions:● You must visit each city once and only once● You must return to the original starting point 42
  • 43. TSPTSP is similar to these variations of Hamiltonian Circuit problems: ● Find the shortest Hamiltonian cycle in a weighted graph. ● Find the Hamiltonian cycle in a weighted graph with the minimal length of the longest edge. (bottleneck TSP). A route returning to the beginning is known as a Hamiltonian Circuit A route not returning to the beginning is known as a 43 Hamiltonian Path
  • 44. THANK YOU 44

×