1 / 31

Trees

Trees. What is a tree?. You know… tall green leafy possibly fruit branches roots I’m sure you’ve seen them. In the context of C (and CS in general). Trees are a Data Structure (Like a linked list) They Are upside-down Have one root Have branches Have leaves (No fruit).

leo-fox
Download Presentation

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. Trees

  2. What is a tree? • You know… • tall • green • leafy • possibly fruit • branches • roots • I’m sure you’veseen them

  3. In the context of C (and CS in general) • Trees are a Data Structure (Like a linked list) • They • Are upside-down • Have one root • Have branches • Have leaves • (No fruit)

  4. A Tree (In general)

  5. Nodes

  6. Edges (Links (Pointers))

  7. The Root

  8. Leaves

  9. Branches (A.K.A. Sub-Trees)

  10. Branches (A.K.A. Sub-Trees)

  11. Branches (A.K.A. Sub-Trees)

  12. Parent / Children

  13. Some info • The root has no parent • The leaves have no children • The interior nodes have at least one child, and a parent

  14. Types of trees • Binary trees • Every node has (at most) two childen • Trinary trees • Every node has (at most) three childen • N-ary trees • Every node has as many children as it wants/needs

  15. We care about binary trees

  16. Binary search trees 55 24 83 18 48 65 97 7 31 51 58 70 26 37 68 79 34 42

  17. Binary Trees typedef struct S_tree { int value; // For example struct S_tree *left, *right; } tree; • Just like a linked list, but instead of one pointer, it has two! (left and right)

  18. In Order Traversal • Printing an entire binary search tree. void print_tree(tree *t) { if (t == NULL) return; print_tree(t->left); printf(“%d\n”,t->val); print_tree(t->right); } Recursion!

  19. Traversing a Tree Start with the root. Traverse Left Subtree Print Node Value Traverse Right Subtree 24 18 48 7 31 51 26 37 34 42

  20. Traversing a Tree Start with the root. Traverse Left Subtree Print Node Value Traverse Right Subtree 24 18 48 7 31 51 26 37 34 42

  21. Traversing a Tree Start with the root. Traverse Left Subtree Print Node Value Traverse Right Subtree 24 18 48 7 31 51 26 37 34 42 7

  22. Traversing a Tree Start with the root. Traverse Left Subtree Print Node Value Traverse Right Subtree 24 18 48 7 31 51 26 37 34 42 7 18

  23. Traversing a Tree Start with the root. Traverse Left Subtree Print Node Value Traverse Right Subtree 24 18 48 7 31 51 26 37 34 42 7 18 24

  24. Traversing a Tree Start with the root. Traverse Left Subtree Print Node Value Traverse Right Subtree 24 18 48 7 31 51 26 37 34 42 7 18 24 26

  25. Traversing a Tree Start with the root. Traverse Left Subtree Print Node Value Traverse Right Subtree 24 18 48 7 31 51 26 37 34 42 7 18 24 26 31

  26. Traversing a Tree Start with the root. Traverse Left Subtree Print Node Value Traverse Right Subtree 24 18 48 7 31 51 26 37 34 42 7 18 24 26 31 34

  27. Traversing a Tree Start with the root. Traverse Left Subtree Print Node Value Traverse Right Subtree 24 18 48 7 31 51 26 37 34 42 7 18 24 26 31 34 37

  28. Traversing a Tree Start with the root. Traverse Left Subtree Print Node Value Traverse Right Subtree 24 18 48 7 31 51 26 37 34 42 7 18 24 26 31 34 37 42

  29. Traversing a Tree Start with the root. Traverse Left Subtree Print Node Value Traverse Right Subtree 24 18 48 7 31 51 26 37 34 42 7 18 24 26 31 34 37 42 48

  30. Traversing a Tree Start with the root. Traverse Left Subtree Print Node Value Traverse Right Subtree 24 18 48 7 31 51 26 37 34 42 7 18 24 26 31 34 37 42 48 51

  31. Traversing a Tree Start with the root. Traverse Left Subtree Print Node Value Traverse Right Subtree 24 18 48 7 31 51 26 37 34 42 7 18 24 26 31 34 37 42 48 51

More Related