Download presentation
Presentation is loading. Please wait.
Published byAmberlynn Ellis Modified over 8 years ago
1
Extensible Tree Discrete Mathematics and Its Applications Baojian Hua bjhua@ustc.edu.cn
2
Binary Tree book chap1chap2 sec11 sec12sec21 subsec111 sec22
3
Binary Search Tree (BST) 10 715 5 812 3 37
4
Extensible Tree 1 23 4 567 8
5
Definition of Extensible Tree An extensible tree is a collection of nodes: 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: n may not be determined statically (dynamically extensible) Recursive definition Sub-trees disjoint
6
In Graph 1 23 4 567 8 unique root
7
Terminologies 1 23 4 567 8 node degree leaves root internal node parent & child siblings depth=1 depth=0 depth=2 depth=3
8
Tree Extension 1 23 4 567 8 9
9
Binary Tree Representation // For binary tree typedef struct btree *btree; struct btree { poly data; btree left; btree right; }; t leftdataright data leftright
10
Extensible Tree // For general tree typedef struct tree *tree; struct tree { poly data; tree child1; tree child2; …; tree childn; }; // ugly and // not extensible data child1childnchild2... t datachild1child2childn …
11
Extensible Tree Recall the graph representation: 1. Adjacency matrix 2. Adjacency list 0 12 3
12
Adjacency Matrix 0 12 3 ### 0 1 2 3 0 1 23
13
Adjacency Matrix Extension 0 12 3 0 1 2 3 0123 4 #### 4 4
14
Adjacency List 0 12 3 0 1 2 3 123
15
Adjacency List Extension 0 12 34 0 1 2 3 4 1234
16
Extensible Tree in C
17
Abstract Data Types in C: Interface // in file “tree.h” #ifndef TREE_H #define TREE_H typedef struct btree *btree; typedef void (*visitTy) (poly); tree newTree (); void insertVertex (tree t, poly v); void insertEdge (tree t, poly from, poly to); void preOrder (btree t, poly r, visitTy visit); void levelOrder (btree t, poly r, visitTy visit); #endif
18
Tree Implementation #1: Adjacency Matrix // adjacency matrix-based implementation #include “matrix.h” #include “hash.h” #include “tree.h” struct tree { matrix matrix; // remember the index hash hash; }; ### 0 1 2 3 0 1 23
19
Matrix Interface // file “matrix.h” #ifndef MATRIX_H #define MATRIX_H typedef struct matrix *matrix; matrix newMatrix (); void matrixInsert (matrix m, int i, int j); int matrixExtend (matrix m); #endif // Implementation could make use of a two- // dimensional extensible array, leave to you.
20
Tree Implementation #1: Adjacency Matrix tree newTree () { tree t = malloc (sizeof (*t)); t->matrix = newMatrix (); // an empty matrix t->hash = newHash (); return t; }
21
Tree Implementation #1: Adjacency Matrix void insertVertex (tree t, poly v) { int i = matrixExtend (t->matrix); hashInsert (t->hash, v, i); return; } ### 0 1 2 3 0 1 230 1 2 3 0123 ### 4 4
22
Tree Implementation #1: Adjacency Matrix void insertEdge (tree t, poly from, poly to) { int f = hashLookup (t->hash, from); int t = hashLookup (t->hash, to); matrixInsert (t->matrix, f, t); return; } ### 0 1 2 3 0 1 230 1 2 3 0123 #### 4 4
23
How to build a tree? Starting from an empty tree t Insert all vertices Insert all edges
24
Client Code int main () { tree t = newTree (); insertVertex (t, “x”); insertVertex (t, “y”); insertVertex (t, “z”); insertVertex (t, “u”); insertEdge (t, “x”, “y”); insertEdge (t, “x”, “z”); insertEdge (t, “x”, “u”); } x yz u
25
Client Code int main () { tree t = newTree (); insertVertex (t, “x”); insertVertex (t, “y”); insertVertex (t, “z”); insertVertex (t, “u”); insertEdge (t, “x”, “y”); insertEdge (t, “x”, “z”); insertEdge (t, “x”, “u”); } x yz u 0
26
Client Code int main () { tree t = newTree (); insertVertex (t, “x”); insertVertex (t, “y”); insertVertex (t, “z”); insertVertex (t, “u”); insertEdge (t, “x”, “y”); insertEdge (t, “x”, “z”); insertEdge (t, “x”, “u”); } x yz u 0 1
27
Client Code int main () { tree t = newTree (); insertVertex (t, “x”); insertVertex (t, “y”); insertVertex (t, “z”); insertVertex (t, “u”); insertEdge (t, “x”, “y”); insertEdge (t, “x”, “z”); insertEdge (t, “x”, “u”); } x yz u 0 12
28
Client Code int main () { tree t = newTree (); insertVertex (t, “x”); insertVertex (t, “y”); insertVertex (t, “z”); insertVertex (t, “u”); insertEdge (t, “x”, “y”); insertEdge (t, “x”, “z”); insertEdge (t, “x”, “u”); } x yz u 0 12 3
29
Client Code int main () { tree t = newTree (); insertVertex (t, “x”); insertVertex (t, “y”); insertVertex (t, “z”); insertVertex (t, “u”); insertEdge (t, “x”, “y”); insertEdge (t, “x”, “z”); insertEdge (t, “x”, “u”); } x yz u 0 12 3
30
Client Code int main () { tree t = newTree (); insertVertex (t, “x”); insertVertex (t, “y”); insertVertex (t, “z”); insertVertex (t, “u”); insertEdge (t, “x”, “y”); insertEdge (t, “x”, “z”); insertEdge (t, “x”, “u”); } x yz u 0 12 3
31
Client Code int main () { tree t = newTree (); insertVertex (t, “x”); insertVertex (t, “y”); insertVertex (t, “z”); insertVertex (t, “u”); insertEdge (t, “x”, “y”); insertEdge (t, “x”, “z”); insertEdge (t, “x”, “u”); } x yz u 0 12 3
32
Tree Representation #2: Adjacency List #include “linkedList.h” #include “tree.h” struct tree{ linkedList vertices; }; typedef struct vertex *vertex; typedef struct edge *edge; struct vertex { poly data; linkedList edges; }; struct edge { vertex from; vertex to; }
33
Tree Representation #2: Adjacency List #include “linkedList.h” #include “tree.h” struct tree{ linkedList vertices; }; typedef struct vertex *vertex; typedef struct edge *edge; struct vertex { poly data; linkedList edges; }; struct edge { vertex from; vertex to; } 0 1 2 3 0->10->20->3
34
Tree Representation #2: Adjacency List tree newTree () { tree t = malloc (sizeof (*t)); t->vertices = newLinkedList (); return t; }
35
Tree Representation #2: Adjacency List // but we’ve vertices and edges vertex newVertex (poly data) { vertex v = malloc (sizeof (*v)); v->data = data; v->edges = newLinkedList (); return v; }
36
Tree Representation #2: Adjacency List // but we’ve vertices and edges edge newEdge (vertex from, vertex to) { edge e = malloc (sizeof (*e)); e->from = from; e->to = to; return e; }
37
Tree Representation #2: Adjacency List void insertVertex (tree t, poly data) { vertex v = newVertex (data); linkedListInsertTail (t->vertices, v); return; } 0 1 2 3 0->10->20->3 4
38
Tree Representation #2: Adjacency List void insertVertex (tree t, poly data) { vertex v = newVertex (data); linkedListInsertTail (t->vertices, v); return; } 0 1 2 3 0->10->20->3 4
39
Tree Representation #2: Adjacency List void insertVertex (tree t, poly data) { vertex v = newVertex (data); linkedListInsertTail (t->vertices, v); return; } 0 1 2 3 0->10->20->3 4
40
Tree Representation #2: Adjacency List void insertEdge (tree t, poly from, poly to) { vertex vf = lookupVertex (t, from); vertex vt = lookupVertex (t, to); edge e = newEdge (vf, vt); linkedListInsertTail (vf->edges, e); return; } 0 1 2 3 0->10->20->3 4
41
Tree Representation #2: Adjacency List void insertEdge (tree t, poly from, poly to) { vertex vf = lookupVertex (t, from); vertex vt = lookupVertex (t, to); edge e = newEdge (vf, vt); linkedListInsertTail (vf->edges, e); return; } 0 1 2 3 0->10->20->3 4
42
Tree Representation #2: Adjacency List void insertEdge (tree t, poly from, poly to) { vertex vf = lookupVertex (t, from); vertex vt = lookupVertex (t, to); edge e = newEdge (vf, vt); linkedListInsertTail (vf->edges, e); return; } 0 1 2 3 0->10->20->3 4
43
Tree Representation #2: Adjacency List void insertEdge (tree t, poly from, poly to) { vertex vf = lookupVertex (t, from); vertex vt = lookupVertex (t, to); edge e = newEdge (vf, vt); linkedListInsertTail (vf->edges, e); return; } 0 1 2 3 0->10->20->3 4 0->4
44
Tree Representation #2: Adjacency List void insertEdge (tree t, poly from, poly to) { vertex vf = lookupVertex (t, from); vertex vt = lookupVertex (t, to); edge e = newEdge (vf, vt); linkedListInsertTail (vf->edges, e); return; } 0 1 2 3 0->10->20->3 4 0->4
45
Client Code int main () { tree t = newTree (); insertVertex (t, “x”); insertVertex (t, “y”); insertVertex (t, “z”); insertVertex (t, “u”); insertEdge (t, “x”, “y”); insertEdge (t, “x”, “z”); insertEdge (t, “x”, “u”); } x yz u
46
Tree Traversal
47
Abstract Data Types in C: Interface // in file “tree.h” #ifndef TREE_H #define TREE_H typedef struct btree *btree; typedef void (*visitTy) (poly); tree newTree (); void insertVertex (tree t, poly v); void insertEdge (tree t, poly from, poly to); void preOrder (btree t, poly r, visitTy visit); void levelOrder (btree t, poly r, visitTy visit); #endif
48
Pre-order Traversal void preOrder (tree t, poly r, visitTy visit) { vertex rv = lookupVertex (t, r); visit (rv->data); linkedList edges = rv->edges; while (edges) { preOrder (t, edges->data->to, visit); edges = edges->next; } return; } // try isit = printf // in-order and post-order similar
49
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
50
Level-order Traversal void levelOrder (tree t, poly r, visitTy visit) { queue q = newQueue (); vertex vr = lookupVertex (t, r); enQueue (q, vr); while (! queueIsEmpty(q)) { vertex x = deQueue (q); visit (x->data); linkeList edges = x->edges; while (edges){ enQueue (q, edges->data->to); edges = edges->next; }
51
Moral Pre-order traversal ==> depth first searching (dfs) Level-order traversal ==> breath first search (bfs)
52
Summary Extensible tree is a beautiful and elegant data structure every vertex has arbitrary number of children structure is inductively defined In all cases, such a data structure is worth of recommendation Could be generalized to graphs Next class
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.