Data Structures and Algorithms

Slides:



Advertisements
Similar presentations
SUNY Oneonta Data Structures and Algorithms Visualization Teaching Materials Generation Group Binary Search Tree A running demonstration of binary search.
Advertisements

Computer Science C++ High School Level By Guillermo Moreno.
Parallel Prefix Computation Advanced Algorithms & Data Structures Lecture Theme 14 Prof. Dr. Th. Ottmann Summer Semester 2006.
Department of Computer Science University of Maryland, College Park
CS2420: Lecture 13 Vladimir Kulyukin Computer Science Department Utah State University.
C++ Programming: Program Design Including Data Structures, Third Edition Chapter 19: Heap Sort.
CS 104 Introduction to Computer Science and Graphics Problems Data Structure & Algorithms (4) Data Structures 11/18/2008 Yang Song.
Basic Algorithms on Trees. If the node v is the root, then the depth of the node v is 0. Otherwise, the depth of the node v is one plus the depth of.
Advanced Topics in Algorithms and Data Structures Page 1 An overview of lecture 3 A simple parallel algorithm for computing parallel prefix. A parallel.
Chapter 9 contd. Binary Search Trees Anshuman Razdan Div of Computing Studies
1 Section 9.2 Tree Applications. 2 Binary Search Trees Goal is implementation of an efficient searching algorithm Binary Search Tree: –binary tree in.
Binary Search Trees Chapter 7 Objectives
Joseph Lindo Trees Sir Joseph Lindo University of the Cordilleras.
By : Budi Arifitama Pertemuan ke Objectives Upon completion you will be able to: Create and implement binary search trees Understand the operation.
Data Structures - CSCI 102 Binary Tree In binary trees, each Node can point to two other Nodes and looks something like this: template class BTNode { public:
Min Chen School of Computer Science and Engineering Seoul National University Data Structure: Chapter 7.
Computer Science: A Structured Programming Approach Using C Trees Trees are used extensively in computer science to represent algebraic formulas;
Computer Science 112 Fundamentals of Programming II Introduction to Trees.
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.
1 Chapter 7 Objectives Upon completion you will be able to: Create and implement binary search trees Understand the operation of the binary search tree.
1Computer Sciences. 2 HEAP SORT TUTORIAL 4 Objective O(n lg n) worst case like merge sort. Sorts in place like insertion sort. A heap can be stored as.
Foundation of Computing Systems Lecture 4 Trees: Part I.
Hello Everyone!!! 1. Tree And Graphs 2 Features of Trees  Tree Nodes Each node have 0 or more children A node have must one parent  Binary tree Tree.
Question 4 Tutorial 8. Part A Insert 20, 10, 15, 5,7, 30, 25, 18, 37, 12 and 40 in sequence into an empty binary tree
TREES From root to leaf. Trees  A tree is a non-linear collection  The elements are in a hierarchical arrangement  The elements are not accessible.
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 Data Structures and Algorithms Prof. Ajit A. Diwan Prof. Ganesh Ramakrishnan Prof. Deepak B. Phatak Department of Computer Science and Engineering.
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 Data Structures and Algorithms Prof. Ajit A. Diwan Prof. Ganesh Ramakrishnan Prof. Deepak B. Phatak Department of Computer Science and Engineering.
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
Binary Search Trees Chapter 7 Objectives
Data Structures and Algorithms
Partially Ordered Data ,Heap,Binary Heap
Data Structures and Algorithms
Data Structures and Algorithms
Balanced Search Trees 2-3 Trees AVL Trees Red-Black Trees
Data Structures and Algorithms
Heap Chapter 9 Objectives Define and implement heap structures
Fundamentals of Programming II Introduction to Trees
Recursive Objects (Part 4)
Data Structures and Algorithms
Trees ---- Soujanya.
Data Structures and Algorithms
CISC220 Fall 2009 James Atlas Lecture 13: Binary Trees.
Binary Search Tree (BST)
Data Structures and Algorithms
AVL DEFINITION An AVL tree is a binary search tree in which the balance factor of every node, which is defined as the difference between the heights of.
Tree.
Lecture 22 Binary Search Trees Chapter 10 of textbook
Section 8.1 Trees.
Lecture 18. Basics and types of Trees
Data Structures and Algorithms
Data Structures and Algorithms
Binary Search Tree In order Pre order Post order Search Insertion
Binary Tree Applications
Data Structures and Algorithms
Alyce Brady CS 470: Data Structures CS 510: Computer Algorithms
Alyce Brady CS 470: Data Structures CS 510: Computer Algorithms
Abstract Data Structures
Binary Tree Traversals
Lecture 36 Section 12.2 Mon, Apr 23, 2007
Drawing Tree wijanarto.
Representing binary trees with lists
Chapter 20: Binary Trees.
BINARY HEAP Prof ajitkumar shitole Assistant Professor Department of computer engineering Hope Foundation’s International Institute of Information.
Binary Search Tree (BST)
Data Structures Using C++ 2E
Data Structures – Binary Tree
Presentation transcript:

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

Binary Tree 60 40 20 25 23 90 80 95 85 Nodes Parent Left Child Right Child Can have either left, right, or both Leaf Node Does not have left and right child 60 40 20 25 23 90 80 95 85 Ajit A. Diwan, Ganesh Ramakrishnan, and Deepak B. Phatak, IIT Bombay

Represented Internally as Each node in the tree is represented with Data: The element leftIndex: Index of the left child rightIndex: Index of the right child Data Left Index Right Index Data Left Index Right Index Data Left Index Right Index Ajit A. Diwan, Ganesh Ramakrishnan, and Deepak B. Phatak, IIT Bombay

Represented Internally as Data of the left child is less than the data of the parent Data of the right child is greater than the data of the parent Value of -1 in leftIndex or rightIndex denotes that it does not have a left or right child respectively At Index: 10 1 2 10 8 12 1 2 At Index: 1 At Index: 2 8 -1 12 -1 Ajit A. Diwan, Ganesh Ramakrishnan, and Deepak B. Phatak, IIT Bombay

Basic Operations Making a node (Root) Inserting Insert Left node Insert Right node Traversing Pre-Order In-Order Post-Order Ajit A. Diwan, Ganesh Ramakrishnan, and Deepak B. Phatak, IIT Bombay

Making a Node (60) 60 Initialized only when the tree is empty Make node with data as ‘60’ Data: 60 Left Index: -1 Right Index: -1 60 Ajit A. Diwan, Ganesh Ramakrishnan, and Deepak B. Phatak, IIT Bombay

Insert (40) ‘40 < 60’, and there is no left child of ‘60’, so insert ‘40’ as left child of ‘60’ Left Index of 60: 1 Data: 40 Left Index: -1 Right Index: -1 60 1 40 Ajit A. Diwan, Ganesh Ramakrishnan, and Deepak B. Phatak, IIT Bombay

Insert (20) 60 40 20 Traverse from root ‘20 < 60’, ‘20 < 40’, and there is no left child of ‘40’, so insert ‘20’ as left child of ‘40’ Left Index of 40: 2 Data: 20 Left Index: -1 Right Index: -1 60 1 40 2 20 Ajit A. Diwan, Ganesh Ramakrishnan, and Deepak B. Phatak, IIT Bombay

Insert (25) 60 40 20 25 Traverse from root ‘25 < 60’, ‘25 < 40’, Now, ‘25 > 20’, and there is no right child of ‘20’, so insert ‘25’ as right child of ‘20’ Right Index of 20: 3 Data: 25 Left Index: -1 Right Index: -1 60 1 40 2 20 3 25 Ajit A. Diwan, Ganesh Ramakrishnan, and Deepak B. Phatak, IIT Bombay

Insert (90) 60 40 90 20 25 Traverse from root ‘90 > 60’, and there is no right child of ‘60’, so insert ‘90’ as right child of ‘60’ Right Index of 60: 4 Data: 90 Left Index: -1 Right Index: -1 60 1 4 40 90 2 20 3 25 Ajit A. Diwan, Ganesh Ramakrishnan, and Deepak B. Phatak, IIT Bombay

Insert (23) 60 40 90 20 25 23 Traverse from root ‘23 < 60’, ‘23 < 40’, ‘23 > 20’, but right child (25) is present, so continue comparing. ‘23 < 25’, and there is no left child of ‘25’, so insert ‘23’ as left child of ‘25’ Left Index of 25: 5 Data: 23 Left Index: -1 Right Index: -1 60 1 4 40 90 2 20 3 25 5 23 Ajit A. Diwan, Ganesh Ramakrishnan, and Deepak B. Phatak, IIT Bombay

Insert (95) 60 40 90 20 95 25 23 Traverse from root ‘95 > 60’, ‘95 > 90’, and there is no right child of ‘90’, so insert ‘95’ as right child of ‘90’ Right Index of 90: 6 Data: 95 Left Index: -1 Right Index: -1 60 1 4 40 90 2 6 20 95 3 25 5 23 Ajit A. Diwan, Ganesh Ramakrishnan, and Deepak B. Phatak, IIT Bombay

Insert (80) 60 40 90 20 80 95 25 23 Traverse from root ‘80 > 60’, But, ‘80 < 90’, and there is no left child of ‘90’, so insert ‘80’ as left child of ‘90’ Left Index of 90: 7 Data: 80 Left Index: -1 Right Index: -1 60 1 4 40 90 2 7 6 20 80 95 3 25 5 23 Ajit A. Diwan, Ganesh Ramakrishnan, and Deepak B. Phatak, IIT Bombay

Insert (85) 60 40 90 20 80 95 25 85 23 Traverse from root ‘85 > 60’, ‘85 < 90’, but left child of ‘90’ is present, so continue ‘85 > 80’ and there is no right child of ‘80’, so insert ‘85’ as right child of ‘80’ Right Index of 80: 8 Data: 85 Left Index: -1 Right Index: -1 60 1 4 40 90 2 7 6 20 80 95 3 8 25 85 5 23 Ajit A. Diwan, Ganesh Ramakrishnan, and Deepak B. Phatak, IIT Bombay

Thank you Ajit A. Diwan, Ganesh Ramakrishnan, and Deepak B. Phatak, IIT Bombay