IIT Bombay Data Structures and Algorithms Prof. Ajit A. Diwan Prof. Ganesh Ramakrishnan Prof. Deepak B. Phatak Department of Computer Science and Engineering.

Slides:



Advertisements
Similar presentations
Chapter 12 Binary Search Trees
Advertisements

Review of Chapter 5 張啟中. Selection Trees Selection trees can merge k ordered sequences (assume in non- decreasing order) into a single sequence easily.
Binary Trees, Binary Search Trees CMPS 2133 Spring 2008.
CS 171: Introduction to Computer Science II
ITEC200 – Week08 Trees. 2 Chapter Objectives Students can: Describe the Tree abstract data type and use tree terminology such as.
CS 104 Introduction to Computer Science and Graphics Problems Data Structure & Algorithms (4) Data Structures 11/18/2008 Yang Song.
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.
Trees CMSC 433 Chapter 8.1 Nelson Padua-Perez Bill Pugh.
CHAPTER 12 Trees. 2 Tree Definition A tree is a non-linear structure, consisting of nodes and links Links: The links are represented by ordered pairs.
2/10/03Tucker, Sec Tucker, Applied Combinatorics, Sec. 3.2, Important Definitions Enumeration: Finding all of the possible paths in a rooted tree.
C o n f i d e n t i a l HOME NEXT Subject Name: Data Structure Using C Unit Title: Trees.
KNURE, Software department, Ph , N.V. Bilous Faculty of computer sciences Software department, KNURE The trees.
Trees and Tree Traversals Prof. Sin-Min Lee Department of Computer Science San Jose State University.
Joseph Lindo Trees Sir Joseph Lindo University of the Cordilleras.
CS 206 Introduction to Computer Science II 09 / 30 / 2009 Instructor: Michael Eckmann.
Trees CS212 & CS-240 D.J. Foreman. What is a Tree A tree is a finite set of one or more nodes such that: –There is a specially designated node called.
1 Trees 2 Binary trees Section Binary Trees Definition: A binary tree is a rooted tree in which no vertex has more than two children –Left and.
CISC220 Fall 2009 James Atlas Lecture 13: Trees. Skip Lists.
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,
Trees Chapter 8. 2 Tree Terminology A tree consists of a collection of elements or nodes, organized hierarchically. The node at the top of a tree is called.
Prof. Amr Goneid, AUC1 CSCE 210 Data Structures and Algorithms Prof. Amr Goneid AUC Part 4. Trees.
Discrete Structures Trees (Ch. 11)
Tree Traversals, TreeSort 20 February Expression Tree Leaves are operands Interior nodes are operators A binary tree to represent (A - B) + C.
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.
Discrete Mathematics Chapter 5 Trees.
© University of Auckland Trees – (cont.) CS 220 Data Structures & Algorithms Dr. Ian Watson.
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.
Binary Search Trees.  Understand tree terminology  Understand and implement tree traversals  Define the binary search tree property  Implement binary.
Trees Ellen Walker CPSC 201 Data Structures Hiram College.
Foundation of Computing Systems Lecture 4 Trees: Part I.
1 CMSC 341 Introduction to Trees Textbook sections:
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.
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.
IIT Bombay Data Structures and Algorithms Prof. Ajit A. Diwan Prof. Ganesh Ramakrishnan Prof. Deepak B. Phatak Department of Computer Science and Engineering.
Prof. Amr Goneid, AUC1 CSCE 210 Data Structures and Algorithms Prof. Amr Goneid AUC Part 4. Trees.
IIT Bombay Data Structures and Algorithms Prof. Ajit A. Diwan Prof. Ganesh Ramakrishnan Prof. Deepak B. Phatak Department of Computer Science and Engineering.
Data Structures and Algorithms
Data Structures and Algorithms
Data Structures and Algorithms
Data Structures and Algorithms
Trees Chapter 15.
CSCE 210 Data Structures and Algorithms
CMSC 341 Introduction to Trees 8/3/2007 CMSC 341 Tree Intro.
Recursive Objects (Part 4)
Data Structures and Algorithms
CMSC 341 Introduction to Trees.
Section 8.1 Trees.
Data Structures and Algorithms
Data Structures and Algorithms
Data Structures and Algorithms
CS223 Advanced Data Structures and Algorithms
Binary Tree Traversal Methods
Binary Tree Traversal Methods
Trees.
CSE 373, Copyright S. Tanimoto, 2002 Binary Trees -
Binary Tree Traversal Methods
Design and Analysis of Algorithms
CMSC 341 Introduction to Trees CMSC 341 Tree Intro.
CSE 373, Copyright S. Tanimoto, 2001 Binary Trees -
Binary Tree Properties & Representation
Trees.
Binary Trees.
Binary Tree Traversal Methods
Trees.
Data Structures Using C++ 2E
Chapter 11 Trees © 2011 Pearson Addison-Wesley. All rights reserved.
A Binary Tree is a tree in which each node has at most 2 children
NATURE VIEW OF A TREE leaves branches root. NATURE VIEW OF A TREE leaves branches root.
Presentation transcript:

