310 likes | 466 Views
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).
E N D
What is a tree? • You know… • tall • green • leafy • possibly fruit • branches • roots • I’m sure you’veseen 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)
Some info • The root has no parent • The leaves have no children • The interior nodes have at least one child, and a parent
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
Binary search trees 55 24 83 18 48 65 97 7 31 51 58 70 26 37 68 79 34 42
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)
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!
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
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
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
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
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
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
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
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
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
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
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
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
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