Review 1 Queue Operations on Queues A Dequeue Operation An Enqueue Operation Array Implementation Link list Implementation Examples.

Slides:



Advertisements
Similar presentations
Binary Trees, Binary Search Trees CMPS 2133 Spring 2008.
Advertisements

1 Trees. 2 Outline –Tree Structures –Tree Node Level and Path Length –Binary Tree Definition –Binary Tree Nodes –Binary Search Trees.
Lists A list is a finite, ordered sequence of data items. Two Implementations –Arrays –Linked Lists.
Binary Tree B G E D I H F c A Binary tree
© 2006 Pearson Addison-Wesley. All rights reserved11 A-1 Chapter 11 Trees.
Unit 11a 1 Unit 11: Data Structures & Complexity H We discuss in this unit Graphs and trees Binary search trees Hashing functions Recursive sorting: quicksort,
Data Structures Using C++ 2E Chapter 11 Binary Trees and B-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.
C o n f i d e n t i a l HOME NEXT Subject Name: Data Structure Using C Unit Title: Trees.
Binary Trees Chapter 6.
Data Structures and Algorithms Session 13 Ver. 1.0 Objectives In this session, you will learn to: Store data in a tree Implement a binary tree Implement.
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.
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.
COSC2007 Data Structures II
Review Binary Tree Binary Tree Representation Array Representation Link List Representation Operations on Binary Trees Traversing Binary Trees Pre-Order.
Introduction Of Tree. Introduction A tree is a non-linear data structure in which items are arranged in sequence. It is used to represent hierarchical.
CS Data Structures Chapter 5 Trees. Chapter 5 Trees: Outline  Introduction  Representation Of Trees  Binary Trees  Binary Tree Traversals 
Trees EENG212 Algorithms and Data Structures. Trees Outline  Introduction to Trees  Binary Trees: Basic Definitions  Traversing Binary Trees  Node.
Lecture 81 Data Structures, Algorithms & Complexity Tree Algorithms GRIFFITH COLLEGE DUBLIN.
Lecture 10 Trees –Definiton of trees –Uses of trees –Operations on a tree.
Chapter 19 Implementing Trees and Priority Queues Fundamentals of Java.
Chapter 19 Implementing Trees and Priority Queues Fundamentals of Java.
Chapter 6 Binary Trees. 6.1 Trees, Binary Trees, and Binary Search Trees Linked lists usually are more flexible than arrays, but it is difficult to use.
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 CSC 211 Data Structures Lecture 24 Dr. Iftikhar Azim Niaz 1.
Data Structures and Algorithm Analysis Trees Lecturer: Jing Liu Homepage:
Compiled by: Dr. Mohammad Omar Alhawarat
Prof. Amr Goneid, AUC1 CSCE 210 Data Structures and Algorithms Prof. Amr Goneid AUC Part 4. Trees.
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.
CE 221 Data Structures and Algorithms Chapter 4: Trees (Binary) Text: Read Weiss, §4.1 – 4.2 1Izmir University of Economics.
Trees : Part 1 Section 4.1 (1) Theory and Terminology (2) Preorder, Postorder and Levelorder Traversals.
1 Storing Hierarchical Information Lists, Stacks, and Queues represent linear sequences Data often contain hierarchical relationships that cannot be expressed.
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.
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.
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.
M180: Data Structures & Algorithms in Java Trees & Binary Trees Arab Open University 1.
Trees Namiq Sultan. Trees Trees are very flexible, versatile and powerful non-liner data structure that can be used to represent data items possessing.
DATA STRUCTURE BS(IT)3rd. Tree An Introduction By Yasir Mustafa Roll No. BS(IT) Bahauddin Zakariya University, Multan.
Data Structures Using C++ 2E Chapter 11 Binary Trees.
Concepts of Algorithms CSC-244 Unit 19 & 20 Binary Search Tree (BST) Shahid Iqbal Lone Computer College Qassim University K.S.A.
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.
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 Trees : Part 1 Reading: Section 4.1 Theory and Terminology Preorder, Postorder and Levelorder Traversals.
DATA STRUCURES II CSC QUIZ 1. What is Data Structure ? 2. Mention the classifications of data structure giving example of each. 3. Briefly explain.
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.
Trees Chapter 15.
CSCE 210 Data Structures and Algorithms
Lecture 1 (UNIT -4) TREE SUNIL KUMAR CIT-UPES.
Tree.
Data Structures & Algorithm Design
Lecture 18. Basics and types of Trees
Binary Trees, Binary Search Trees
TREES General trees Binary trees Binary search trees AVL trees
Introduction to Trees IT12112 Lecture 05.
Trees and Binary Trees.
CSE 373, Copyright S. Tanimoto, 2002 Binary Trees -
Binary Trees, Binary Search Trees
Trees.
CSE 373, Copyright S. Tanimoto, 2001 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:

