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.

Slides:



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

© 2006 Pearson Addison-Wesley. All rights reserved11 A-1 Chapter 11 Trees.
Review of Chapter 5 張啟中. Threaded Binary Tree 利用 Binary Tree 節點中的 0-links ,指向中序的先行 者或後繼者,以方便中序追蹤。 Threading Rules  A 0 RightChild field at node p is.
© 2006 Pearson Addison-Wesley. All rights reserved11 A-1 Chapter 11 Trees.
Marc Smith and Jim Ten Eyck
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.
Bioinformatics Programming 1 EE, NCKU Tien-Hao Chang (Darby Chang)
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.
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.
Chapter 11 A Trees. © 2004 Pearson Addison-Wesley. All rights reserved 11 A-2 Terminology A tree consists of vertices and edges, –An edge connects to.
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.
CS Data Structures Chapter 5 Trees. Chapter 5 Trees: Outline  Introduction  Representation Of Trees  Binary Trees  Binary Tree Traversals 
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.
Chapter 19 Implementing Trees and Priority Queues Fundamentals of Java.
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.
For Monday Read Weiss, chapter 7, sections 1-3. Homework –Weiss, chapter 4, exercise 6. Make sure you include parentheses where appropriate.
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,
Tree ADTs Tree concepts. Applications of Trees. A Tree ADT – requirements, contract. Linked implementation of Trees. Binary Tree ADTs. Binary Search.
Tree Data Structures. Introductory Examples Willliam Willliam BillMary Curt Marjorie Richard Anne Data organization such that items of information are.
Tree Data Structures.
Prof. Amr Goneid, AUC1 CSCE 210 Data Structures and Algorithms Prof. Amr Goneid AUC Part 4. Trees.
 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.
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.
Trees : Part 1 Section 4.1 (1) Theory and Terminology (2) Preorder, Postorder and Levelorder Traversals.
Computer Science 112 Fundamentals of Programming II Introduction to Trees.
Advance Data Structure 1 College Of Mathematic & Computer Sciences 1 Computer Sciences Department م. م علي عبد الكريم حبيب.
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.
Min Chen School of Computer Science and Engineering Seoul National University Data Structure: Chapter 6.
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.
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.
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 Chapter 10. CS 308 2Chapter Trees Preview: Preview: The data organizations presented in previous chapters are linear, in that items are one.
CHAPTER 5 TREE CSEB324 DATA STRUCTURES & ALGORITHM.
© 2006 Pearson Addison-Wesley. All rights reserved11 A-1 Chapter 11 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.
Binary Tree.
1 Trees What is a Tree? Tree terminology Why trees? What is a general tree? Implementing trees Binary trees Binary tree implementation Application of Binary.
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.
Foundation of Computing Systems Lecture 4 Trees: Part I.
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.
Chapter 05 Trees (Part I). Definition A tree is a finite set of one or more nodes including: ▫A starting node called root. ▫The rest of nodes can be split.
1 Trees : Part 1 Reading: Section 4.1 Theory and Terminology Preorder, Postorder and Levelorder Traversals.
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.
 آشنايي با درخت  درخت هاي دودويي  پيمايش درختان  هرم  جنگل اهداف 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.
Trees Chapter 15.
CSCE 210 Data Structures and Algorithms
Fundamentals of Programming II Introduction to Trees
Chapter 11 Trees © 2011 Pearson Addison-Wesley. All rights reserved.
TREES General trees Binary trees Binary search trees AVL trees
2017, Fall Pusan National University Ki-Joune Li
Data Structures Chapter 5: Trees.
CSE 373, Copyright S. Tanimoto, 2002 Binary Trees -
Trees Chapter 10.
2018, Fall Pusan National University Ki-Joune Li
CSE 373, Copyright S. Tanimoto, 2001 Binary Trees -
Tree.
Chapter 20: Binary Trees.
Chapter 11 Trees © 2011 Pearson Addison-Wesley. All rights reserved.
Binary Trees.
NATURE VIEW OF A TREE leaves branches root. NATURE VIEW OF A TREE leaves branches root.
2019, Fall Pusan National University Ki-Joune Li
Presentation transcript:

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  A specially designated node called root.  The remaining nodes are partitioned into n  0 disjoint sets T 1, …, T n, called subtrees. root T1T1 T2T2 TnTn

Terminology A B C D EF G HIJ L M node leaf parent B children E siblings B, D level root K ancestors A, D, H height or depth = maximum level of any node in the tree = 4 degree 3 degree of a tree = maximum of the degree of the nodes = 3

Representation of Trees Tree 的表示方法,常用的有下列三種  List Representation  Left Child-Right Sibling Representation  Representation as a Degree-Two Tree

Representation of Trees (1) List Representation  利用 generalized lists 來表示 (A( B(E(K, L), F), C(G), D(H(M), I, J ) ) )  利用 fixed size 的 Node ABF0EKL0CG00 本圖未完 成,留給 同學練習

Representation of Trees (1) Lemma 5.1 If T is a k-ary tree (i.e., a tree of degree k) with n nodes, each having a fixed size as in Figure 5.4, then n(k-1) + 1 of the nk child fileds are 0, n ≥ 1. DataChild 1Child 2Child 3Child 4 … Child k Wasting memory!

