Data Structures Chapter 5: Trees 5-1. Pedigree Genealogical Chart Cheryl Kevin Rosemary JohnTerry Richard Kelly Jack Mary Anthony KarenJoe Michelle MikeAngela.

Slides:



Advertisements
Similar presentations
Trees Types and Operations
Advertisements

Review of Chapter 5 張啟中. Selection Trees Selection trees can merge k ordered sequences (assume in non- decreasing order) into a single sequence easily.
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 nodes are.
Binary Trees Chapter 6. Linked Lists Suck By now you realize that the title to this slide is true… By now you realize that the title to this slide is.
Binary Trees, Binary Search Trees CMPS 2133 Spring 2008.
Binary Trees, Binary Search Trees COMP171 Fall 2006.
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, Binary Trees, and Binary Search Trees COMP171.
Review of Chapter 5 張啟中. Threaded Binary Tree 利用 Binary Tree 節點中的 0-links ,指向中序的先行 者或後繼者,以方便中序追蹤。 Threading Rules  A 0 RightChild field at node p is.
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.
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.
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
Binary Trees Chapter 6.
Bioinformatics Programming 1 EE, NCKU Tien-Hao Chang (Darby Chang)
Chap 5 Trees. 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.
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.
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.
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.
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.
Trees, Binary Trees, and Binary Search Trees COMP171.
Starting at Binary Trees
Preview  Graph  Tree Binary Tree Binary Search Tree Binary Search Tree Property Binary Search Tree functions  In-order walk  Pre-order walk  Post-order.
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.
1 Storing Hierarchical Information Lists, Stacks, and Queues represent linear sequences Data often contain hierarchical relationships that cannot be expressed.
Tree Traversals, TreeSort 20 February Expression Tree Leaves are operands Interior nodes are operators A binary tree to represent (A - B) + C.
Lecture - 10 on Data Structures. 6:05:57 PM Prepared by, Jesmin Akhter, Lecturer, IIT,JU.
Trees 2015, Fall Pusan National University Ki-Joune Li.
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.
Trees By P.Naga Srinivasu M.tech,(MBA). Basic Tree Concepts A tree consists of finite set of elements, called nodes, and a finite set of directed lines.
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.
CISC 235 Topic 3 General Trees, Binary Trees, Binary Search 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. 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.
Trees Trees are a very useful data structure. Many different kinds of trees are used in Computer Science. We shall study just a few of these.
2/11/ IT 179 Recursive Definition of Tree Structures 1.Empty is a tree; the root is null 2.A node points to a finite number of the roots of some.
Foundation of Computing Systems Lecture 4 Trees: Part I.
Trees (Unit 7).
BINARY TREES Objectives Define trees as data structures Define the terms associated with trees Discuss tree traversal algorithms Discuss a binary.
TREES General trees Binary trees Binary search trees AVL trees Balanced and Threaded trees.
1 CSE 326: Data Structures Trees Lecture 6: Friday, Jan 17, 2003.
Chapter 8 (Lafore’s Book) Binary Tree Hwajung Lee.
1 Trees. 2 Trees Trees. Binary Trees Tree Traversal.
Tree Representation and Terminology Binary Trees Binary Search Trees Pointer-Based Representation of a Binary Tree Array-Based Representation of a Binary.
DS.T.1 Trees Chapter 4 Overview Tree Concepts Traversals Binary Trees Binary Search Trees AVL Trees Splay Trees B-Trees.
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.
Trees Chapter 15.
TREE DATA STRUCTURE Data Structure 21-Sep-18 Tree
Binary Trees, Binary Search Trees
Ch. 11 Trees 사실을 많이 아는 것 보다는 이론적 틀이 중요하고, 기억력보다는 생각하는 법이 더 중요하다.
Introduction to Trees IT12112 Lecture 05.
Data Structures Chapter 5: Trees.
Binary Trees, Binary Search Trees
Trees.
Binary Trees, Binary Search Trees
NATURE VIEW OF A TREE leaves branches root. NATURE VIEW OF A TREE leaves branches root.
Presentation transcript:

Data Structures Chapter 5: Trees 5-1

Pedigree Genealogical Chart Cheryl Kevin Rosemary JohnTerry Richard Kelly Jack Mary Anthony KarenJoe Michelle MikeAngela Ancestors of Cheryl: A binary Tree 血統、家譜 宗譜的、家系的 John and Terry are parents of Kevin. 5-2

