1 / 97

Chapter 7. Trees & Binary Trees

Lecture 14. Chapter 7. Trees & Binary Trees. Set ADT Introduction to trees and binary trees. Pictures of trees. File System. Pictures of trees. Pictures of trees. Pictures of trees. Pictures of trees. Pictures of trees. Pictures of trees. unrooted. Pictures of trees. unrooted.

bo-reeves
Download Presentation

Chapter 7. Trees & Binary Trees

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Lecture 14 Chapter 7. Trees & Binary Trees Set ADT Introduction to trees and binary trees ADS2 Lecture 14

  2. Pictures of trees File System

  3. Pictures of trees

  4. ADS2 Lecture 14

  5. Pictures of trees

  6. Pictures of trees

  7. Pictures of trees

  8. Pictures of trees

  9. Pictures of trees unrooted

  10. Pictures of trees unrooted

  11. Pictures of trees unrooted

  12. unrooted Pictures of trees ADS2 Lecture 14 12

  13. Pictures of trees ADS2 Lecture 14 13

  14. Pictures of trees ADS2 Lecture 14 14

  15. Pictures of trees ADS2 Lecture 14 15

  16. Pictures of trees

  17. Pictures of trees

  18. Pictures of trees

  19. Pictures of trees

  20. Pictures of trees

  21. Pictures of trees

  22. Pictures of trees

  23. General Trees • Nonlinear data structure • Natural way to organise data • File system • GUI • Databases • Websites • Inheritance relation in Java classes • Books (chapters, sections, subsections, subsubsections) • “nonlinear” organisational structure • Not just “before” “after” • “above” “below” “part of” • Relationships are typically • Parent • Children • Ancestors • Descendents • Siblings ADS2 Lecture 14

  24. Formal definition General Trees A tree T is a set of nodes with a parent-child relationship Each node has one parent, apart from the root (which has no parent) A tree T is either empty or consists of a node r, the root of T, and a (possibly empty) set of trees whose roots are the children of r An edge in T is a pair of nodes (u,v) where u is parent of v or v is parent of u ADS2 Lecture 14

  25. a e c i b d g j f h R General Trees • a node • an edge • a path • a child • a parent • an ancestor • a descendant • siblings • depth of a node • height of a node • height of a tree • a leaf • an internal node • the root • a subtree Z p n X

  26. a e c i b d g j f h R General Trees • n nodes • n-1 edges • no cycles • unique path from a node to root Z p n X

  27. How might we implement a general tree? ADS2 Lecture 14

  28. a e c i b d g j f h R General Trees Z p Therefore we might implement a tree as follows • Tree<E> • Node<E> root n X • Node: • <E> element • Node<E> parent • ArrayList<Node<E>> children Note: there could be order amongst the children ADS2 Lecture 14

  29. a e c i b d g j f h R Depth General Trees • Tree<E> • Node<E> root • Node: • <E> element • Node<E> parent • ArrayList<Node<E>> children Z p Depth of a node is how far it is from the root. n X depth(Node<E> node) if (node.isRoot()) return 0; return 1 + depth(node.parent());

  30. a e c i b d g j f h R Height General Trees • Tree<E> • Node<E> root • Node: • <E> element • Node<E> parent • ArrayList<Node<E>> children Z p • Height of a node is • If node is a leaf then 0 • Otherwise 1 + the maximum of the height of its children n X height(Node<E> node) if (node.isLeaf()) return 0; int h = 0; for (Node<E> child : node.children()) h = Math.max(h,height(child)); return 1 + h;

  31. a e c i b d g j f h R Preorder Traversal General Trees • Tree<E> • Node<E> root • Node: • <E> element • Node<E> parent • ArrayList<Node<E>> children Z p n X Visit the parent then visit its children

  32. a e c i b d g j f h R Preorder Traversal General Trees • Tree<E> • Node<E> root • Node: • <E> element • Node<E> parent • ArrayList<Node<E>> children Z p n X Visit the parent then visit its children

  33. a e c i b d g j f h R Preorder Traversal General Trees • Tree<E> • Node<E> root • Node: • <E> element • Node<E> parent • ArrayList<Node<E>> children Z p n X Visit the parent then visit its children

  34. a e c i b d g j f h R Preorder Traversal General Trees • Tree<E> • Node<E> root • Node: • <E> element • Node<E> parent • ArrayList<Node<E>> children Z p n X Visit the parent then visit its children

  35. a e c i b d g j f h R Preorder Traversal General Trees • Tree<E> • Node<E> root • Node: • <E> element • Node<E> parent • ArrayList<Node<E>> children Z p n X Visit the parent then visit its children

  36. a e c i b d g j f h R Preorder Traversal General Trees • Tree<E> • Node<E> root • Node: • <E> element • Node<E> parent • ArrayList<Node<E>> children Z p n X Visit the parent then visit its children

  37. a e c i b d g j f h R Preorder Traversal General Trees • Tree<E> • Node<E> root • Node: • <E> element • Node<E> parent • ArrayList<Node<E>> children Z p n X Visit the parent then visit its children

  38. a e c i b d g j f h R Preorder Traversal General Trees • Tree<E> • Node<E> root • Node: • <E> element • Node<E> parent • ArrayList<Node<E>> children Z p n X Visit the parent then visit its children

  39. a e c i b d g j f h R Preorder Traversal General Trees • Tree<E> • Node<E> root • Node: • <E> element • Node<E> parent • ArrayList<Node<E>> children Z p n X Visit the parent then visit its children

  40. a e c i b d g j f h R Preorder Traversal General Trees • Tree<E> • Node<E> root • Node: • <E> element • Node<E> parent • ArrayList<Node<E>> children Z p n X Visit the parent then visit its children

  41. a e c i b d g j f h R Preorder Traversal General Trees • Tree<E> • Node<E> root • Node: • <E> element • Node<E> parent • ArrayList<Node<E>> children Z p n X Visit the parent then visit its children

  42. a e c i b d g j f h R Preorder Traversal General Trees • Tree<E> • Node<E> root • Node: • <E> element • Node<E> parent • ArrayList<Node<E>> children Z p n X Visit the parent then visit its children

  43. a e c i b d g j f h R Preorder Traversal General Trees • Tree<E> • Node<E> root • Node: • <E> element • Node<E> parent • ArrayList<Node<E>> children Z p n X Visit the parent then visit its children

  44. a e c i b d g j f h R Preorder Traversal General Trees • Tree<E> • Node<E> root • Node: • <E> element • Node<E> parent • ArrayList<Node<E>> children Z p n X Visit the parent then visit its children

  45. a e c i b d g j f h R Preorder Traversal General Trees • Tree<E> • Node<E> root • Node: • <E> element • Node<E> parent • ArrayList<Node<E>> children Z p n X Visit the parent then visit its children

  46. a e c i b d g j f h R Preorder Traversal General Trees • Tree<E> • Node<E> root • Node: • <E> element • Node<E> parent • ArrayList<Node<E>> children Z p Visit the parent then visit its children n X preorder(Node<E> node) if (node != null) { visit(node); for (Node<E> child : node.children()) preorder(child); } Whatever “visit” means

  47. a e c i b d g j f h R Preorder Traversal General Trees • Tree<E> • Node<E> root • Node: • <E> element • Node<E> parent • ArrayList<Node<E>> children Z p a c b R X n d p Z i g f h j Visit the parent then visit its children n X preorder(Node<E> node) if (node != null) { visit(node); for (Node<E> child : node.children()) preorder(child); } Whatever “visit” means

  48. a e c i b d g j f h R Preorder Traversal General Trees • Tree<E> • Node<E> root • Node: • <E> element • Node<E> parent • ArrayList<Node<E>> children Z p a c b R X n d p Z i g f h j Visit the parent then visit its children n X preorder(Node<E> node) if (node != null) { visit(node); for (Node<E> child : node.children()) preorder(child); } O(n) Whatever “visit” means

  49. ADS2 Lecture 14

  50. a e c i b d g j f h R Parenthetic Representation General Trees • Tree<E> • Node<E> root • Node: • <E> element • Node<E> parent • ArrayList<Node<E>> children Z p n X (a (c (b ((R),(X),(n))),(d)),(p),(Z),(i (g ((f),(h))),(j))) Also called Caley Notation (I think) and is a preorder print

More Related