Representation of Trees (2) A BCD EF G HIJ L M K data left childRight sibling

Representation of Trees (3) A B C D E FG H I J L M K

Relation of Tree Data Structures Binary Tree Complete Binary TreeBinary Search Tree Search Struct Max Heap TreeWinner Max PQ

Binary Trees A binary tree is 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. A BC D EE

Binary Trees VS. Trees category item TreesBinary Trees The order of the subtreesnone distinctions between left and right Empty (zero nodes)NoYes degree0..n 0..2 ( 註 ) 註:修正老師上課時的說法

Full Binary Tree A full binary tree of depth k is a binary tree of depth k having 2 k – 1 nodes, k ≥ 0. A BC D IH G M level EF JKLNO

Complete Binary Tree A binary tree with n nodes and depth k is complete if and only if its nodes correspond to the nodes numbered from 1 to n in the full binary tree of depth k level A BC DGFE IH

Properties of Binary Trees Lemma 5.2 [Maximum number of nodes]  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. Lemma 5.3 [Relation between number of leaf nodes and nodes of degree 2]  For any non-empty binary tree, T, if n 0 is the number of leaf nodes and n2 the number of nodes of degree 2, then n 0 = n

Properties of Binary Trees Lemma 5.4 If a complete binary tree with n nodes is represented sequentially, then for any node with index i, 1 ≤ i ≤ n, we have:  parent( i ) is at if i ≠1. If i = 1, i is at the root and has no parent.  left_child( i ) is at 2i if 2i ≤ n. If 2i > n, then i has no left child.  right_child (i) is at 2i + 1 if 2i + 1 ≤ n. If 2i + 1 > n, then i has no right child. Position zero of the array is not used.

Binary Tree Representations Array Representation  Use Lemma 5.4 Linked Representation

Binary Tree Representations (Array) A B C D E — A B — C — — — D — E skewed binary tree

Binary Tree Representations (Array) A BC D G FE IH — A B C D E F G — — —

Binary Tree Representations (Linked) A B C D E LeftChild data RightChild data LeftChild Right Child A0 B0 C0 D0 0E0 root

Binary Tree Representations (Linked) A B C D EH0I0 F0G0 root

Binary Tree Representations (Linked) class Tree; //forward declaration class TreeNode { friend class Tree; private: TreeNode *LeftChild; char data; TreeNode *RightChild; }; class Tree { public: //Tree operation private: TreeNode *root; };

Manipulation of Binary Tree Traversal  Inorder (LVR) (Stack)  Postorder (LRV) (Stack)  Preorder (VLR) (Stack)  Level-Order (Queue) Copying Binary Trees Testing Equality  Two binary trees are equal if their topologies are the same and the information in corresponding nodes is identical.

Binary Tree Traversal ( Inorder LVR ) + *E *D /C AB A / B * C * D + E

Binary Tree Traversal ( Postorder LRV ) + *E *D /C AB A B / C * D * E +

Binary Tree Traversal ( Preorder VLR ) + *E *D /C AB + * * / A B C D E

Binary Tree Traversal ( Level-Order ) + *E *D /C AB + * E * D / C A B

Implement of Binary Tree Traversal Recursive Method  Program 5.1 (p263) --- Inorder  Program 5.2 (p264) --- Preorder  Program 5.3 (p265) --- Postorder Iterative Method  Program 5.4 (p266) --- Inorder  Program 5.5 and Use Iterator (Inorder)

Implement of Binary Tree Traversal Recusive void Tree :: inorder() // Driver calls workhorse for traversal of entire tree. The // driver is declared as a public member function of Tree. { inorder(root); } void Tree :: inorder(TreeNode *CurrentNode) // Workhorse traverses the subtree rooted at CurrentNode // The workhorse is declared as a private member function of Tree. { if(CurrentNode){ inorder(CurrentNode->LeftChild); cout data; inorder(CurrentNode->RightChild); }

Implement of Binary Tree Traversal Iterative void Tree::NonrecInorder() //nonrecursive inorder traversal using a stack { Stack s; //declare and initialize stack TreeNode *CurrentNode = root; while (1) { while (CurrentNode) { //move down LeftChild fields s.Add(CurrentNode); //add to stack CurrentNode = CurrentNode->LeftChild; } if (!s.IsEmpty()) { //stack is not empty CurrentNode = *s.Delete(CurrentNode); cout data << endl; CurrentNode = CurrentNode->RightChild; } else break; }

Implement of Binary Tree Traversal void Tree::LevelOrder() //Traverse the binary tree in level order { Queue q; TreeNode *CurrentNode = root; while (CurrentNode) { cout data<<endl; if (CurrentNode->LeftChild) q.Add(CurrentNode->LeftChild); if (CurrentNode->RightChild) q.Add(CurrentNode->RightChild); CurrentNode = *q.Delete(); }

Traversal without Stack 二種方式  每個 node 都增加一個欄位紀錄 parent node 的位 置。  將 binary trees 改成 threaded binary Trees.