UNIT VI TREES. Marks - 14

Similar documents
The Tree Data Model. Laura Kovács

SET 1C Binary Trees. 2. (i) Define the height of a binary tree or subtree and also define a height balanced (AVL) tree. (2)

Ch 10 Trees. Introduction to Trees. Tree Representations. Binary Tree Nodes. Tree Traversals. Binary Search Trees

COSC160: Data Structures Binary Trees. Jeremy Bolton, PhD Assistant Teaching Professor

2 all subsequent nodes. 252 all subsequent nodes. 401 all subsequent nodes. 398 all subsequent nodes. 330 all subsequent nodes

Practical session No. 5 Trees

1 Solutions to Tute09

Practical session No. 5 Trees

CSE 21 Winter 2016 Homework 6 Due: Wednesday, May 11, 2016 at 11:59pm. Instructions

MAT385 Final (Spring 2009): Boolean Algebras, FSM, and old stuff

Binary Tree Applications

Supporting Information

Successor. CS 361, Lecture 19. Tree-Successor. Outline

Lecture l(x) 1. (1) x X

Homework #4. CMSC351 - Spring 2013 PRINT Name : Due: Thu Apr 16 th at the start of class

Binary Decision Diagrams

Binary Decision Diagrams

CSCI 104 B-Trees (2-3, 2-3-4) and Red/Black Trees. Mark Redekopp David Kempe

Splay Trees. Splay Trees - 1

4/8/13. Part 6. Trees (2) Outline. Balanced Search Trees. 2-3 Trees Trees Red-Black Trees AVL Trees. to maximum n. Tree A. Tree B.

> asympt( ln( n! ), n ); n 360n n

It is used when neither the TX nor RX knows anything about the statistics of the source sequence at the start of the transmission

Fundamental Algorithms - Surprise Test

Splay Trees Goodrich, Tamassia, Dickerson Splay Trees 1

TABLEAU-BASED DECISION PROCEDURES FOR HYBRID LOGIC

On the Optimality of a Family of Binary Trees Techical Report TR

Heaps. Heap/Priority queue. Binomial heaps: Advanced Algorithmics (4AP) Heaps Binary heap. Binomial heap. Jaak Vilo 2009 Spring

CSE 100: TREAPS AND RANDOMIZED SEARCH TREES

Heaps

Priority Queues 9/10. Binary heaps Leftist heaps Binomial heaps Fibonacci heaps

3/7/13. Binomial Tree. Binomial Tree. Binomial Tree. Binomial Tree. Number of nodes with respect to k? N(B o ) = 1 N(B k ) = 2 N(B k-1 ) = 2 k

Introduction to Greedy Algorithms: Huffman Codes

Advanced Algorithmics (4AP) Heaps

AVL Trees. The height of the left subtree can differ from the height of the right subtree by at most 1.

Information Theory and Coding Prof. S. N. Merchant Department of Electrical Engineering Indian Institute of Technology, Bombay

Principles of Program Analysis: Algorithms

Recall: Data Flow Analysis. Data Flow Analysis Recall: Data Flow Equations. Forward Data Flow, Again

Binary and Binomial Heaps. Disclaimer: these slides were adapted from the ones by Kevin Wayne

Basic Data Structures. Figure 8.1 Lists, stacks, and queues. Terminology for Stacks. Terminology for Lists. Chapter 8: Data Abstractions

Notes on Natural Logic

Node betweenness centrality: the definition.

PRIORITY QUEUES. binary heaps d-ary heaps binomial heaps Fibonacci heaps. Lecture slides by Kevin Wayne Copyright 2005 Pearson-Addison Wesley

Q1. [?? pts] Search Traces

Handout 4: Deterministic Systems and the Shortest Path Problem

Algorithms PRIORITY QUEUES. binary heaps d-ary heaps binomial heaps Fibonacci heaps. binary heaps d-ary heaps binomial heaps Fibonacci heaps

COMP Analysis of Algorithms & Data Structures

Rational Behaviour and Strategy Construction in Infinite Multiplayer Games

The exam is closed book, closed calculator, and closed notes except your three crib sheets.

CEC login. Student Details Name SOLUTIONS

Bioinformatics - Lecture 7

Design and Analysis of Algorithms

Chapter 16. Binary Search Trees (BSTs)

Heaps. c P. Flener/IT Dept/Uppsala Univ. AD1, FP, PK II Heaps 1

