Presentation is loading. Please wait.

Presentation is loading. Please wait.

Trees. What is a tree? You know…  tall  green  leafy  possibly fruit  branches  roots  I’m sure you’ve seen them.

Similar presentations


Presentation on theme: "Trees. What is a tree? You know…  tall  green  leafy  possibly fruit  branches  roots  I’m sure you’ve seen them."— Presentation transcript:

1 Trees

2 What is a tree? You know…  tall  green  leafy  possibly fruit  branches  roots  I’m sure you’ve seen 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

11

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 97 58 26 3442 517 6879 70 37 2483 48 65 31 18 55

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 26 3442 517 37 24 48 31 18 Start with the root. Traverse Left Subtree Print Node Value Traverse Right Subtree

20 Traversing a Tree 26 3442 517 37 24 48 31 18 Start with the root. Traverse Left Subtree Print Node Value Traverse Right Subtree

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

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

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

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

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

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

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

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

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

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

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


Download ppt "Trees. What is a tree? You know…  tall  green  leafy  possibly fruit  branches  roots  I’m sure you’ve seen them."

Similar presentations


Ads by Google