Lineal Genealogical Chart Germanic Osco-Umbrian LatinNorth West OscoUmbrianFrenchItalianIcelandicSwedishLowYiddish Proto Indo-European ItalicHellenic HighSpanishNorwegian Greek 直系的、世襲的 宗譜的、家系的 Modern European languages Latin produces Spanish, French and Italian. 5-3

A Tree A BCD EFGHIJ KLM Level root degree(A) = 3 degree(B) = 2 degree(C) = 1 nonleaf nodes (nonterminal nodes): ** height = depth = 4 node: A, B, C, …M parent of E: B children of B: E, F ancestors of E: A, B descendants of B: E, F, K, L leaf nodes (terminal nodes): K, L, F, G, M, I, J 5-4

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 nodes are partitioned into n  0 disjoint ( 無交集的 ) sets T 1, …, T n, each of which is a subtree. 5-5

Tree Terminologies (1) degree ( 分支度 ) of a node: number of subtrees of the node. degree of a tree: maximum degree of the nodes in the tree. leaf (terminal) node: a node with degree zero Siblings (brothers): the nodes with the same parent A BCD EFGHIJ KLM 5-6

Tree Terminologies (2) ancestors of a node: all the nodes along the path from the root to the node. descendants of a node: all the nodes of its subtrees. level of a node: the level of the node ’ s parent plus one. Here, the level of the root is 1. height (depth) of a tree: the maximum level of the nodes in the tree. A BCD EFGHIJ KLM 5-7

List Representation of a Tree A BF0CG0 0 DIJ0 EKL0HM0 The tree is represented as a list: (A (B (E (K, L), F), C(G), D(H (M), I, J))) A BCD EFGHIJ KLM 5-8

Representation of Trees Left child-right sibling tree –two links (or pointers): left child and right sibling data left childright sibling A BCD EFGHIJ KLM A BCD EFGHIJ KLM 5-9

Binary Trees A B C D E A B C DE FG HI Skewed binary tree 歪斜二元樹 Complete binary tree 完整二元樹 A G FE C D B HI Binary tree 5-10

Binary Tree 二元樹 A binary tree: – a finite set of nodes that is either empty, or consists of a root and two disjoint binary trees called the left subtree and the right subtree. In a binary tree, we distinguish between the order of the children; in a tree we do not. Two different binary trees A B A B 5-11

Full Binary Tree The maximum number of nodes on level i of a binary tree is 2 i – 1, i  1. The maximum number of nodes in a binary tree of depth k is 2 k – 1, k  1. A full binary tree of depth k is a binary tree of depth k having 2 k – 1 nodes, k 

A complete binary tree with n nodes and depth k is that its nodes correspond to the nodes numbered from 1 to n in the full binary tree of depth k. It can be represented by an array. Root is at a[1]. a[0] is not used. Complete Binary Tree parent(i)= ** left_hild(i)= right_child(i)= 5-13

Linked Representation of Binary Trees leftChilddatarightChild data leftChildrightChild template class Tree; //forward declaration template class TreeNode { friend class Tree ; private: T data; TreeNode *leftChild; TreeNode *rightChild; }; 5-14

