Been-Chian Chien,Wei-Pang Yang and Wen-Yang Lin 5-1 Chapter 5 Trees Introduction to Data Structures CHAPTER 5 Trees 5.1 Introduction 5.2 Binary Trees 5.3.

Slides:



Advertisements
Similar presentations
1 abstract containers hierarchical (1 to many) graph (many to many) first ith last sequence/linear (1 to 1) set.
Advertisements

Binary Trees, Binary Search Trees CMPS 2133 Spring 2008.
Binary Trees, Binary Search Trees COMP171 Fall 2006.
Trees Chapter 8.
Chapter 5. Trees Definition: A tree is a finite set of one or more nodes such that: –There is a specially designated node called the root. –The remaining.
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.
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.
Binary Trees A binary tree is made up of a finite set of nodes that is either empty or consists of a node called the root together with two binary trees,
Review of Chapter 5 張啟中. Definition of Tree A tree is a finite set of one or more nodes such that A tree is a finite set of one or more nodes such that.
Lec 15 April 9 Topics: l binary Trees l expression trees Binary Search Trees (Chapter 5 of text)
© 2006 Pearson Addison-Wesley. All rights reserved11 A-1 Chapter 11 Trees.
1 abstract containers hierarchical (1 to many) graph (many to many) first ith last sequence/linear (1 to 1) set.
Fundamentals of Python: From First Programs Through Data Structures
CS Data Structures Chapter 5 Trees. Additional Binary Tree Operations (1/7)  Copying Binary Trees  we can modify the postorder traversal algorithm.
Department of Computer Eng. & IT Amirkabir University of Technology (Tehran Polytechnic) Data Structures Lecturer: Abbas Sarraf Trees.
10. Binary Trees A. Introduction: Searching a linked list.
Binary and Other Trees CSE, POSTECH. 2 2 Linear Lists and Trees Linear lists are useful for serially ordered data – (e 1,e 2,e 3,…,e n ) – Days of week.
Chapter 5 Trees: Outline
Bioinformatics Programming 1 EE, NCKU Tien-Hao Chang (Darby Chang)
CHAPTER 71 TREE. Binary Tree A binary tree T is a finite set of one or more nodes such that: (a) T is empty or (b) There is a specially designated node.
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.
Advanced Algorithms Analysis and Design Lecture 8 (Continue Lecture 7…..) Elementry Data Structures By Engr Huma Ayub Vine.
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.
12-CRS-0106 REVISED 8 FEB 2013 CSG2A3 ALGORITMA dan STRUKTUR DATA.
CS Data Structures Chapter 5 Trees. Chapter 5 Trees: Outline  Introduction  Representation Of Trees  Binary Trees  Binary Tree Traversals 
Lecture 10 Trees –Definiton of trees –Uses of trees –Operations on a tree.
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.
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 ),
Binary Trees. Binary Tree Finite (possibly empty) collection of elements A nonempty binary tree has a root element The remaining elements (if any) are.
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, Binary Search Trees RIZWAN REHMAN CENTRE FOR COMPUTER STUDIES DIBRUGARH UNIVERSITY.
Tree Data Structures. Introductory Examples Willliam Willliam BillMary Curt Marjorie Richard Anne Data organization such that items of information are.
Tree Data Structures.
Binary Trees. 2 Parts of a binary tree A binary tree is composed of zero or more nodes In Java, a reference to a binary tree may be null Each node contains:
1 10. Binary Trees Read Sec A. Introduction: Searching a linked list. 1. Linear Search /* Linear search a list for a particular item */ 1. Set.
Data Structures & Algorithm Analysis Muhammad Hussain Mughal Trees. Binary Trees. Reading: Chap.4 ( ) Weiss.
Review 1 Queue Operations on Queues A Dequeue Operation An Enqueue Operation Array Implementation Link list Implementation Examples.
Binary Trees Chapter 10. Introduction Previous chapter considered linked lists –nodes connected by two or more links We seek to organize data in a linked.
Lecture - 10 on Data Structures. 6:05:57 PM Prepared by, Jesmin Akhter, Lecturer, IIT,JU.
12-CRS-0106 REVISED 8 FEB 2013 CSG2A3 ALGORITMA dan STRUKTUR DATA.
Introduction to Trees IT12112 Lecture 05 Introduction Tree is one of the most important non-linear data structures in computing. It allows us to implement.
CSCE 3110 Data Structures & Algorithm Analysis Rada Mihalcea Trees. Binary Trees. Reading: Chap.4 ( ) Weiss.
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.
Trees. 2 Root leaf CHAPTER 5 3 Definition of Tree n A tree is a finite set of one or more nodes such that: n There is a specially designated node called.
CHAPTER 5 TREE CSEB324 DATA STRUCTURES & ALGORITHM.
Chapter 10: Trees A tree is a connected simple undirected graph with no simple circuits. Properties: There is a unique simple path between any 2 of its.
§3 Binary Tree Traversals —— visit each node exactly once L ::= move left ; R ::= move right ; V ::= visit the node. We will always traverse the left.
Trees Ellen Walker CPSC 201 Data Structures Hiram College.
Foundation of Computing Systems Lecture 4 Trees: Part I.
Trees (Unit 7).
Chapter 7 Trees_ Part2 TREES. Depth and Height 2  Let v be a node of a tree T. The depth of v is the number of ancestors of v, excluding v itself. 
Chapter 8 (Lafore’s Book) Binary Tree Hwajung Lee.
1 Trees. 2 Trees Trees. Binary Trees Tree Traversal.
 آشنايي با درخت  درخت هاي دودويي  پيمايش درختان  هرم  جنگل اهداف 1.