PARELLIZATION OF DIJKSTRA S ALGORITHM: COMPARISON OF VARIOUS PRIORITY QUEUES

6.231 DYNAMIC PROGRAMMING LECTURE 3 LECTURE OUTLINE

On the Optimality of a Family of Binary Trees

UNIT 2. Greedy Method GENERAL METHOD

Sublinear Time Algorithms Oct 19, Lecture 1

Lesson 9: Heuristic Search and A* Search

Initializing A Max Heap. Initializing A Max Heap

Data Structures. Binomial Heaps Fibonacci Heaps. Haim Kaplan & Uri Zwick December 2013

Course Information and Introduction

Design and Analysis of Algorithms 演算法設計與分析. Lecture 9 November 19, 2014 洪國寶

A relation on 132-avoiding permutation patterns

Chapter wise Question bank

CIS 540 Fall 2009 Homework 2 Solutions

MSU CSE Spring 2011 Exam 2-ANSWERS

What is Greedy Approach? Control abstraction for Greedy Method. Three important activities

Design and Analysis of Algorithms 演算法設計與分析. Lecture 8 November 16, 2016 洪國寶

1.6 Heap ordered trees

Introduction to Fall 2007 Artificial Intelligence Final Exam

Reasoning about B+ Trees with Operational Semantics and Separation Logic

Optimal Satisficing Tree Searches

6.854J / J Advanced Algorithms Fall 2008

Notes on the EM Algorithm Michael Collins, September 24th 2005

CS4311 Design and Analysis of Algorithms. Lecture 14: Amortized Analysis I

Design and Analysis of Algorithms. Lecture 9 November 20, 2013 洪國寶

Meld(Q 1,Q 2 ) merge two sets

Finding Equilibria in Games of No Chance

The Pill Problem, Lattice Paths and Catalan Numbers

Algorithmic Game Theory and Applications. Lecture 11: Games of Perfect Information