IIT Bombay Data Structures and Algorithms Prof. Ajit A. Diwan Prof. Ganesh Ramakrishnan Prof. Deepak B. Phatak Department of Computer Science and Engineering IIT Bombay Session: Binary Trees 1Ajit A. Diwan, Ganesh Ramakrishnan, and Deepak B. Phatak, IIT Bombay

IIT Bombay Trees Trees are the simplest example of a non-sequential data type. In a sequence, every element (except the first and the last), has a unique next and previous element. In a tree, an element can have more than one ``next’’ element. Every element (except one), has exactly one previous element. Trees can give more compact representations of other types. Operations are more efficient compared to sequences. Ajit A. Diwan, Ganesh Ramakrishnan, and Deepak B. Phatak, IIT Bombay2

IIT Bombay Binary Trees Binary trees are the simplest kind of trees. Defined using a single operation, starting from an empty binary tree. Ф is a binary tree. If L, R are binary trees, then plant(n, L, R) is a binary tree. Here n denotes a node, a data type that can store values of any type. All binary trees can be obtained from Ф using the plant operation. Ajit A. Diwan, Ganesh Ramakrishnan, and Deepak B. Phatak, IIT Bombay3

IIT Bombay Definitions In the binary tree T obtained using the plant operation T = plant(n, L, R) n is called the root node of the tree T. L is the left subtree of T (also of node n) and R is the right subtree of T ( and node n). If L (or R) is not empty, but is of the form plant(m, L 1, L 2 ), then m is called the left (right) child of n, and n is the parent of m. T is said to contain the node n, and all nodes contained in L and R. Note that we assume that the node n is not contained in L or R. Ajit A. Diwan, Ganesh Ramakrishnan, and Deepak B. Phatak, IIT Bombay4

IIT Bombay Operations on Binary Trees Operations on trees defined in a way similar to numbers and sequences. Define for the empty tree. Assuming it is defined for trees L and R, define it for the tree plant(n, L, R). This defines the operation for all binary trees. Ajit A. Diwan, Ganesh Ramakrishnan, and Deepak B. Phatak, IIT Bombay5

IIT Bombay Height and Size height(Ф) = 0 height(plant(n, L, R)) = next(max(height(L), height(R))) Here max is the maximum function on numbers defined by max(0, n) = n for all n max(next(m), n) = next(m) if max(m, n) = m = n otherwise size(Ф) = 0 size(plant(n, L, R)) = next(add(size(L), size(R))) Ajit A. Diwan, Ganesh Ramakrishnan, and Deepak B. Phatak, IIT Bombay6

IIT Bombay Equality When are two binary trees equal? equal(Ф, Ф) = true equal(Ф, plant(m, L 1, R1 )) = false equal(plant(n, L, R), Ф) = false equal(plant(n, L, R), plant(m, L 1, R 1 )) = equal(L, L 1 ) and equal(R, R 1 ) Note that the nodes play no role in the definition. Ajit A. Diwan, Ganesh Ramakrishnan, and Deepak B. Phatak, IIT Bombay7

IIT Bombay Special Binary Trees Some special types of binary trees are particularly useful. Full binary trees full(Ф) = true full(plant(n, L, R)) = false if height(L) ≠ height(R) = full(L) and full(R) otherwise A full binary tree of height h has size For every node, the left and right subtree have the same size. Ajit A. Diwan, Ganesh Ramakrishnan, and Deepak B. Phatak, IIT Bombay8

IIT Bombay Complete Binary Trees complete(Ф) = true complete(plant(n, L, R)) = full(L) and complete(R) if height(L) = height(R) = complete(L) and full(R) if height(L) = next(height(R)) = false otherwise Ajit A. Diwan, Ganesh Ramakrishnan, and Deepak B. Phatak, IIT Bombay9

IIT Bombay Tree Traversals Many times we need a list of all the nodes in a tree. Tree traversal is a way of constructing a sequence of nodes in the tree. Different ways of traversing give sequences with different properties. These sequences give an alternative way of representing the tree. Ajit A. Diwan, Ganesh Ramakrishnan, and Deepak B. Phatak, IIT Bombay10

IIT Bombay Inorder, Preorder, Postorder Traversal inorder(Ф) = Ф, preorder(Ф) = Ф, postorder(Ф) = Ф inorder(plant(n, L, R)) = concatenate(concatenate(inorder(L), push(n,Ф)), inorder(R)) preoder(plant(n, L, R)) = concatenate(concatenate(push(n,Ф), preorder(L)), preorder(R)) postorder(plant(n, L, R)) = concatenate(concatenate(postorder(L), postorder(R)), push(n,Ф)) Ajit A. Diwan, Ganesh Ramakrishnan, and Deepak B. Phatak, IIT Bombay11

IIT Bombay Exercises Let T(n) denote the number of binary trees of size n. Show how you can compute T(n) using a recurrence relation. Suppose you are given the sequence of nodes in a binary tree obtained by inorder and preorder traversal. Show that these two sequences uniquely represent the binary tree. Two nodes in a binary tree are said to be siblings if they have the same parent. Write a function to count the number of pairs of siblings in a binary tree. Ajit A. Diwan, Ganesh Ramakrishnan, and Deepak B. Phatak, IIT Bombay12