Chapter 05 Trees (Part II). Array Representation We can use an array to represent a complete binary tree. Lemma 5.4 ▫If a complete binary tree with.
CSCE 3110 Data Structures & Algorithm Analysis
Trees Chapter 15.
Binary Tree.
CMSC 341 Introduction to Trees.
EEE2108: Programming for Engineers Chapter 5. Trees
abstract containers sequence/linear (1 to 1) hierarchical (1 to many)
Binary Trees, Binary Search Trees
Binary Trees, Binary Search Trees
Binary Trees, Binary Search Trees
NATURE VIEW OF A TREE leaves branches root. NATURE VIEW OF A TREE leaves branches root.
Tree (new ADT) Terminology: A tree is a collection of elements (nodes)
Presentation transcript:

Been-Chian Chien,Wei-Pang Yang and Wen-Yang Lin 5-1 Chapter 5 Trees Introduction to Data Structures CHAPTER 5 Trees 5.1 Introduction 5.2 Binary Trees 5.3 Binary Tree Traversal 5.4 Additional Binary Tree Operations 5.5 Threaded Binary Trees 5.6 Heaps 5.7 Binary Search Trees 5.8 Selection Trees 5.9 Forests 5.10 Set Representations 5.11 Counting Binary Trees

Been-Chian Chien,Wei-Pang Yang and Wen-Yang Lin 5-2 Chapter 5 Trees Chapter 1 Basic Concepts Chapter 2 Arrays Chapter 3 Stacks and Queues Chapter 4 Linked Lists Chapter 5 Trees Chapter 6 Graph Chapter 7 Sorting Chapter 8 Hashing Chapter 9 Heap Structures Chapter 10 Search Structures Contents

Been-Chian Chien,Wei-Pang Yang and Wen-Yang Lin 5-3 Chapter 5 Trees 5.1 Introduction Def: Tree A tree T is a finite set of one or more nodes such that (1) root, root(T) (2) the remaining nodes are partitioned into n ≧ 0 disjoint sets T 1,…, T n, and T i is a tree Note 1. Note 2. Recursive definition Note 3. Genealogical trees: Figure 5.1, p.187 T 1 , T 2 , T 3 are called the subtree of the root T root T2T2 T3T3 T1T1

Been-Chian Chien,Wei-Pang Yang and Wen-Yang Lin 5-4 Chapter 5 Trees Trees Dusty Honey BearBrandy BrunhildeTerry GillTanseyTweedZoe CoyoteNugget CrocusPrimroseNousBelle Introduction: Genealogical trees leaf root

Been-Chian Chien,Wei-Pang Yang and Wen-Yang Lin 5-5 Chapter 5 Trees Level node (13) leaf (terminal) nonterminal parent children sibling ancestor level of a node height of a tree (4) A B E KL F CD H MG IJ Introduction: Terminologies

Been-Chian Chien,Wei-Pang Yang and Wen-Yang Lin 5-6 Chapter 5 Trees Introduction: Degree, Level, Depth Degree of a node: # of subtrees of a node Level of a node: root  level 1 (for some textbook , root  level 0) if a node at level L, then its children  L+1. Depth (height) of a tree: max level of any node in the tree. 3 2 degree level

Been-Chian Chien,Wei-Pang Yang and Wen-Yang Lin 5-7 Chapter 5 Trees Introduction: Tree Representation  List Representation: ( A ( B ( E ( K, L ), F ), C ( G ), D ( H ( M ), I, J ) ) ) The root comes first, followed by a list of sub-trees degree of a node A B C D EF KL G HIJ M

Been-Chian Chien,Wei-Pang Yang and Wen-Yang Lin 5-8 Chapter 5 Trees Introduction: Tree Representation (cont.) List Representation: linked node Figure 5.3 How many link fields are needed in such a representation? Lemma 5.1: If T is a k-ary tree (i.e., a tree of degree k) with n nodes, then n(k – 1) + 1 of the nk child fields are 0, n ≧ 1. Proof: The total number of child fields is nk Non-0 child fields is n –1. Hence, the number of 0 fields is nk – (n – 1) = n(k – 1) + 1 Child n ... Child 2Child 1data

