Download presentation
Presentation is loading. Please wait.
1
Tree C and Data Structures Baojian Hua bjhua@ustc.edu.cn
2
What ’ s a Tree? book chap1chap2 chap3 sec11sec12sec21 subsec111
3
Definition of Tree A tree is a collection of nodes, which satisfies: there exists a unique root node r other nodes are classified into n (n>=0) disjoint sets T_1, …, T_n, and every T_i is also a tree. T_1, …, T_n are called sub-trees of r. Moral: Recursive definition Sub-trees disjoint
4
What ’ s a Tree? book chap1chap2 chap3 sec11sec12sec21 subsec111 unique root
5
Terminologies book chap1chap2 chap3 sec11sec12sec21 subsec111 node degree leaves root internal node parent & child siblings depth=1 depth=0 depth=2 depth=3
6
Binary Tree A binary tree is a collection of nodes, which satisfies: there exists a unique root node r other nodes are classified into n (0<=n<=2) disjoint ordered sets T_1, …, T_n, and every set T_i is also a binary tree. To note: 0<=Degree(any node)<=2 the order of sub-tree is relevant
7
Examples book chap1chap2 sec11 sec12sec21 subsec111 sec22 book chap1 sec11 subsec111
8
Properties of Binary Tree
9
Properties of Binary Tree
10
Full and Complete Binary Tree A full binary tree is a binary tree of depth k and 2^{k+1}-1 nodes each node either has degree 2 (internal nodes) or degree 0 (leaves) A n-node complete binary tree is a tree with nodes number of full binary tree
11
Abstract Data Types in C: Interface // in file “btree.h” #ifndef BTREE_H #define BTREE_H typedef struct btreeStruct *btree; btree newTree (); btree newTree2 (btree left, btree right, poly data); void insert (btree t, poly parent, poly child, char pos); void preOrder (btree t, void (*visit)(poly)); void inOrder (btree t, void (*visit)(poly)); void postOrder (btree t, void (*visit)(poly)); void levelOrder (btree t, void (*visit)(poly)); #endif
12
Implementation // in file “btree.c” #include “btree.h” struct btreeStruct { poly data; btree left; btree right; }; t leftdataright
13
Operations: “ new ” // “new” creates an empty new binary tree. Just // as the case for linked list, we may add a head // node. btree newTree () { btree t = (btree)malloc (sizeof (*t)); t->data = NULL; t->left = NULL; t->right = NULL; return t; } t leftdataright
14
Operations: “ new2 ” // “newTree2” creates an tree node with fields // properly initialized. btree newTree2 (btree left, btree right, poly data) { btree t = (btree)malloc (sizeof (*t)); t->data = data; t->left = left; t->right = right; return t; } t leftdataright
15
How to build a tree? book chap1chap2 sec11 sec12sec21 subsec111 sec22
16
How to build a tree? A tree can be built in a step-by-step way first new an empty tree t to insert edge (parent, child, ‘ l ’ ) or (parent, child, ‘ r ’ ), we write a function insert insert (t, parent, child, pos) puts the child node at the pos field of parent node in t if parent does not exist or has already had a pos filed, an error occurs
17
Operations: “ insert ” // a nearly correct version: btree insert (btree t, poly parent, poly child, char pos) { btree temp = search (t, parent); if (pos == ‘l’) { btree p = new2 (temp->left, NULL, child); temp->left = p; } else {} // similar case for pos == ‘r’ return t; }
18
Operations: “ search ” btree search (btree t, poly data) { btree p; if (t==NULL) return NULL; else if (t->data == data) // what’s “==“ ? return t; else { p = search (t->left, data); return (p) ? (p) : (search (t->right, data)); }
19
Client Code Create this tree: book chap1chap2 sec11 sec12sec21 subsec111 sec22 // the creation process: t = new (); t = insert (t, NULL, book, ‘ l ’ ); t = insert (t, book, chap1, ‘ l ’ ); t =insert (t, book, chap2, ‘ r ’ ); t=insert (t, chap1, sec11, ‘ l ’ ); t=insert (t, chap1, sec12, ‘ r ’ ); …
20
What ’ s wrong with “ insert ” ? Many subtle conditions: what if the search fail? what if the arguments parent or child be NULL? what if the pos field unequal to ‘ l ’ or ‘ r ’ ? … All these should be checked in real production code a style called defensive programming worth of great recommendation
21
Tree Traversal book chap1chap2 Traversal: a systematic way to visit all nodes in a tree. For instance, the visit strategies for the right tree may be: book, chap1, chap2 or: chap1, book, chap2 and so on. Next, we ’ ll study four of them, namely: pre-order, in- order, post-order and level-order.
22
Pre-order Traversal Pre-order: first visit the root node of the tree and then left subtree and finally right subtree Ex: book, chap1, chap2 book chap1chap2
23
Pre-order Traversal void preOrder (btree t, void (*visit)(poly)) { if (t) { visit (t->data); preOrder (t->left, visit); preOrder (t->right, visit); } } // try visit = printf t left “a”“a” right /\ “b”“b” right/\ “c”“c” “d”“d”
24
Pre-order Traversal void preOrder (btree t, void (*visit)(poly)) { if (t) { visit (t->data); preOrder (t->left, visit); preOrder (t->right, visit); } // try visit = printf t left “a”“a” right /\ “b”“b” right/\ “c”“c” “d”“d”
25
Pre-order Traversal void preOrder (btree t, void (*visit)(poly)) { if (t) { visit (t->data); preOrder (t->left, visit); preOrder (t->right, visit); } // print out “a” t left “a”“a” right /\ “b”“b” right/\ “c”“c” “d”“d”
26
Pre-order Traversal void preOrder (btree t, void (*visit)(poly)) { if (t) { visit (t->data); preOrder (t->left, visit); preOrder (t->right, visit); } // print out “a” t left “a”“a” right /\ “b”“b” right/\ “c”“c” “d”“d”
27
Pre-order Traversal void preOrder (btree t, void (*visit)(poly)) { if (t) { visit (t->data); preOrder (t->left, visit); preOrder (t->right, visit); } // print out “a” // print out “b” t left “a”“a” right /\ “b”“b” right/\ “c”“c” “d”“d”
28
Pre-order Traversal void preOrder (btree t, void (*visit)(poly)) { if (t) { visit (t->data); preOrder (t->left, visit); preOrder (t->right, visit); } // print out “a” // print out “b” // print out “d” t left “a”“a” right /\ “b”“b” right/\ “c”“c” “d”“d”
29
Pre-order Traversal void preOrder (btree t, void (*visit)(poly)) { if (t) { visit (t->data); preOrder (t->left, visit); preOrder (t->right, visit); } // print out “a” // print out “b” // print out “d” t left “a”“a” right /\ “b”“b” right/\ “c”“c” “d”“d”
30
Pre-order Traversal void preOrder (btree t, void (*visit)(poly)) { if (t) { visit (t->data); preOrder (t->left, visit); preOrder (t->right, visit); } // print out “a” // print out “b” // print out “d” // print out “c” t left “a”“a” right /\ “b”“b” right/\ “c”“c” “d”“d”
31
Pre-order Traversal void preOrder (btree t, void (*visit)(poly)) { if (t) { visit (t->data); preOrder (t->left, visit); preOrder (t->right, visit); } // print out “a” // print out “b” // print out “d” // print out “c” t left “a”“a” right /\ “b”“b” right/\ “c”“c” “d”“d”
32
Pre-order Traversal void preOrder (btree t, void (*visit)(poly)) { if (t) { visit (t->data); preOrder (t->left, visit); preOrder (t->right, visit); } // print out “a” // print out “b” // print out “d” // print out “c” t left “a”“a” right /\ “b”“b” right/\ “c”“c” “d”“d”
33
Moral preOrder is a recursive algorithm: defined on recursively defined data structures system (machine) keeps a stack to control the recursive order Generally, recursion are more elegant, easy- to-write and easy-to-reason than corresponding iterative ones A powerful programming idiom to recommend Some languages even encourage this by removing “ while ” and “ for ” completely
34
Level-order Traversal void levelOrder (btree t, void (*visit)(poly)) { queue q = newQueue (); if (t) enQueue (q, t); while (!queueIsEmpty(q)) { btree temp = deQueue (q); visit (temp->data); if (temp->left) enQueue (q, temp->left); if (temp->right) enQueue (q, temp->right); } return; }
35
Example t left “a”“a” right /\ “b”“b” right/\ “c”“c” “d”“d”
36
Example t left “a”“a” right /\ “b”“b” right/\ “c”“c” “d”“d” t
37
Example t left “a”“a” right /\ “b”“b” right/\ “c”“c” “d”“d” t->l print out “a”
38
Example t left “a”“a” right /\ “b”“b” right/\ “c”“c” “d”“d” t->r t->l print out “a”
39
Example t left “a”“a” right /\ “b”“b” right/\ “c”“c” “d”“d” t->r print out “a” print out “b”
40
Example t left “a”“a” right /\ “b”“b” right/\ “c”“c” “d”“d” t->l->r t->r print out “a” print out “b”
41
Example t left “a”“a” right /\ “b”“b” right/\ “c”“c” “d”“d” t->l->r print out “a” print out “b” print out “c”
42
Example t left “a”“a” right /\ “b”“b” right/\ “c”“c” “d”“d” t->l->r print out “a” print out “b” print out “c”
43
Example t left “a”“a” right /\ “b”“b” right/\ “c”“c” “d”“d” print out “a” print out “b” print out “c” print out “d”
44
Summary Tree is a non-linear data structure every element has 0, 1 or 2 successors structure could be inductively defined Operations could also be inductive defined recursive functions
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.