rooted trees
play

(rooted) trees Oct. 23, 2017 1 Linear Data Structures linked list - PowerPoint PPT Presentation

COMP 250 Lecture 19 (rooted) trees Oct. 23, 2017 1 Linear Data Structures linked list array Non-Linear Data Structures tree graph 2 Tree Example: Organization Hierarchy (McGill) 3


  1. COMP 250 Lecture 19 (rooted) trees Oct. 23, 2017 1

  2. Linear Data Structures linked list array Non-Linear Data Structures tree graph 2

  3. Tree Example: Organization Hierarchy (McGill) 3

  4. Family Tree (descendents) person Child 3 Child 1 Child 2 Here I ignore spouses (partner). 4

  5. Family Tree (ancestors) person dad mom mom’s mom’s dad’s dad’s mom dad mom dad This is an example of a binary tree. 5

  6. Tree: UNIX file system 6

  7. Tree: Java Classes e.g. GUI 7

  8. Tree Terminology A directed edge is ordered pair: (from, to) root node, vertex 8

  9. Every node except the root is a child, and has exactly one parent. root parent child 9

  10. For some trees, • edges are from parent to child • edges are from child to parent • edges are both from parent to child and child to parent. • edge direction is ignored e.g. common with non- rooted trees (see final slide of today) 10

  11. For some trees, Most of definitions today • edges are from parent to child will assume edges are from parent to child. • edges are from child to parent • edges are both from parent to child and child to parent. • edge direction is ignored e.g. common with non- rooted trees (see final slide of today) 11

  12. Q: If a tree has N nodes, how many edges does it have ? 12

  13. Q: If a tree has N nodes, how many edges does it have ? A: N-1 Since every edge is of the form (parent, child), and each node except the root is a child and each child has exactly one parent. 13

  14. Two nodes are siblings if they have the same parent. parent siblings 14

  15. Recursive definition of rooted tree… 15

  16. Recursive definition of rooted tree A tree T is a finite (& possibly empty) set of 𝑜 nodes such that: • if 𝑜 > 0 then one of the nodes is the root r • if 𝑜 > 1 then the 𝑜 − 1 non-root nodes are partitioned into (non- empty) subsets T1, T2 , …, Tk, each of which is a tree (called a subtree”), and the roots of the subtrees are the children of root r. This definition does NOT assume edges are from parent to child. 16

  17. internal nodes (e.g. file directories) external nodes, leaves (e.g. files) 17

  18. A path in a tree is a sequence of nodes ( 𝑤 1 , 𝑤 2 ,…, 𝑤 𝑙 ) such that (𝑤 𝑗 , 𝑤 𝑗+1 ) is an edge. The length of a path is the number of edges in the path (number of nodes – 1) 18

  19. A path with just one node ( 𝑤 1 ) has length = 0, since it has no edges. 19

  20. What is the path length? (2 or 3?) 20

  21. Node v is an ancestor of node w if there is a path from v to w. Node w is a descendent of node v. ancestor descendent 21

  22. The depth or level of a node is the length of the path from the root to the node . depth (level) 0 root 1 2 3 4 22

  23. How to compute depth(v) ? depth (level) 0 1 2 3 4 23

  24. This requires parent links. depth( v ){ This is analogous to a ‘ prev ’ if ( v.parent == null) //root link in a doubly linked list. return 0 else return 1 + depth( v.parent ) } 24

  25. The height of a node is the maximum length of a path from that node to a leaf. ? 3 1 1 0 0 2 1 0 1 0 0 0 0 0 25

  26. The height of a node is the maximum length of a path from that node to a leaf. 2 3 1 0 ? ? 1 0 2 1 0 1 0 0 0 0 0 26

  27. The height of a node is the maximum length of a path from that node to a leaf. ? 2 3 1 1 1 0 0 0 2 1 0 1 0 0 0 0 0 27

  28. How to compute height(v) ? 4 3 2 1 1 0 0 2 1 0 1 0 0 0 0 0 28

  29. 4 height(v){ if (v is a leaf) 3 2 1 return 0 else{ 1 0 0 2 1 h = 0 for each child w of v 0 1 0 0 0 h = max(h, height(w)) return 1 + h 0 0 } } 29

  30. How to implement a tree ? class TreeNode<T>{ T element; } 30

  31. How to implement a tree ? class TreeNode<T>{ T element; ArrayList< TreeNode<T> > children; TreeNode<T> parent; } 31

  32. Another common implementation: ‘ first child, next sibling ’ 32

  33. More common implementation: ‘ first child, next sibling ’ (similar to singly linked lists) class Tree<T>{ TreeNode<T> root; : // inner class class TreeNode<T>{ T element; TreeNode<T> firstChild; TreeNode<T> nextSibling; : } } 33

  34. More common implementation: ‘ first child, next sibling ’ (similar to singly linked lists) class Tree<T>{ TreeNode<T> root; : // inner class class TreeNode<T>{ T element; TreeNode<T> firstChild; TreeNode<T> nextSibling; TreeNode<T> parent; : } } 34

  35. A tree of what? Each node has an element (not illustrated on right) class Tree<T>{ TreeNode<T> root; : // inner class class TreeNode<T>{ T element; TreeNode<T> firstChild; TreeNode<T> nextSibling; : } } 35

  36. Exercise A tree can be represented using lists, as follows: tree = root | ( root listOfSubTrees ) listOfSubTrees = tree | tree listOfSubTrees 36

  37. Exercise A tree can be represented using lists, as follows: tree = root | ( root listOfSubTrees ) listOfSubTrees = tree | tree listOfSubTrees Draw the tree that corresponds to the following list, where the root elements are single digits. ( 6 ( 2 1 7 ) 3 ( 4 5 ) ( 9 8 0 ) ) 37

  38. ( 6 ( 2 1 7 ) 3 ( 4 5 ) ( 9 8 0 ) ) 6 9 4 2 3 8 0 1 7 5 38

  39. ( 6 ( 2 1 7 ) 3 ( 4 5 ) ( 9 8 0 ) ) 6 6 9 2 4 9 3 4 2 3 1 7 8 0 5 8 0 1 7 5 39

  40. ASIDE: Non-rooted trees You will see non-rooted trees mostly commonly when edges are not directed, and there is no natural way to define the ‘root’. You will see examples in COMP 251. Note the two trees below are the same. 3 6 6 0 9 4 2 3 9 4 2 8 0 1 7 5 8 1 7 5 40

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend