Advance Data Structure 1 College Of Mathematic & Computer Sciences 1 Computer Sciences Department م. م علي عبد الكريم حبيب.

Slides:



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

Main Index Contents 11 Main Index Contents Week 6 – Binary Trees.
1 A Two-Level Binary Expression ‘-’ ‘8’ ‘5’ treePtr INORDER TRAVERSAL : has value 3 PREORDER TRAVERSAL: POSTORDER TRAVERSAL: 8 5 -
Advance Data Structure 1 College Of Mathematic & Computer Sciences 1 Computer Sciences Department م. م علي عبد الكريم حبيب.
Binary Trees, Binary Search Trees CMPS 2133 Spring 2008.
CS 206 Introduction to Computer Science II 09 / 22 / 2008 Instructor: Michael Eckmann.
Introduction to Data Structure, Fall 2006 Slide- 1 California State University, Fresno Introduction to Data Structure Chapter 10 Ming Li Department of.
© 2006 Pearson Addison-Wesley. All rights reserved11 A-1 Chapter 11 Trees.
1 Chapter 7 Trees. 2 What is a Tree In computer science, a tree is an abstract model of a hierarchical structure A tree consists of nodes with a parent-child.
Binary Tree Properties & Representation. Minimum Number Of Nodes Minimum number of nodes in a binary tree whose height is h. At least one node at each.
CS 206 Introduction to Computer Science II 02 / 11 / 2009 Instructor: Michael Eckmann.
Starting Out with C++: Early Objects 5/e © 2006 Pearson Education. All Rights Reserved Starting Out with C++: Early Objects 5 th Edition Chapter 19 Binary.
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.
Review Binary Tree Binary Tree Representation Array Representation Link List Representation Operations on Binary Trees Traversing Binary Trees Pre-Order.
Advanced Algorithms Analysis and Design Lecture 8 (Continue Lecture 7…..) Elementry Data Structures By Engr Huma Ayub Vine.
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 122 – Data Structures Templatized Tree.
CS Data Structures Chapter 5 Trees. Chapter 5 Trees: Outline  Introduction  Representation Of Trees  Binary Trees  Binary Tree Traversals 
Trees. Containers we have studied so far are linear. To represent nonlinear, i.e. hierarchal data we use trees. Nonlinear Containers root node leaf edge.
Lecture 10 Trees –Definiton of trees –Uses of trees –Operations on a tree.
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.
1 Binary Trees (7.3) CSE 2011 Winter November 2015.
Topics Definition and Application of Binary Trees Binary Search Tree Operations.
CE 221 Data Structures and Algorithms Chapter 4: Trees (Binary) Text: Read Weiss, §4.1 – 4.2 1Izmir University of Economics.
Review 1 Queue Operations on Queues A Dequeue Operation An Enqueue Operation Array Implementation Link list Implementation Examples.
Lecture - 10 on Data Structures. 6:05:57 PM Prepared by, Jesmin Akhter, Lecturer, IIT,JU.
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.
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.
CS 206 Introduction to Computer Science II 10 / 02 / 2009 Instructor: Michael Eckmann.
Binary Tree 10/22/081. Tree A nonlinear data structure Contain a distinguished node R, called the root of tree and a set of subtrees. Two nodes n1 and.
© University of Auckland Trees – (cont.) CS 220 Data Structures & Algorithms Dr. Ian Watson.
CH 7. TREES ACKNOWLEDGEMENT: THESE SLIDES ARE ADAPTED FROM SLIDES PROVIDED WITH DATA STRUCTURES AND ALGORITHMS IN C++, GOODRICH, TAMASSIA AND MOUNT (WILEY.
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. Iterative Preorder Traversal Rpreorder(T) 1. [process the root node] if T!= NULL then Write Data(T) else Write “empty Tree” 2. [process the left subtree]
Copyright © 2012 Pearson Education, Inc. Chapter 20: Binary Trees.
AL-HUSEEN BIN TALAL UNIVERSITY College of Engineering Department of Computer Engineering Algorithms and Data Structures Binary Tree Course No.:
Concepts of Algorithms CSC-244 Unit 19 & 20 Binary Search Tree (BST) Shahid Iqbal Lone Computer College Qassim University K.S.A.
Binary Tree.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 20: Binary Trees.
CH 7 : TREE ACKNOWLEDGEMENT: THESE SLIDES ARE ADAPTED FROM SLIDES PROVIDED WITH DATA STRUCTURES AND ALGORITHMS IN C++, GOODRICH, TAMASSIA AND MOUNT (WILEY.
Foundation of Computing Systems Lecture 4 Trees: Part I.
Graphs and Trees Mathematical Structures for Computer Science Chapter 5 Copyright © 2006 W.H. Freeman & Co.MSCS SlidesGraphs and Trees.
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.
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. 
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 20: Binary Trees.
IKI 10100: Data Structures & Algorithms Ruli Manurung (acknowledgments to Denny & Ade Azurat) 1 Fasilkom UI Ruli Manurung (Fasilkom UI)IKI10100: Lecture13.
TREE Ahsan Ahmed College of Computer and Information Science Majma’ah University 1.
1 Trees. 2 Trees Trees. Binary Trees Tree Traversal.
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.
Traversal From CSCE 3110 Data Structures & Algorithm Analysis
Recursive Objects (Part 4)
Trees 8/7/2018 2:27 PM Binary Trees © 2010 Goodrich, Tamassia Trees.
Trees Another Abstract Data Type (ADT)
Data Structures & Algorithm Design
Binary Trees, Binary Search Trees
Chapter 20: Binary Trees.
TREES General trees Binary trees Binary search trees AVL trees
Introduction to Trees IT12112 Lecture 05.
Chapter 21: Binary Trees.
Trees Another Abstract Data Type (ADT)
Trees.
Trees Definitions Implementation Traversals K-ary Trees
Trees Another Abstract Data Type (ADT)
Binary Trees, Binary Search Trees
Binary Tree Properties & Representation
Chapter 20: Binary Trees.
Binary Trees, Binary Search Trees
Data Structures Using C++ 2E
NATURE VIEW OF A TREE leaves branches root. NATURE VIEW OF A TREE leaves branches root.
Presentation transcript:

Advance Data Structure 1 College Of Mathematic & Computer Sciences 1 Computer Sciences Department م. م علي عبد الكريم حبيب

2 Binary Tree Representations (Array)  An array can be used to store the nodes of a binary tree.  The nodes stored in an array of memory can be accessed sequentially.  Consider a binary tree T of depth d.  Then at most 2 d – 1 nodes can be there in T.  (i.e. SIZE = 2 d –1) So the array of size “SIZE” to represent the binary tree.  Consider a binary tree in Fig. below of depth 3.  Then SIZE = 2 3 – 1 = 7.  Then the array A[7] is declared to hold the nodes.

3 Binary Tree of Depth 3

To perform any operation often we have to identify the father, the left child and right child of an arbitrary node.  The father of a node having index n can be obtained by (n – 1)/2. For example to find the father of D, where array index n = 3. Then the father nodes index can be obtained  = (n – 1)/2  = 3 – 1/2  = 2/2  = 1 (i.e. father of D is B at index 1)  The left child of a node having index n can be obtained by (2n+1). For example to find the left child of C, where array index n = 2. Then it can be obtained by  = (2n +1)  = 2*2 + 1  =  = 5 (i.e. left child of C is F at index 5) 4

 The right child of a node having array index n can be obtained by the formula (2n + 2). For example to find the right child of B, where the array index n = 1. Then  = (2n + 2)  = 2*1 + 2  = 4 (i.e. right child of B is E at index 4)  If the left child is at array index n, then its right brother is at (n + 1). Similarly, if the right child is at index n, then its left brother is at (n – 1).  The array representation is more ideal for the complete binary tree. In case of incomplete binary tree there is memory wastage as memory allocated to even if there is no left or right child of the non-terminal nodes. 5

Linked List Representation ( B Tree )  The most popular and practical way of representing a binary tree is using linked list (or pointers). In linked list, every element is represented as nodes. A node consists of three fields such as :  (a) Left Child (LChild)  (b) Information of the Node (Info)  (c) Right Child (RChild)  The LChild links points to the left child of the parent node, Info holds the information of evey node and the RChild holds the address of right child node of the parent node. Fig. shows the structure of a binary tree node. 6

7 Binary Tree Representations (Linked List) If a node has no left or / and right node, corresponding LChild or RChild is assigned to NULL

 Structural Definition of Binary Trees A binary tree is either empty or it has 3 parts:  a value  a left subtree  a right subtree  Every node in a Binary tree has a structure like this - struct NODE { struct NODE *leftchild; int nodevalue; /*this can be of any type*/ struct NODE *rightchild; };  The 'leftchild' and 'rightchild' are pointers to another tree-node. The "leafnode" will have NULL values for these pointers. 8

Properties and Application Of B tree  Properties of a Binary Tree: * Each internal node has two children * The children of a node are an ordered pair.  The children of an internal node is referred to as right child and left child.  Maximum number of nodes on level I of a binary tree is 2 i-1, i>=1  Applications  Arithmetic Expressions  Decision Process  Searching 9

Arithmetic Expression Tree  In a binary tree associated with an arithmetic expression  The internal nodes : operators  The externals nodes: operands.  Example : Arithmetic Expression Tree for the expression (2 * ( a – 1 ) + ( 3 * b ) ) 10

Decision Tree  In a binary tree associated with a decision process  The internal nodes : questions with yes/ no answers  The externals nodes: decisions.  Example: Dining Decision 11

Tree Traversal  Traversing a data structure: to process every node exactly once.  To traverse a binary tree, do the following steps in some order: (L) traverse the left subtree. (R) traverse the right subtree. (N) process the node itself.  Traversing Binary Trees Pre-Order Traversal: (1) Process the node (N). (2) Traverse the left subtree (L). (3) Traverse the right subtree (R). 12

Tree Traversal Contd..  Inorder Traversal: (1) Traverse the left subtree (L). (2) Process the node (N). (3) Traverse the right subtree (R).  Postorder Traversal: (1) Traverse the left subtree (L). (2) Traverse the right subtree (R). (3) Process the node (N).  From the figure, we know that Inorder : B D A E C F Preorder : A B D C E F Postorder : D B E F C A 13

Recursive implementation for INORDER traversal.  C ++ implementation:- struct NODE { struct NODE *left; int value; struct NODE *right; };  inorder(struct NODE *curr) { if(curr->left != NULL) inorder(curr->left); /*step-1 & step-2*/ cout value ; /*step-3*/ if(curr->right != NULL) inorder(curr->right); /*step-4*/ 14

Pre-Order Traversal Recursively  C ++ Implementation:- struct NODE { struct NODE *left; int value; struct NODE *right; };  Preorder(struct NODE *curr) {  Cout value ; if(curr->left != NULL) Preorder(curr->left); if(curr->right != NULL) Preorder(curr->right); } 15

Post-Order Traversal Recursively  C ++ Implementation:- struct NODE { struct NODE *left; int value; struct NODE *right; };  Preorder(struct NODE *curr) {  if(curr->left != NULL) Preorder(curr->left); if(curr->right != NULL) Preorder(curr->right); Cout value ; } 16

Exercise 17  Find the preorder, post order and in order traversals for the following trees (1) (2)