Linked Representation of Binary Trees template class Tree{ public: // Tree operations. private: TreeNode *root; }; 5-15

Linked Representation of a Binary Tree A B C D EH0I0 F0G0 root A B C DE FG HI 5-16

Binary Tree Traversal Preorder traversal: 1.root 2.left subtree 3.right subtree Inorder traversal: 1.left subtree 2.root 3.right subtree Postorder traversal: 1.left subtree 2.right subtree 3.root 5-17

Arithmetic Expression Trees Preorder Traversal: ** => Prefix expression Inorder Traversal: => Infix expression Postorder Traversal: => Postfix expression + *E *D /C AB 5-18

Preorder Traversal template void Tree ::Preorder() { //Driver calls workhorse for traversal of entire tree Preorder(root); } template void Tree ::Preorder (TreeNode *currentNode) { //Workhouse traverses the subtree rooted at currentNode If (currentNode){ Visit(currentNode); //visit root Preorder(currentNode->leftChild); Preorder(currentNode->rightChild); } 5-19

Inorder Traversal template void Tree ::Inorder() { //Driver calls workhorse for traversal of entire tree Inorder(root); } template void Tree ::Inorder (TreeNode *currentNode) { //Workhouse traverses the subtree rooted at currentNode If (currentNode){ Inorder(currentNode->leftChild); Visit(currentNode); //visit root Inorder(currentNode->rightChild); } 5-20

Postorder Traversal template void Tree ::Postorder() { //Driver calls workhorse for traversal of entire tree Postorder(root); } template void Tree ::Postorder (TreeNode *currentNode) { //Workhouse traverses the subtree rooted at currentNode If (currentNode){ Postorder(currentNode->leftChild); Postorder(currentNode->rightChild); Visit(currentNode); //visit root } 5-21

Level-Order Traversal Preorder, inorder and postorder traversals all require a stack. Level-order traversal uses a queue. Level-order traversal: 1.root 2.left child 3.right child. After All nodes on a level have been visited, we can move down. + *E *D /C AB Level-order traversal: ** 5-22

template void Tree ::LevelOrder() {// Traverse the binary tree in level order. Queue *> q; TreeNode *currentNode = root; while (currentNode) { Visit(currentNode); if (currentNode ->leftChild) q.Push(currentNode->leftChild); if (currentNode->rightChild) q.Push(currentNode->rightChild); if (q.IsEmpty()) return; currentNode = q.Front(); q.Pop(); } Level-Order Traversal of a Binary Tree +*E*D/CAB + *E *D /C AB 5-23

Threaded Binary Tree 引線二元樹 Threading Rules –A 0 rightChild field at node p is replaced by a pointer to the node that would be visited after p when traversing the tree in inorder. That is, it is replaced by the inorder successor of p. –A 0 leftChild link at node p is replaced by a pointer to the node that immediately precedes node p in inorder (i.e., it is replaced by the inorder predecessor of p). 5-24

Threaded Binary Tree A HI B DE C GF Inorder sequence: HDIBEAFCG A B C DE FG HI 5-25

Memory Representation of Threaded Tree f -f f Af f Bf f Df t Ht t It tEt f Bf tDt tEt A HI B DE C GF x->rightThread == TRUE => x ->rightChild is a thread (pointer to inorder successor) x->rightThread == FALSE => x ->rightChild is a pointer to the right child. 5-26

Inorder Successor in Threaded Trees By using the threads, we can perform an inorder traversal without making use of a stack. T* ThreadedInorderIterator::Next() {//Return the inorder successor of currentNode=x ThreadedNode *temp = currentNode -> rightChild; if (!currentNode->rightThread) //real rightChild while (!temp->leftThread) temp = temp -> leftChild; // a path of leftChild starting from rightChild of x currentNode = temp; if (currentNode == root) return 0; else return &currentNode -> data; } A HI B DE C GF Inorder sequence: H D I B E A F C G 5-27

Insertion of r as the Right Child of s in a Threaded Binary Tree (1) u s r u s r before after Case 1: The right subtree of s is empty. 5-28

Insertion of r as the Right Child of s in a Threaded Binary Tree (2) s p q r r s p q before after Case 2: The right subtree of s is not empty. 5-29

Inserting r as the Right Child of s template void ThreadedTree ::InsertRight (ThreadedNode *s, ThreadedNode *r) {// Insert r as the right child of s. r -> rightChild = s -> rightChild; r -> rightThread = s -> rightThread; r -> leftChild = s; r -> leftThread = True; // leftChild is a thread s -> rightChild = r; s -> rightThread = false; if (! r -> rightThread) {// rightChild is not a thread ThreadedNode *q = InorderSucc (r); // return the inorder successor of r q -> leftChild = r; } 5-30

Priority Queue Maximum (minimum) finding: In a priority queue, the element to be deleted is the one with highest (or lowest) priority. It is easy to get the maximum (minimum). Insertion: An element with arbitrary priority can be inserted into the queue according to its priority. max (min) priority queue: a data structure supports the above two operations. 5-31

Max heaps Min heaps 5-32

Max (Min) Heaps A max (min) heap is a complete binary tree in which the key value in each node is no smaller (larger) than the key values in its children (if any). Heaps are frequently used to implement priority queues. Time complexity of a max (min) heap with n nodes: –Max (min) finding: O(1) –Insertion: O(log n) –Deletion: O(log n) 5-33

Insertion into a Max Heap (b)  (c1) Insert 5 (a)(b) (b)  (c2) Insert

Deletion from a Max Heap (a)(b) (c)(d) 5-35

Deletion from a Max Heap (Cont.) (e) (f) (g) (i) 5-36

Binary Search Trees A binary search tree is a binary tree. It may be empty. If it is not empty. then it satisfies the following properties: –Every element has a key and no two elements have the same key (i.e., the keys are distinct) –The keys (if any) in the left subtree are smaller than the key in the root. –The keys (if any) in the right subtree are larger than the key in the root. –The left and right subtrees are also binary search trees. 5-37

Binary Trees Not binary search tree Binary search trees 5-38

Searching a Binary Search Tree Search for an element with key value k. If the root is 0, then this is an empty tree, and the search is unsuccessful. Otherwise, compare k with the key in the root.  k==root: successful search.  k < root: search the left subtree.  k > root: search the right subtree

Searching a Binary Search Tree template // driver pair * BST :: Get(const K& k) {// If found, return a pointer; otherwise, return 0. // “pair”: A class couples together a pair of values. // K: key, E: node element return Get(root, k); } template // Workhouse pair * BST :: Get(TreeNode >* p, const K& k) { if (!p) return 0; if (k data.first) return Get(p->leftChild, k); if (k > p->data.first) return Get(p->rightChild, k); return &p->data; } 5-40

Insertion into a Binary Search Tree (1) Insert 80 (2) Insert 35 Time complexity: O(h) h: tree height 5-41

5 Deletion from a Binary Search Tree Delete 35 Delete

Deletion from a Binary Search Tree Deletion of a node x: –x is a leaf node: delete x directly. –x has one child: move up the position of child to x. –x has two children: replace x with either inorder successor (smallest in the right subtree, no left child) or inorder predecessor (largest in the left subtree, no right child). Time complexity: O(h), h:tree height 5-43

Selection Trees Goal: merge ( 合併 ) k ordered sequences (called runs) in nondecreasing into a single ordered sequence. Straightforward method: perform k – 1 comparisons each time to select the smallest one among the first number of each of the k ordered sequences. Better method: winner tree run1 run2 run3 run4 run5 run6 run7 run8 k =

Winner Tree for k = run1 run2 run3 run4 run5 run6 run7 run8 5-45

Winner Tree for k = run1 run2 run3 run4 run5 run6 run7 run8 5-46

Winner Tree A winner tree is a complete binary tree in which each node represents the smaller of its two children. Thus, the root represents the smallest. Each leaf node represents the first record in the corresponding run. Each nonleaf node represents the winner of a tournament. Number of levels: Total time for merging k runs: O(n log 2 k) 5-47

Loser Tree Overall winner run Winner Tree (9,10)(6,20) (6,9)(6,9) (8,9)(8,9)(17,90) (8,17) (6,8)(6,8) 5-48

Loser Tree Loser tree: A selection tree in which each nonleaf node retains a pointer to the loser. Each leaf node represents the first record of each run. An additional node, node 0, has been added to represent the overall winner of the tournament. 5-49

Forests Forest: a set of n ≥ 0 disjoint trees. If we remove the root of a tree, we obtain a forest. – E.g., removing the root of a binary tree produces a forest of two trees. E F G HI A BCD 5-50

Representing a Forest with a Binary Tree leftChild=first child rightChild=next sibling E F G HI A BCD E FG H I A B C D A forest of 3 trees A forest is represented by a binary tree. 5-51

Constructing a Binary Tree from Its Inorder Sequence and Preorder Sequence A B, C D, E, F, G, H, I A B C Inorder sequence: BCAEDGHFI Preorder sequence: ABCDEFGHI Step 1: Find the rootStep 2: Do recursively 5-52

Number of Distinct Binary Trees n = 2, b 2 =2n = 3, b 3 =5 Number of distinct binary trees with n nodes: bnbn bibi b n-i

5-54

5-55

5-56

Number of Distinct Binary Trees b n are Catalan numbers:1, 1, 2, 5, 14, 42, 132, 429, 1430, 4862, 16796, 58786, , , , , , , , , , … Number of distinct binary trees with n nodes= b n –b 0 =1, b 1 =1, b 2 =2, b 3 =5, b 4 =14, b 5 =42, …