Extensible Tree Discrete Mathematics and Its Applications Baojian Hua

Slides:



Advertisements
Similar presentations
S. Sudarshan Based partly on material from Fawzi Emad & Chau-Wen Tseng
Advertisements

Senem Kumova Metin Spring2009 BINARY TREES && TREE TRAVERSALS Chapter 10 in A Book on C.
©Brooks/Cole, 2003 Chapter 12 Abstract Data Type.
Advanced Data Structures
CS 171: Introduction to Computer Science II
Binary Trees Terminology A graph G = is a collection of nodes and edges. An edge (v 1,v 2 ) is a pair of vertices that are directly connected. A path,
Queue C and Data Structures Baojian Hua
Lists A list is a finite, ordered sequence of data items. Two Implementations –Arrays –Linked Lists.
Dynamically Extensible Data Structures Discrete Mathematics and Its Applications Baojian Hua
Fall 2007CS 2251 Iterators and Tree Traversals. Fall 2007CS 2252 Binary Trees In a binary tree, each node has at most two subtrees A set of nodes T is.
Binary Search Tree C and Data Structures Baojian Hua
CS 104 Introduction to Computer Science and Graphics Problems Data Structure & Algorithms (4) Data Structures 11/18/2008 Yang Song.
Relation Discrete Mathematics and Its Applications Baojian Hua
Breath First Searching & Depth First Searching C and Data Structures Baojian Hua
Graph C and Data Structures Baojian Hua
Stack C and Data Structures Baojian Hua
Graph C and Data Structures Baojian Hua
Queue C and Data Structures Baojian Hua
Extensible Array C and Data Structures Baojian Hua
Tree C and Data Structures Baojian Hua
Graph Traversal Discrete Mathematics and Its Applications Baojian Hua
Graph Discrete Mathematics and Its Applications Baojian Hua
CS21, Tia Newhall Binary Search Trees (BST) 1.Hierarchical data structure with a single pointer to root node 2.Each node has at most two child nodes (a.
Binary Search Tree C and Data Structures Baojian Hua
Graph Traversal C and Data Structures Baojian Hua
Joseph Lindo Trees Sir Joseph Lindo University of the Cordilleras.
1 Chapter 18 Trees Objective To learn general trees and recursion binary trees and recursion tree traversal.
Search Related Algorithms. Graph Code Adjacency List Representation:
CS Data Structures Chapter 5 Trees. Chapter 5 Trees: Outline  Introduction  Representation Of Trees  Binary Trees  Binary Tree Traversals 
Chapter 19 Implementing Trees and Priority Queues Fundamentals of Java.
CISC220 Fall 2009 James Atlas Lecture 13: Trees. Skip Lists.
1 Trees A tree is a data structure used to represent different kinds of data and help solve a number of algorithmic problems Game trees (i.e., chess ),
Chapter 19 Implementing Trees and Priority Queues Fundamentals of Java.
Review for Final Andy Wang Data Structures, Algorithms, and Generic Programming.
 Trees Data Structures Trees Data Structures  Trees Trees  Binary Search Trees Binary Search Trees  Binary Tree Implementation Binary Tree Implementation.
TREES. What is a tree ? An Abstract Data Type which emulates a tree structure with a set of linked nodes The nodes within a tree are organized in a hierarchical.
Trees : Part 1 Section 4.1 (1) Theory and Terminology (2) Preorder, Postorder and Levelorder Traversals.
IKI 10100I: Data Structures & Algorithms Ruli Manurung (acknowledgments to Denny & Ade Azurat) 1 Fasilkom UI Ruli Manurung (Fasilkom UI)IKI10100I: Data.
Binary Trees In computer science, a binary tree is a tree data structure in which each node has at most two children, which are referred to as the left.
Discrete Mathematics Chapter 5 Trees.
Trees, Binary Search Trees, Balanced Trees, Graphs Graph Fundamentals Telerik Algo Academy
CMSC 341 Introduction to Trees. 2/21/20062 Tree ADT Tree definition –A tree is a set of nodes which may be empty –If not empty, then there is a distinguished.
1 Chapter 4 Trees Basic concept How tree are used to implement the file system How tree can be used to evaluate arithmetic expressions How to use trees.
Rooted Tree a b d ef i j g h c k root parent node (self) child descendent leaf (no children) e, i, k, g, h are leaves internal node (not a leaf) sibling.
Trees and Graphs CSE 2320 – Algorithms and Data Structures Vassilis Athitsos University of Texas at Arlington 1.
1 Trees General Trees  Nonrecursive definition: a tree consists of a set of nodes and a set of directed edges that connect pairs of nodes.
Binary Tree Implementation. Binary Search Trees (BST) Nodes in Left subtree has smaller values Nodes in right subtree has bigger values.
CMSC 202, Version 5/02 1 Trees. CMSC 202, Version 5/02 2 Tree Basics 1.A tree is a set of nodes. 2.A tree may be empty (i.e., contain no nodes). 3.If.
Hello Everyone!!! 1. Tree And Graphs 2 Features of Trees  Tree Nodes Each node have 0 or more children A node have must one parent  Binary tree Tree.
1 Trees General Trees  Nonrecursive definition: a tree consists of a set of nodes and a set of directed edges that connect pairs of nodes.
1 Trees : Part 1 Reading: Section 4.1 Theory and Terminology Preorder, Postorder and Levelorder Traversals.
IKI 10100: Data Structures & Algorithms Ruli Manurung (acknowledgments to Denny & Ade Azurat) 1 Fasilkom UI Ruli Manurung (Fasilkom UI)IKI10100: Lecture13.
1 Trees. 2 Trees Trees. Binary Trees Tree Traversal.
TREES From root to leaf. Trees  A tree is a non-linear collection  The elements are in a hierarchical arrangement  The elements are not accessible.
Trees Saurav Karmakar
Chapter 12 – Data Structures
Non Linear Data Structure
Trees Chapter 15.
CSCE 210 Data Structures and Algorithms
Lecture 1 (UNIT -4) TREE SUNIL KUMAR CIT-UPES.
Binary Search Tree (BST)
Tree.
Section 8.1 Trees.
Trees 1: Theory, Models, Generic Heap Algorithms, Priority Queues
Discrete Mathematics and
Trees (part 2) CSE 2320 – Algorithms and Data Structures
Discrete Mathematics and
Chapter 20: Binary Trees.
Data Structures Using C++ 2E
NATURE VIEW OF A TREE leaves branches root. NATURE VIEW OF A TREE leaves branches root.
Presentation transcript:

Extensible Tree Discrete Mathematics and Its Applications Baojian Hua

Binary Tree book chap1chap2 sec11 sec12sec21 subsec111 sec22

Binary Search Tree (BST)

Extensible Tree

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

In Graph unique root

Terminologies node degree leaves root internal node parent & child siblings depth=1 depth=0 depth=2 depth=3

Tree Extension

Binary Tree Representation // For binary tree typedef struct btree *btree; struct btree { poly data; btree left; btree right; }; t leftdataright data leftright

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 …

Extensible Tree Recall the graph representation: 1. Adjacency matrix 2. Adjacency list

Adjacency Matrix ###

Adjacency Matrix Extension #### 4 4

Adjacency List

Adjacency List Extension

Extensible Tree in C

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

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; }; ###

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.