Review 1 Queue Operations on Queues A Dequeue Operation An Enqueue Operation Array Implementation Link list Implementation Examples

Review 1. Circular queue 2. Double Ended Queue 3. Priority queue 2

Trees Binary Tree Binary Tree Representation Array Representation Link List Representation Operations on Binary Trees Traversing Binary Trees Pre-Order Traversal Recursively In-Order Traversal Recursively Post-Order Traversal Recursively 3

Trees Trees are very flexible, versatile and powerful non-liner data structure It can be used to represent data items possessing hierarchical relationship A tree can be theoretically defined as a finite set of one or more data items (or nodes) such that There is a special node called the root of the tree Remaining nodes (or data item) are partitioned into number of subsets each of which is itself a tree, are called sub tree A tree is a set of related interconnected nodes in a hierarchical structure 4

Trees Where have you seen a tree structure before? Examples of trees: - Directory tree - Family tree - Company organization chart - Table of contents - etc. 5

Basic Terminologies Root is a specially designed node (or data items) in a tree It is the first node in the hierarchical arrangement of the data items For example, Figure 1. A Tree 6

In this example ‘A’ is the root node Each data item in a tree is known as node It specifies the data information and links to other data items Degree of a node Degree of a node is the number of subtrees of a node in a given tree In the above example the degree of node A is 3 the degree of node B is 2 the degree of node C is 2 the degree of node D is 3 7

The Degree of a Tree The degree of a tree is the maximum degree of node in a given tree The degree of node J is 4 All other nodes have less or equal degree So the degree of the above tree is 4 A node with degree zero (0) is called terminal node or a leaf In the above example, nodes M,N,I,O etc are leaf nodes Any node whose degree is not zero is called a non-terminal node 8

Levels of a Tree The tree is structured in different levels The entire tree is leveled in such a way that the root node is always of level 0 Its immediate children are at level 1 and their immediate children are at level 2 and so on up to the terminal nodes If a node is at level n then its children will be at level n+1 9

Depth of a Tree Depth of a tree is the maximum level of any node in a given tree The number of levels from root to the leaves is called depth of a tree The term height is also used to denote the depth of a tree 10

A vertex (or node) is a simple object that can have a name and can carry other associated information An edge is a connection between two vertices A path in a tree is a list of distinct vertices in which successive vertices are connected by edges in the tree example: {a, b, d, i } is path The defining property of a tree is that there is precisely one path connecting any two nodes 11

Example of a Tree 12

Binary Trees A binary tree is a tree in which no node can have more than 2 children These children are described as “left child” and “right child” of the parent node A binary tree T is defined as a finite set of elements called nodes such that T is empty if T has no nodes called the null or empty tree T contains a special node R, called root node of T and the remaining nodes of T form an ordered pair of disjoined binary trees T1 and T2. They are called left and right sub tree of R 13

Consider the following binary tree T Here ‘A’ is the root node of the tree ‘B’ is the left child of ‘A’ and ‘C’ is the right child of ‘A’ i.e. ‘A’ is the father of ‘B’ and ‘C’ The nodes ‘B’ and ‘C’ are called brothers 14

Strictly Binary Tree The tree is said to be strictly binary tree, if every non-leaf node in a binary tree has non-empty left and right sub trees A strictly binary tree with n level always contains 2 n –1 nodes Every node in the strictly binary tree can have either no children or two children They are also called 2-tree or extended binary tree 15

Example of a strictly binary tree 16

Application of an extended binary tree The main application of a 2-tree is to represent and compute any algebraic expression using binary operation For example, consider an algebraic expression E E = (a + b)/((c – d )*e) E can be represented by means of the extended binary tree T as shown in figure Each variable or constant in E appears as an internal node in T whose left and right sub tree corresponds to the operands of the operation 17

E = (a + b)/((c – d )*e) 18

A complete binary tree at depth ‘d’ is the strictly binary tree where all the leaves are at level d Following figure illustration the complete binary tree of depth 3 19

