Tree C and Data Structures Baojian Hua

Slides:



Advertisements
Similar presentations
Senem Kumova Metin Spring2009 BINARY TREES && TREE TRAVERSALS Chapter 10 in A Book on C.
Advertisements

Binary Trees, Binary Search Trees COMP171 Fall 2006.
Trees Chapter 8.
ITEC200 – Week08 Trees. 2 Chapter Objectives Students can: Describe the Tree abstract data type and use tree terminology such as.
Fall 2007CS 2251 Trees Chapter 8. Fall 2007CS 2252 Chapter Objectives To learn how to use a tree to represent a hierarchical organization of information.
Trees Chapter 8. Chapter 8: Trees2 Chapter Objectives To learn how to use a tree to represent a hierarchical organization of information To learn how.
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.
Breath First Searching & Depth First Searching C and Data Structures Baojian Hua
1 General Trees & Binary Trees CSC Trees Previous data structures (e.g. lists, stacks, queues) have a linear structure. Linear structures represent.
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
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.
Trees Chapter 8. 2 Tree Terminology A tree consists of a collection of elements or nodes, organized hierarchically. The node at the top of a tree is called.
Trees. Tree Terminology Chapter 8: Trees 2 A tree consists of a collection of elements or nodes, with each node linked to its successors The node at the.
Lecture Objectives  To learn how to use a tree to represent a hierarchical organization of information  To learn how to use recursion to process trees.
1 Joe Meehean. A A B B D D I I C C E E X X A A B B D D I I C C E E X X  Terminology each circle is a node pointers are edges topmost node is the root.
Trees Chapter 8. Chapter 8: Trees2 Chapter Objectives To learn how to use a tree to represent a hierarchical organization of information To learn how.
Chapter 19 Implementing Trees and Priority Queues Fundamentals of Java.
Spring 2010CS 2251 Trees Chapter 6. Spring 2010CS 2252 Chapter Objectives Learn to use a tree to represent a hierarchical organization of information.
CISC220 Fall 2009 James Atlas Lecture 13: Trees. Skip Lists.
Chapter 19 Implementing Trees and Priority Queues Fundamentals of Java.
CMSC 341 Introduction to Trees. 8/3/2007 UMBC CMSC 341 TreeIntro 2 Tree ADT Tree definition  A tree is a set of nodes which may be empty  If not empty,
DATA STRUCTURES AND ALGORITHMS Lecture Notes 5 Prepared by İnanç TAHRALI.
Binary Trees 2 Overview Trees. Terminology. Traversal of Binary Trees. Expression Trees. Binary Search Trees.
Tree (new ADT) Terminology:  A tree is a collection of elements (nodes)  Each node may have 0 or more successors (called children)  How many does a.
Binary Trees Definition A binary tree is: (i) empty, or (ii) a node whose left and right children are binary trees typedef struct Node Node; struct Node.
Trees Chapter 8. 2 Tree Terminology A tree consists of a collection of elements or nodes, organized hierarchically. The node at the top of a tree is called.
Trees, Binary Trees, and Binary Search Trees COMP171.
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.
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.
M180: Data Structures & Algorithms in Java Trees & Binary Trees Arab Open University 1.
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/14/20161 BST Operations Data Structures Ananda Gunawardena.
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.
Copyright © 2012 Pearson Education, Inc. Chapter 20: Binary Trees.
Trees Ellen Walker CPSC 201 Data Structures Hiram College.
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 Joe Meehean. A A B B D D I I C C E E X X A A B B D D I I C C E E X X  Terminology each circle is a node pointers are edges topmost node is the root.
Extensible Tree Discrete Mathematics and Its Applications Baojian Hua
1 Trees. Objectives To understand the concept of trees and the standard terminology used to describe them. To appreciate the recursive nature of a tree.
TREES General trees Binary trees Binary search trees AVL trees Balanced and Threaded trees.
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.
Lecture 9 Binary Trees Trees General Definition Terminology
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 20: Binary Trees.
1 CMSC 341 Introduction to Trees Textbook sections:
IKI 10100: Data Structures & Algorithms Ruli Manurung (acknowledgments to Denny & Ade Azurat) 1 Fasilkom UI Ruli Manurung (Fasilkom UI)IKI10100: Lecture13.
What is a Tree? Formally, we define a tree T as a set of nodes storing elements such that the nodes have a parent-child relationship, that satisfies the.
DS.T.1 Trees Chapter 4 Overview Tree Concepts Traversals Binary Trees Binary Search Trees AVL Trees Splay Trees B-Trees.
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.
Chapter 12 – Data Structures
Trees Chapter 15.
CC 215 Data Structures Trees
Trees CSE 2320 – Algorithms and Data Structures Vassilis Athitsos
Tree.
Section 8.1 Trees.
Data Structures & Algorithm Design
CSE 373 Data Structures Lecture 7
Chapter 20: Binary Trees.
Chapter 21: Binary Trees.
General Trees & Binary Trees
Trees CSE 373 Data Structures.
Trees (part 2) CSE 2320 – Algorithms and Data Structures
Chapter 20: Binary Trees.
Trees CSE 373 Data Structures.
Trees CSE 373 Data Structures.
NATURE VIEW OF A TREE leaves branches root. NATURE VIEW OF A TREE leaves branches root.
Presentation transcript:

Tree C and Data Structures Baojian Hua

What ’ s a Tree? book chap1chap2 chap3 sec11sec12sec21 subsec111

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

What ’ s a Tree? book chap1chap2 chap3 sec11sec12sec21 subsec111 unique root

Terminologies book chap1chap2 chap3 sec11sec12sec21 subsec111 node degree leaves root internal node parent & child siblings depth=1 depth=0 depth=2 depth=3

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

Examples book chap1chap2 sec11 sec12sec21 subsec111 sec22 book chap1 sec11 subsec111

Properties of Binary Tree

Properties of Binary Tree

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

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

Implementation // in file “btree.c” #include “btree.h” struct btreeStruct { poly data; btree left; btree right; }; t leftdataright

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

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

How to build a tree? book chap1chap2 sec11 sec12sec21 subsec111 sec22

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

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

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

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 ’ ); …

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

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.

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

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”

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”

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”

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”

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”

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”

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”

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”

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”

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”

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

Example t left “a”“a” right /\ “b”“b” right/\ “c”“c” “d”“d”

Example t left “a”“a” right /\ “b”“b” right/\ “c”“c” “d”“d” t

Example t left “a”“a” right /\ “b”“b” right/\ “c”“c” “d”“d” t->l print out “a”

Example t left “a”“a” right /\ “b”“b” right/\ “c”“c” “d”“d” t->r t->l print out “a”

Example t left “a”“a” right /\ “b”“b” right/\ “c”“c” “d”“d” t->r print out “a” print out “b”

Example t left “a”“a” right /\ “b”“b” right/\ “c”“c” “d”“d” t->l->r t->r print out “a” print out “b”

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”

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”

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”

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