Search Space and Average Proof Length of Resolution. H. Kleine Buning T. Lettmann. Universitat { GH { Paderborn. Postfach 16 21

CS 188 Fall Introduction to Artificial Intelligence Midterm 1. ˆ You have approximately 2 hours and 50 minutes.

CS 188 Fall Introduction to Artificial Intelligence Midterm 1. ˆ You have approximately 2 hours and 50 minutes.

BITTIGER #11. Oct

Copyright 1973, by the author(s). All rights reserved.

Counting Basics. Venn diagrams

CTL Model Checking. Goal Method for proving M sat σ, where M is a Kripke structure and σ is a CTL formula. Approach Model checking!

Programme Evaluation and Review Techniques (PERT) And Critical Path Method (CPM) By K.K. Bandyopadhyay. August 2001

Contents. Heinemann Maths Zone

Outline for this Week

6.231 DYNAMIC PROGRAMMING LECTURE 3 LECTURE OUTLINE

CMPSCI 311: Introduction to Algorithms Second Midterm Practice Exam SOLUTIONS

Multirate Multicast Service Provisioning I: An Algorithm for Optimal Price Splitting Along Multicast Trees

Unit 6: Amortized Analysis

Binomial Heaps. Bryan M. Franklin

The exam is closed book, closed calculator, and closed notes except your one-page crib sheet.

Lecture 5: Tuesday, January 27, Peterson s Algorithm satisfies the No Starvation property (Theorem 1)

On the number of one-factorizations of the complete graph on 12 points

CS 4110 Programming Languages and Logics Lecture #2: Introduction to Semantics. 1 Arithmetic Expressions

Transcription:

UNIT VI TREES Marks - 14

SYLLABUS 6.1 Non-linear data structures 6.2 Binary trees : Complete Binary Tree, Basic Terms: level number, degree, in-degree and out-degree, leaf node, directed edge, path, depth, similar binary trees, copies of binary trees, General Tree, Conversion of General Tree to Binary Tree, Forest. 6.3 Binary Search Tree : Insertion of a node in binary tree, Deletion of a node in binary tree, Searching a node in binary tree 6.4 Tree Traversal : Inorder, Preorder, Postorder 6.5 Applications of binary tree

NON-LINEAR DATA STRUCTURES In the Non linear data structure processing of data items is not possible in linier fashion. Examples of the non linier data structure are: (A)Tree (B) Graph (A) Tree: In tree data contains hierarchical relationship. VPMP (B) Graph: this data structure contains relationship between pairs of elements.

TREE A tree is defined as a finite set of one or more nodes such that There is a special node called the root node R. The remaining nodes are divided into n 0 disjoint sets T1, T2,.,.,. TN, where each of these sets are tree. T1, T2., T n is called the sub tree of the root

BINARY TREE If in a directed tree the out degree of every node is less than or equal to 2 then the tree is called the binary tree.

COMPLETE BINARY TREE If the out degree of every node is exactly equal to 2 or 0 and the number of nodes at level i is 2 i-1 then the tree is called full or complete binary tree.

LEVEL NUMBER Level of a node represents the generation of a node. If the root node is at level 0, then its next child node is at level 1, its grandchild is at level 2, and so on. Example:

DEGREE / TOTAL DEGREE In a tree the sum of edges comes into particular node and edges comes out from particular node is called degree of that particular node. It means Degree is the sum of in degree and out degree. It is also called total degree of node. Example: Total degree of node B is 3

INDEGREE In a tree number of edges comes in to particular node is called Indegree of that particular node. Root node having indegree always equals to 0 because it is the first node of tree. Example: Indegree of node B is 1

OUTDEGREE In a tree number of edges comes out from particular node is called Out degree of that particular node. Leaf node having out degree always equals to 0 because it is the last node of tree having no further sub tree. Example: Oudegree of node B is 2.

LEAF NODE The node which does not have any child node is called the leaf node.

ROOT NODE The node at the top of the tree is called root. There is only one root per tree and one path from the root node to any node.

DIRECTED EDGE An edge of directed tree is called as directed edge. Here in above example an edge from A to B is directed edge.

PATH A sequence of connecting edges from one node to another node is called Path. In above figure path from Node A to H is given as A->B->D->H In above figure path from Node A to I is given as A->C->G->I

DEPTH The length of the path from Root Node to particular Node V is called depth of node V. In above figure depth of node E is 2 because the Path from Root Node A to Node E is A->B->E. Here the length of the Path is 2 so depth of node E is 2.

SIMILAR BINARY TREES If two binary trees are similar in structure then they are said to be similar binary trees. Consider following figure:

COPIES OF BINARY TREES If two binary trees are similar in structure and their corresponding nodes having same value then they are said to be copies of binary trees. Consider following figure:

GENERAL TREE A Tree in which each node having either 0 or more child nodes is called general tree. So we can say that a Binary Tree is a specialized case of General tree. General Tree is used to implement File System. Example:

FOREST A forest is a set of n 0 disjoint trees. Forest is a collection of disjoint trees. Consider Following Figure in which a forest is a collection of two disjoint tree.

CONVERSION OF GENERAL TREE TO BINARY TREE The process of converting general tree in to binary tree is given below: 1. Root node of general tree becomes root node of Binary Tree. 2. Now consider T1, T2, T3... Tn are child nodes of the root node in general tree. The left most child (T1) of the root node in general tree becomes left most child of root node in the binary tree. Now Node T2 becomes right child of Node T1, Node T3 becomes right child of Node T2 and so on in binary tree. 3. The same procedure of step 2 is repeated for each leftmost node in the general tree.

Example: General tree is given in the figure.

Step 1: Root Node of General tree becomes the Root node of binary tree. Step 2: Now Root Node (A) has three child Nodes (B, H, E) in general tree. The leftmost node (B) of the root node (A) in the general tree becomes the left most node of the root node (A) in binary tree.

Step 3: Now Node H becomes the right node of B and Node E becomes the right node of H. Step 4: Now Node B has only one left child node, which is C in general tree. So Node C becomes left child of Node B in binary tree.

Step 5: Now Node E has two child nodes (F, G). The leftmost node (F) of the node (E) in the general tree becomes the left most node of the node E in the binary tree.

Step 6: Now Node G becomes right node of Node F in binary tree.

BINARY SEARCH TREE: BST On binary search tree, we can perform several operations like, Insertion of a node in Binary Tree Deletion of a node from Binary tree Searching a node from Binary Tree

INSERTION OF A NODE IN BINARY TREE Binary search tree has following characteristics: All the nodes to the left of root node have value less than the value of root node. All the nodes to the right of root node have value greater than the value of root node.

Suppose we want to construct binary search tree for following set of data: 45 68 35 42 15 64 78 Step 1: First element is 45 so it is inserted as a root node of the tree. 45 Root Node Step 2: Now we have to insert 68. First we compare 68 with the root node which is 45. Since the value of 68 is greater then 45 so it is inserted to the right of the root node. 45 68

Step 3: Now we have to insert 35. First we compare 35 with the root node which is 45. Since the value of 35 is less then 45 so it is inserted to the left of the root node. 45 35 68 Step 4: Now we have to insert 42. 45 35 68 42

Step 5: Now we have to insert 15 45 35 68 15 42 Step 6: Now we have to insert 64. 45 35 68 15 42 64

Step 7: Now we have to insert 78. 45 35 68 15 42 64 78

DELETION OF A NODE FROM BINARY TREE Consider the following binary tree:

There are three possibilities when we want to delete an element from binary search tree. (1) If a node to be deleted has empty left and right sub tree then a node is deleted directly. Suppose we want to delete node 9

(2) If a node to be deleted has only one left sub tree or right sub tree then the sub tree of the deleted node is linked directly with the parent of the deleted node. Suppose we want to delete node 7

(3) If a node to be deleted has left and right sub tree then we have to do following steps: (a) Find next maximum (in order successor) of the deleted node. (b) Append the right sub tree of the in order successor to its grandparent. (c) Replace the node to be deleted with its next maximum (in order successor).

Suppose we want to delete node 55.

SEARCHING A NODE IN BINARY TREE First the key to be searched is compared with root node. If it is not in the root node then we have to possibilities : (1) If the key to be searched having value less than root node then we search the key in left sub tree. (2) If the key to be searched having value greater then root node then we search the key in right sub tree.

Step 1: If ROOT == NULL then Write Tree is Empty. Search Un Successful Exit Step 2: If X=ROOT->INFO then Write Search is Successful Return (ROOT) Step 3: If X < ROOT->INFO then Call SEARCH (ROOT->LPTR, X) Else Call SEARCH (ROOT->RPTR, X)

TREE TRAVERSAL Traversal: Traversal is the method of processing every nodes in the tree exactly once in a systematic manner. Types of Traversal: There are three different types of tree traversal. 1. Preorder Traversal 2. In order Traversal 3. Post order Traversal

TREE TRAVERSAL : INORDER The Inorder traversal follows the three steps: 1. Traverse the left sub tree in Post order. (Left node) 2. Process the root node. (Vertices or Node) 3. Traverse the right sub tree in Post order. (Right node)

Example: A B C D E F G Inorder Traversal: D B E A F C G

INORDER(T) Step 1:[Check for empty tree] If T == NULL then Write Tree is empty Exit Step 2:[Process the left sub tree] If LPTR (T) NULL then Call INORDER (LPTR (T)) Step 3:[Process the root node] Write (DATA(T)) Step 4:[Process the right sub tree] If RPTR (T) NULL then Call INORDER (RPTR (T)) Step 5:[Finished] Exit

TREE TRAVERSAL : PREORDER The Preorder traversal follows the three steps: 1. Process the root node. (Vertices or Node) 2. Traverse the left sub tree in Post order. (Left node) 3. Traverse the right sub tree in Post order. (Right node)

Example: A B C D E F G Preorder Traversal: A B D E C F G

PREORDER(T) Step 1:[Check for empty tree] If T == NULL then Write Tree is empty Exit Step 2:[Process the root node] Write (DATA(T)) Step 3:[Process the left sub tree] If LPTR (T) NULL then Call PREORDER (LPTR (T)) Step 4:[Process the right sub tree] If RPTR (T) NULL then Call PREORDER (RPTR (T)) Step 5:[Finished] Exit

TREE TRAVERSAL : POSTORDER The Post order traversal follows the three steps: 1. Traverse the left sub tree in Post order. (Left node) 2. Traverse the right sub tree in Post order. (Right node) 3. Process the root node. (Vertices or Node)

Example: A B C D E F G Post Order Traversal: D E B F G C A

POSTORDER(T) Step 1:[Check for empty tree] If T == NULL then Write Tree is empty Exit Step 2:[Process the left sub tree] If LPTR (T) NULL then Call POSTORDER (LPTR (T)) Step 3:[Process the right sub tree] If RPTR (T) NULL then Call POSTORDER (RPTR (T)) Step 4:[Process the root node] Write (DATA(T)) Step 5:[Finished] Exit

APPLICATIONS OF BINARY TREE Manipulation of arithmetic expressions Binary tree is used to represent formulas in prefix or postfix notation. For Example: Infix- A+B Syntax Analysis It deals with the use of grammars in syntax analysis or parsing. Construction and maintenance of symbol table Binary tree will formulate the algorithms that will maintain a stack implemented tree structured symbol table.

End