A binary tree with n nodes, n > 0, has exactly n – 1 edges A binary tree of depth d, d > 0, has at least d and at most 2 d – 1 nodes in it If a binary tree contains n nodes at level l, then it contains at most 2n nodes at level l + 1 The main difference between a binary tree and ordinary tree is 1. A binary tree can be empty where as a tree cannot 2. Each element in binary tree has exactly two sub trees. Each element in a tree can have any number of sub trees 3. The sub tree of each element in a binary tree are ordered, left and right sub trees. The sub trees in a tree are unordered 20

If a binary tree has only left sub trees, then it is called left skewed binary tree Following is an example of left skewed binary tree 21

If a binary tree has only right sub trees, then it is called right skewed binary tree Following is an example 22

Binary Tree Representation There are two ways of representing binary tree T in memory : 1. Sequential representation using arrays 2. Linked list representation Array Representation 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 Suppose 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 23

Consider a binary tree in the figure of depth 3 Then SIZE = 2 3 – 1 = 7 Then the array A[7] is declared to hold the nodes 24

The array representation of the binary tree is shown below: To perform any operation often we have to identify the father, the left child and right child of an arbitrary node 1. 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., A[1] is the father of D, which is B 25

2. 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., A[5] is the left child of C, which is NULL. So no left child for C. 26

3. 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., A[4] is the right child of B, which is E. 27

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 The Fig. given in this example is not a complete binary tree Since there is no left child for node C, i.e., A[5] is vacant. Even though memory is allocated for A[5] it is not used, so wasted unnecessarily 28

Linked List Representation The most popular and practical way of representing a binary tree is using linked list 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) 29

The LChild links to the left child node of the parent node Info holds the information of the node And RChild holds the address of the right child node of the parent node The following figure shows the structure of a binary tree node: 30

For example we have the following binary tree We want to present this binary tree in linked list form 31

The linked list representation of this binary tree is: If a node has no left or/and right node, corresponding LChild or RChild is assigned to NULL 32

Operations on Binary Trees The basic operations that are commonly performed on binary trees are listed below: Create an empty binary tree Traversing a binary tree Inserting a new node Deleting a Node Searching for a Node Determine the total no. of Nodes Determine the total no. of leaf Nodes Determine the total no. of non-leaf Nodes Find the smallest element in the tree Finding the largest element Find the Height of the tree Finding the Father/Left Child/Right Child/Brother of an arbitrary node 33

Traversing Binary Trees Tree traversal is one of the most common operations performed on trees In traversing right sub-tree is always traversed after left sub-tree It is a way in which each node in the tree is visited exactly once in a systematic manner There are three standard ways of traversing a binary tree 1. Pre Order Traversal (Root-left-right) 2. In order Traversal (Left-root-right) 3. Post Order Traversal (Left-right-root) 34

Pre-Order Traversal Recursively To traverse a non-empty binary tree in pre-order following steps are to be processed 1. Visit the root node 2. Traverse the left sub tree in preorder 3. Traverse the right sub tree in preorder That is, in pre-order traversal, the root node is visited (or processed) first, before traversing through left and right sub trees recursively 35

void preorder (Node * Root) { If (Root != NULL) { cout <<Root → Info; preorder(Root → L child); preorder(Root → R child); } 36

For Example The preorder traversal of this binary tree is A, B, D, E, H, I, C, F, G, J. 37

In-Order Traversal Recursively The in order traversal of a non-empty binary tree is defined as follows : 1. Traverse the left sub tree in order 2. Visit the root node 3. Traverse the right sub tree in order In order traversal, the left sub tree is traversed recursively, before visiting the root After visiting the root the right sub tree is traversed recursively 38

The procedure for an in order traversal is given below : void inorder (NODE *Root) { If (Root != NULL) { inorder(Root → L child); cout<<Root → info; inorder(Root → R child); } 39

For Example The in order traversal of this binary tree is D, B, H, E, I, A, F, C, J, G. 40

Post-Order Traversal Recursively The post order traversal of a non-empty binary tree can be defined as : 1. Traverse the left sub tree in post order 2. Traverse the right sub tree in post order 3. Visit the root node In Post Order traversal, the left and right sub tree(s) are recursively processed before visiting the root 41

The procedure for post order traversal is given below: void postorder (NODE *Root) { If (Root != NULL) { postorder(Root → Lchild); postorder(Root → Rchild); cout<<Root → info; } 42

For Example The post order traversal of this binary tree is D, H, I, E, B, F, J, G, C, A 43

Summary Binary Tree Binary Tree Representation Array Representation Link List Representation Operations on Binary Trees Traversing Binary Trees Pre-Order Traversal Recursively In-Order Traversal Recursively Post-Order Traversal Recursively 44