Been-Chian Chien,Wei-Pang Yang and Wen-Yang Lin 5-9 Chapter 5 Trees Another way to represent tree structure (a) Nested sets (b) Nested parentheses (A(B(H)(J))(C(D)E(G))(F)) HJDG F E A BC HJDEF G BC A Introduction: Tree Representation (cont.)

Been-Chian Chien,Wei-Pang Yang and Wen-Yang Lin 5-10 Chapter 5 Trees (c) Indentation (d) Dewey decimal notation A| | B| | H| | J| | C| | D| | E| | G|-----| F| | Ch2 | | 2.1| | 2.2| | 2.2.1| | 2.2.2| | 2.3| | 2.3.1| | |-----| 2.3.2| | 2.4| | 2.5| | A B C HJDEF G Introduction: Tree Representation (cont.)

Been-Chian Chien,Wei-Pang Yang and Wen-Yang Lin 5-11 Chapter 5 Trees A B C D E F G H IJ K L M data left childright sibling Introduction: Tree Representation as a Left Child-Right Sibling A B C D EF KL G HIJ M Fig. 5.5

Been-Chian Chien,Wei-Pang Yang and Wen-Yang Lin 5-12 Chapter 5 Trees A B C D E F G H I J K L M data left childright child Introduction: Tree Representation as a Left child-Right child (Binary Tree) A B C D E F G H IJ K L M ??? Left child-Right child i.e. Degree-Two Tree i.e. Binary Tree We can represent any tree as a binary tree. Fig. 5.6, p.191 Example 2: Fig. 5.8, p.253

Been-Chian Chien,Wei-Pang Yang and Wen-Yang Lin 5-13 Chapter 5 Trees 5.2 Binary Trees Def: Binary tree is a finite set of nodes, which is (i) empty or (ii) consists of a root and two disjoint binary trees (called the left subtree and right subtree) Cf. ∴ Binary tree isn’t a special case of a tree; it is another concept. TreesBinary Trees min. node #10 degree0, 1, 2, 3, … 0, 1, 2 (right, left subtree) the samedifferent B A A B

Been-Chian Chien,Wei-Pang Yang and Wen-Yang Lin 5-14 Chapter 5 Trees The Abstract Data Type ADT 5.1, p. 193 structure Binary_Tree (abbreviated BinTree) is objects: a finite set of nodes empty or consisting of a root, left Binary_Tree, and right Binary_Tree. functions: for all bt, bt1, bt2  BinTree, item  element BinTree Create() ::=create an empty binary tree Boolean IsEmpty(bt) ::= if(bt == empty binary tree) return TRUE else return FALSE BinTree MakeBT(bt1, item, bt2) ::= return a binary tree with bt1 as left subtree, bt2 as right subtree, and a root containing item BinTree Rchild(bt) ::= if(IsEmpty(bt)) return error else return the left subtree of bt. element Data(bt) ::= if(IsEmpty(bt)) return error else return the data in root of bt. BinTree Rchild(bt) ::= if(IsEmpty(bt)) return error else return the right subtree of bt.

Been-Chian Chien,Wei-Pang Yang and Wen-Yang Lin 5-15 Chapter 5 Trees A B C D E Skewed Binary Tree Special Binary Trees A B C G E I D H F Complete Binary Tree Level Max # i 2 i -1

Been-Chian Chien,Wei-Pang Yang and Wen-Yang Lin 5-16 Chapter 5 Trees Strictly Binary Tree Special Binary Trees (cont.) A B C E I D H Every nonleaf node has nonempty left and right subtrees

Been-Chian Chien,Wei-Pang Yang and Wen-Yang Lin 5-17 Chapter 5 Trees Properties of Binary Trees Lemma 5.1 (Maximum number of nodes, p.194) (1) The maximum number of nodes on level i of a binary tree is 2 i-1, i  1. (2) The maximum number of nodes in a binary tree of depth k is 2 k  1, k  1. Pf: (1) The proof is by induction on i 1 0 Induction Base: The root is the only node on level i = 1. Hence the maximum # of nodes on level i = 1 is 2 0 = Induction Hypothesis: For all j, 1  j < i, the max # of nodes on level j is 2 j Induction step: The max # of nodes on level i-1 is 2 i-2, by the induction hypothesis. Since each node in a binary tree has maximum degree 2, the max # of nodes on level i is 2 times the max # of level i-1 or 2 i-1.

Been-Chian Chien,Wei-Pang Yang and Wen-Yang Lin 5-18 Chapter 5 Trees Lemma 5.1 (cont.) Pf: (2) The max # of nodes in a binary tree of depth k is  Note: k=1 k=2 k=