Tree Implementation #1: Adjacency Matrix tree newTree () { tree t = malloc (sizeof (*t)); t->matrix = newMatrix (); // an empty matrix t->hash = newHash (); return t; }

Tree Implementation #1: Adjacency Matrix void insertVertex (tree t, poly v) { int i = matrixExtend (t->matrix); hashInsert (t->hash, v, i); return; } ### ### 4 4

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; } ### #### 4 4

How to build a tree? Starting from an empty tree t Insert all vertices Insert all edges

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

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

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

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

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

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

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

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

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; }

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; } >10->20->3

Tree Representation #2: Adjacency List tree newTree () { tree t = malloc (sizeof (*t)); t->vertices = newLinkedList (); return t; }

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; }

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; }

Tree Representation #2: Adjacency List void insertVertex (tree t, poly data) { vertex v = newVertex (data); linkedListInsertTail (t->vertices, v); return; } >10->20->3 4

Tree Representation #2: Adjacency List void insertVertex (tree t, poly data) { vertex v = newVertex (data); linkedListInsertTail (t->vertices, v); return; } >10->20->3 4

Tree Representation #2: Adjacency List void insertVertex (tree t, poly data) { vertex v = newVertex (data); linkedListInsertTail (t->vertices, v); return; } >10->20->3 4

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; } >10->20->3 4

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; } >10->20->3 4

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; } >10->20->3 4

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; } >10->20->3 4 0->4

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; } >10->20->3 4 0->4

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

Tree Traversal

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

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

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

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; }

Moral Pre-order traversal ==> depth first searching (dfs) Level-order traversal ==> breath first search (bfs)

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