Been-Chian Chien,Wei-Pang Yang and Wen-Yang Lin 5-19 Chapter 5 Trees Lemma 5.2 ( Relationship between the # of leaf nodes and the # of nodes of degree 2 in a binary tree) For any nonempty binary tree, T n 0 = the # of leaf (terminal) nodes, n 2 = the # of nodes of degree 2 then n 0 = n e.g. Skewed tree n 0 =1 n 2 =0  n 0 =n 2 +1 Complete binary tree n 0 =5 n 2 =4  n 0 =n 2 +1 Lemma 5.2

Been-Chian Chien,Wei-Pang Yang and Wen-Yang Lin 5-20 Chapter 5 Trees Pf : 1 0 Let n 1 = the #of nodes of degree 1 n = the total # of nodes Since all nodes in T are of degree  2, we have: n = n 0 + n 1 + n (1) 2 0 Let B = the # of branches, then n = B + 1 ( ∵ except for tree root, each node has a branch leading into it) 3 0 ∵ All branches emanate either from a node of degree one or from a node of degree 2 ∴ B = n n 2 => n = B + 1 = n 1 + 2n (2) (2)  (1)  0 = n  n 0  n 0 = n Lemma 5.2 (cont.)

Been-Chian Chien,Wei-Pang Yang and Wen-Yang Lin 5-21 Chapter 5 Trees  A full binary tree of depth k is a binary tree of depth k having 2 k -1 nodes, k  0.  A binary tree with n nodes and depth k is complete iff its nodes correspond to the nodes numbered from 1 to n in the full binary tree of depth k. A B C G E I D H F A B C G E K D J F I H O N M L Full binary tree of depth 4Complete binary tree Full Binary Tree vs. Complete Binary Tree

Been-Chian Chien,Wei-Pang Yang and Wen-Yang Lin 5-22 Chapter 5 Trees A B C D D---C-BA D00C40B30A  Method 1. Array Representation:  Method 2. Linked Representation: 0A 0B 0C 0D Binary Tree Representations

Been-Chian Chien,Wei-Pang Yang and Wen-Yang Lin 5-23 Chapter 5 Trees [1] [2] [3] [4] [5] [6] [7] [8] [9] ABCDEFGHIABCDEFGHI A B C G E I D H F Array Representation (in computer)

Been-Chian Chien,Wei-Pang Yang and Wen-Yang Lin 5-24 Chapter 5 Trees A B -- C -- D --. E [1] [2] [3] [4] [5] [6] [7] [8] [9]. [16] A B E C D Disadvantages: (1) waste space (2) insertion/deletion problem Array Representation: Example

Been-Chian Chien,Wei-Pang Yang and Wen-Yang Lin 5-25 Chapter 5 Trees Lemma 5.3 if a complete binary tree with n nodes is represented sequentially, then for any node with index i, 1  i  n, we have: (1) parent (i) is at  i/2  if i≠1. If i = 1, i is at the root and has no parent. (2) left_child (i) is at 2i if 2i  n. If 2i > n, then i has no left child. (3) right_child (i) is at 2i + 1 if 2i+1  n. If 2i + 1 > n, then i has no right child. Array Representation :Lemma 5.3 A B C G E I D H F

Been-Chian Chien,Wei-Pang Yang and Wen-Yang Lin 5-26 Chapter 5 Trees E.g. Parent of node 7: parent (7) is at Parent of node 6: parent (6) is at left_child of 5 is at 2i =10, right_child of 5 is at 2i +1 =11 (2 x 8 = 16 > n has no Child) Pf : We prove (2) (3) is an immediate consequence of (2). ( ∵ the numbering of nodes on same level from left to right) (1) follows from (2) and (3) Array Representation :Lemma 5.3 (cont.)

Been-Chian Chien,Wei-Pang Yang and Wen-Yang Lin 5-27 Chapter 5 Trees We prove (2) by induction on i. For i = 1, clearly the left child is at 2 unless 2 > n in which case 1 has no left child. Now assume that for all j, 1  j  i, left_child (j) is at 2j. Then, the two nodes immediately preceding left_child (i+1) in the representation are the right child of i and the left child of i. The left child of i is at 2i. Hence, the left child of i + 1 is at 2i + 2 = 2(i + 1) unless 2(i + 1) > n in which case i + 1 has no left child. ii+1 2i+2 2i2i Array Representation :Lemma 5.3 (cont.) (2) left_child (i) is at 2i if 2i  n. If 2i > n, then i has no left child.

Been-Chian Chien,Wei-Pang Yang and Wen-Yang Lin 5-28 Chapter 5 Trees  Node structure: typedef struct node *tree_pointer; typedef struct node { int data; tree_pointer left_child, right_child; }; Linked Representation left_child data right_child

Been-Chian Chien,Wei-Pang Yang and Wen-Yang Lin 5-29 Chapter 5 Trees  Let L, V, and R stand for moving left, visiting the node, and moving right.  There are six possible combinations of traversal: LVR, LRV, VLR, VRL, RVL, RLV  Adopt convention that we traverse left before right, only 3 traversals remain LVR: inorder, a+b LRV: postorder, ab+ VLR: preorder, +ab  Binary Tree Traversals: Combinations 5.3 Binary Tree Traversals data left_child right_child V L R + a b

Been-Chian Chien,Wei-Pang Yang and Wen-Yang Lin 5-30 Chapter 5 Trees Binary Tree Traversal: Example Traversing (walking through) a tree Inorder traversal BAC a+b infix Preorder traversal ABC +ab prefix Postorder traversal BCA ab+ postfix A B C A B C G F Inorder traversal BA____ Preorder traversal ABC____ Postorder traversal _________ E.g. 2 E.g. 1 + a b

Been-Chian Chien,Wei-Pang Yang and Wen-Yang Lin 5-31 Chapter 5 Trees Binary Tree Traversal: Exercise inorder traversal DIBA____ preorder traversal ABDI____ postorder traversal _________ A B C G I D F E.g. 3

Been-Chian Chien,Wei-Pang Yang and Wen-Yang Lin 5-32 Chapter 5 Trees Inorder traversal (infix expression) A / B * C * D + E Preorder traversal (prefix expression) + * * / A B C D E Postorder traversal (postfix expression) A B / C * D * E + Level order traversal + * E * D / C A B  Arithmetic Expression: A / B * C * D + E Arithmetic Expression using Binary Tree + * A * / E D C B

Been-Chian Chien,Wei-Pang Yang and Wen-Yang Lin 5-33 Chapter 5 Trees Inorder traversal Traverse the left subtree in inorder visit the root Traverse the right subtree in inorder Program 5.1: Inorder traversal Binary Tree Traversal: Inorder + * A * / E D C B A / B * C * D + E P.201

Been-Chian Chien,Wei-Pang Yang and Wen-Yang Lin 5-34 Chapter 5 Trees Binary Tree Traversal: Preorder Preorder traversal visit the root Traverse the left subtree in preorder Traverse the right subtree in preorder Program 5.2: Preorder traversal + * A * / E D C B + * * / A B C D E P.202

Been-Chian Chien,Wei-Pang Yang and Wen-Yang Lin 5-35 Chapter 5 Trees Postorder traversal Traverse the left subtree Traverse the right subtree Visit the root Program 5.3: Postorder traversal Binary Tree Traversal: Postorder + * A * / E D C B A B / C * D * E + P.203

Been-Chian Chien,Wei-Pang Yang and Wen-Yang Lin 5-36 Chapter 5 Trees Iterative Inorder Traversal Level-Order Traversal Traversal without a Stack Binary Tree Traversal: More Topics (skip) + * A * / E D C B Method 1: adding parent field Method 2: §5.5 Threaded binary tree

Been-Chian Chien,Wei-Pang Yang and Wen-Yang Lin 5-37 Chapter 5 Trees 5.4 Additional Binary Tree Operations With the inorder, postorder, or preorder mechanisms, we can implement all needed binary tree functions Copying Binary Trees Program 5.6: Copying a binary tree Testing Equality Program 5.7: Binary tree equivalence p.207

Been-Chian Chien,Wei-Pang Yang and Wen-Yang Lin 5-38 Chapter 5 Trees  The Satisfiability Problem  Prepositional Calculus Expression (Formula)  A variable is an expression.  If x and y are expressions, then ¬x, x  y, x  y are expressions.  Parentheses can be used  Example: x 1  (x 2  ¬x 3 )  The Satisfiability Problem: Is there an assignment to make an expression true? The Satisfiability Problem

Been-Chian Chien,Wei-Pang Yang and Wen-Yang Lin 5-39 Chapter 5 Trees  Example: A  B  C  The Satisfiability Problem: Is there an assignment to make the expression A  B  C true? The Satisfiability Problem: Example V  C A B T B C VT T  F TF TFT A Value assignment

Been-Chian Chien,Wei-Pang Yang and Wen-Yang Lin 5-40 Chapter 5 Trees     x3x3   x1x1 x2x2 x1x1  x3x3  Formula: (x 1  ¬x 2 )  (¬ x 1  x 3 )  ¬x 3  2 n possible combinations for n variables: (T,T,T) (T,T,F) (T,F,T) (T,F,F) (F,T,T) (F,T,F) (F,F,T) (F,F,F)  Note : there are 2 n possible assignments of truth values to (x 1, ……, x n ) ∴ check O(2 n . g), where g is evaluation time The Satisfiability Problem: Complexity

Been-Chian Chien,Wei-Pang Yang and Wen-Yang Lin 5-41 Chapter 5 Trees left_child data value right_child typedef emun {not, and, or, true, false } logical; typedef struct node *tree_pointer; typedef struct node { tree_pointer left_child; logical data; // ¬, ,  short int value; tree_pointer right_child; } ;  Node structures The Satisfiability Problem: Data Structure  C declaration T/F

Been-Chian Chien,Wei-Pang Yang and Wen-Yang Lin 5-42 Chapter 5 Trees 1 0 node structure A  B  C 2 0 Program Program 5.9 LchildDataValueRchild T. False V ^C A B T B C VT T^F TF TFT A The Satisfiability Problem: Evaluating a Formula Value combination Postorder: A B  C 

Been-Chian Chien,Wei-Pang Yang and Wen-Yang Lin 5-43 Chapter 5 Trees for (all 2 n possible truth value combinations) { generate the next combination; replace the variables by their values; evaluate root by traversing it in postorder; if (root->value) { printf( ) ’ return; } printf( “ no satisfiable combination\n ” ); The Satisfiability Problem: Algorithm

Been-Chian Chien,Wei-Pang Yang and Wen-Yang Lin 5-44 Chapter 5 Trees void post_order_eval(tree_pointer node) { if (node) { post_order_eval(node->left_child); post_order_eval(node->right_child); switch (node->data) { case not: node->value =!node->right_child->value; break; case and: node->value = node->left_child->value && node->right_child->value; break; case or: node->value = node->left_child->value || node->right_child->value; break; case true: node->value = TRUE; break; case false: node->value = FALSE; } The Satisfiability Problem: Program

Been-Chian Chien,Wei-Pang Yang and Wen-Yang Lin 5-45 Chapter 5 Trees  Two many null pointers in current representation of binary trees n: number of nodes number of non-null links: n – 1 total links: 2n null links: 2n  (n  1) = n + 1  Replace these null pointers with some useful “threads”. * 9 nodes 8 non-null link 10 null-link or 10 0-links 5.5 Threaded Binary Trees

Been-Chian Chien,Wei-Pang Yang and Wen-Yang Lin 5-46 Chapter 5 Trees  If left_child of ptr is null, replace it with a pointer to the node that would be visited before ptr in an inorder traversal. i.e. inorder predecessor of ptr  If right_child of ptr is null, replace it with a pointer to the node that would be visited after ptr in an inorder traversal. i.e. inorder successor of ptr  Replace null pointers with “threads”. Threaded BT: Using Null Fields

Been-Chian Chien,Wei-Pang Yang and Wen-Yang Lin 5-47 Chapter 5 Trees A B C G E I D H F dangling Inorder traversal: H, D, I, B, E, A, F, C, G  A Threaded Binary Tree Threaded BT: Example left_child: inorder predecessor right_child: inorder successor Problem: How to create a threaded BT without dangling links?

Been-Chian Chien,Wei-Pang Yang and Wen-Yang Lin 5-48 Chapter 5 Trees Threaded BT: Adding Head Node A B C G E I D H F dangling Inorder traversal: H, D, I, B, E, A, F, C, G Root = Head Node left_child: inorder predecessor right_child: inorder successor

Been-Chian Chien,Wei-Pang Yang and Wen-Yang Lin 5-49 Chapter 5 Trees left_thread left_child data right_child right_thread t f false: child true: thread  Data Structures for Threaded Binary tree Threaded BT: Data Structures typedef struct threaded_tree *threaded_pointer; typedef struct threaded_tree { short int left_thread; threaded_pointer left_child; char data; threaded_pointer right_child; short int right_thread; };  Node structure definition for threaded BT

Been-Chian Chien,Wei-Pang Yang and Wen-Yang Lin 5-50 Chapter 5 Trees  Memory Representation of A Threaded Binary tree f f -- f f A f f C f f B t t E t tF t t G f f D t tI t t H root Threaded BT: Memory Representation

Been-Chian Chien,Wei-Pang Yang and Wen-Yang Lin 5-51 Chapter 5 Trees t f false: child true: thread  An Empty Threaded Binary Tree Threaded BT: Empty left_thread left_child data right_child right_thread

Been-Chian Chien,Wei-Pang Yang and Wen-Yang Lin 5-52 Chapter 5 Trees Insert child as the right child of node parent (1) change parent->right_thread to FALSE (2) set child->left_thread and child->right_thread to TRUE (3) set child->left_child to point to parent (4) set child->right_child to parent->right_child (5) change parent->right_child to point to child  Inserting nodes into Threaded Binary tree Threaded BT: Inserting nodes

Been-Chian Chien,Wei-Pang Yang and Wen-Yang Lin 5-53 Chapter 5 Trees  Examples:Insert a node D as a right child of B. root parent A B C D child Case 1: parent A B C D child (1) (2) (3) root Threaded BT: Inserting Node

Been-Chian Chien,Wei-Pang Yang and Wen-Yang Lin 5-54 Chapter 5 Trees Case 2: root parent A B C D child E FG (1) (3) (4) (2) root parent A B C D child E FG Threaded BT: Inserting Node (cont.)

Been-Chian Chien,Wei-Pang Yang and Wen-Yang Lin 5-55 Chapter 5 Trees Threaded BT: Inserting Node (cont.) void insert_right(thread_pointer parent, thread_pointer child) // Insert child as the right child of parent { child->right_child = parent ->right_child; child->right_thread = parent -> right_thread; child->left_child = parent; child->left_thread = TRUE;// left_child is a thread parent->right_child = child;// attach child to parent parent->right_thread = FALSE; if (! child->right_thread) { temp = insucc(child); // returns the inorder successor of child temp->left_child = child; }  Program 5.12, p. 218

Been-Chian Chien,Wei-Pang Yang and Wen-Yang Lin 5-56 Chapter 5 Trees Def. Min Tree: value of parent  value of children Def. Max Tree: value of parent  value of children 5.6 Heaps ‧‧‧ max

Been-Chian Chien,Wei-Pang Yang and Wen-Yang Lin 5-57 Chapter 5 Trees Def. Min Heap: (1) min tree and (2) complete binary tree Def. Max Heap: (1) max tree and (2) complete binary tree Min Heap vs. Max Heap  Operations on heaps create an empty heap insert a new node delete a node …  ADT for Max Heap p. 220 insert a new node delete a node

Been-Chian Chien,Wei-Pang Yang and Wen-Yang Lin 5-58 Chapter 5 Trees Insertion into a max heap Max heap E.g.1. Add “5” E.g.2. Add “21” Heaps: Insert a Node ∵ complete binary tree

Been-Chian Chien,Wei-Pang Yang and Wen-Yang Lin 5-59 Chapter 5 Trees  Insertion to a Max Heap initial location of new node insert 5 into heap Heaps: Insert a Node Program 5.13 p.223 Time complexity: O(log 2 n)

Been-Chian Chien,Wei-Pang Yang and Wen-Yang Lin 5-60 Chapter 5 Trees  Deletion from a Max Heap remove Heaps: Delete a Node Time complexity: O(log 2 n) Program 5.14 p.225

Been-Chian Chien,Wei-Pang Yang and Wen-Yang Lin 5-61 Chapter 5 Trees  Time complexity for different data structures for priority queue  More topics about Heap on Ch. 9: Heap Structures Time complexity for Different DS

Been-Chian Chien,Wei-Pang Yang and Wen-Yang Lin 5-62 Chapter 5 Trees Def. Binary Search Tree, T is a binary tree T = ψ or T≠ψ (1) all keys are distinct (2) keys in left subtree are smaller than the key in the root (3) keys in right subtree are larger than the key in the root (4) the left and right subtree are also b.s.t. E.g. Fig 5.30, p.227 (a) 〤, (b) V, (c) V 5.7 Binary Search Tree Not a binary search tree binary search trees

Been-Chian Chien,Wei-Pang Yang and Wen-Yang Lin 5-63 Chapter 5 Trees Searching a binary search tree Recursive Search of a Binary Search Tree p. 227 Program 5.15 ‧‧‧‧ Iterative Search of a Binary Search Tree p. 228 Program 5.16 Ex. Binary Search Tree: Searching

Been-Chian Chien,Wei-Pang Yang and Wen-Yang Lin 5-64 Chapter 5 Trees Insert 80 Insert Binary Search Tree: Insert Node

Been-Chian Chien,Wei-Pang Yang and Wen-Yang Lin 5-65 Chapter 5 Trees X T1 T2 1 T1 T2 delete Binary Search Tree: Deletion  Case 1: One child or Leaf Node

Been-Chian Chien,Wei-Pang Yang and Wen-Yang Lin 5-66 Chapter 5 Trees Before deleting 60 After deleting 60  Case 2: Two children Binary Search Tree: Deletion (cont.)

Been-Chian Chien,Wei-Pang Yang and Wen-Yang Lin 5-67 Chapter 5 Trees 5.8 Selection Trees ( 暫略 )

Been-Chian Chien,Wei-Pang Yang and Wen-Yang Lin 5-68 Chapter 5 Trees General tree K-ary tree: if k is the max degree of any node Node structure: or Waste space 5.9 Forests ……… 5-ary tree A 7-ary tree Data 1 2 ....... K

Been-Chian Chien,Wei-Pang Yang and Wen-Yang Lin 5-69 Chapter 5 Trees Definition: A forest is a set of n  0 disjoint trees A B C D E F G H I Forests: Definition Figure 5.34: Three-tree forest

Been-Chian Chien,Wei-Pang Yang and Wen-Yang Lin 5-70 Chapter 5 Trees  Definition: If T 1, T 2, …, T n : a forest of trees B(T 1, T 2, …, T n ): a binary tree corresponding to this forest we define B(T 1, T 2, …, T n ) by (1)empty, if n = 0 (2)has root equal to root(T 1 ) has left subtree equal to B(T 11, T 12, …, T 1m ) has right subtree equal to B(T 2, T 3, …, T n ) Transform a Forest into a Binary Tree A BC D E F G H I G H I A B C D F E Forest Binary tree B

Been-Chian Chien,Wei-Pang Yang and Wen-Yang Lin 5-71 Chapter 5 Trees To represent any forest as a binary tree. Consider the following forest Step 1: Transform each tree into a binary tree BC A EF D G HJ K Transform a Forest into a Binary Tree (cont.) BC A K EF D G HJ

Been-Chian Chien,Wei-Pang Yang and Wen-Yang Lin 5-72 Chapter 5 Trees Step 2: Regard the other trees as the sibling of the first tree and link their roots together Step 3: Tilt tree diagram 45° clockwise BC A K EF D G HJ B C A K E F D G H J Transform a Forest into a Binary Tree (cont.)

Been-Chian Chien,Wei-Pang Yang and Wen-Yang Lin 5-73 Chapter 5 Trees Preorder - If F is empty, then return - Visit the root of the first tree of F - Traverse the subtrees of the first tree in tree preorder - Traverse the remaining trees of F in tree preorder Forest Traversals: Preorder preorder: A B C D E F G H I G H I A B C D F E A B C D E F G H I Equivalent Preorder is a time-honored concept which might be called dynastic order.  order of succession to the throne.

Been-Chian Chien,Wei-Pang Yang and Wen-Yang Lin 5-74 Chapter 5 Trees Inorder - If F is empty, then return -Traverse the subtrees of the first tree in tree inorder - Visit the root of the first tree - Traverse the remaining trees in tree inorder Forest Traversals: Inorder inorder:B C D A F E H I G G H I A B C D F E A B C D E F G H I Equivalent

Been-Chian Chien,Wei-Pang Yang and Wen-Yang Lin 5-75 Chapter 5 Trees Postorder - If F is empty, then return - Traverse the subtrees of the first tree in tree postorder - Traverse the remaining trees in tree postorder - Visit the root of the first tree Note: Postorder has no corresponding binary tree traversal of a forest G H I A B C D F E A B C D E F G H I Postorder: D C B F I H G E A Postorder: B C D F E H I G A Forest Traversals: Postorder Equivalent

Been-Chian Chien,Wei-Pang Yang and Wen-Yang Lin 5-76 Chapter 5 Trees Manipulation of Algebraic Formula Set Representation Decision Tree A Huffman Decode Tree … Applications of Trees ( 略 )

Been-Chian Chien,Wei-Pang Yang and Wen-Yang Lin 5-77 Chapter 5 Trees Evaluate a Expression: in §3.4, p.116 X = A/B-C+D*E-A*C (Formula, p.116) Postorder => AB/C-DE*+AC*- => Program 3.9 void eval(expression e) (p.122)  Formula => Binary Tree  Postorder Traversal on tree AB/C-DE*+AC*- => To evaluate the Expression/Formula - *+ -* A C /C AB DE Manipulation of Algebraic Formula

Been-Chian Chien,Wei-Pang Yang and Wen-Yang Lin 5-78 Chapter 5 Trees A Huffman Decode Tree Consider the coding schemes for {A,B,C,D,E} Method 1: Method 2: A: 000 B: 001 C: 010 D: 011 E: 100 average length = 3 bits/char encoding: C E D A: 1 B: 01 C: 001 D: 0001 E: 0000 average length = ( )/5 = 2.8 bits/char encoding: B E D Method 3: : Huffman coding the most commonly occurring characters are represented by the shortest strings. Assume: A: 30% - 01 B: 5% C: 10% D: 20% E: 35% - 1 5% 20% 100% 65% 35% 15% B C D A E % % 30% average length = 2*30%+4*5%+4*10%+3*20%+1*35% = 2.15 bits/char encoding: A D B

Been-Chian Chien,Wei-Pang Yang and Wen-Yang Lin 5-79 Chapter 5 Trees Consider the “eight coins” problem: Given coins a, b, c, d, e, f, g, h. One is a counterfeit and has a different weight than the others. We want to determine which coin it is, making use of an equal arm balance. Decision Trees a+b+c ? d+e+f a+d ? b+e g ? h > > > >> > > > > > > > = == = = == = == = < <<< a?bc?ab?a g?a h?aa?ba?cb?a aHeLcHfLbHdLgHhLhHgLbLdHcLfHaLeH

Been-Chian Chien,Wei-Pang Yang and Wen-Yang Lin 5-80 Chapter 5 Trees Procedure EIGHTCOINS (*eight weights are input; the different one is discovered using only 3 comparisons *) read(a, b, c, d, e, f, g, h) Case :a+b+c=d+e+f: If g>h then call COMP(g, h, a) else call COMP(h, g, a) :a+b+c>d+e+f: case :a+d=b+e: call COMP(c, f, a) :a+d>b+e: call COMP(a, e, b) :a+d<b+e: call COMP(b, d, a) end :a+b+c<d+e+f: case :a+d=b+e: call COMP(f, c, a) :a+d>b+e: call COMP(d, b, a) :a+d<b+e: call COMP(e, a, b) end End EIGHTCOINS Decision Trees (cont.) Procedure COMP(x, y, z) If x > y then print (x ‘heavy’) else print (y ‘light’) End COMP.