Lecture - 11 on Data Structures. Prepared by, Jesmin Akhter, Lecturer, IIT,JU Threaded Trees Binary trees have a lot of wasted space: the leaf nodes each.

Slides:



Advertisements
Similar presentations
Chapter 12 Binary Search Trees
Advertisements

Trees Types and Operations
SUNY Oneonta Data Structures and Algorithms Visualization Teaching Materials Generation Group Binary Search Tree A running demonstration of binary search.
Binary Trees, Binary Search Trees CMPS 2133 Spring 2008.
Binary Trees, Binary Search Trees COMP171 Fall 2006.
CS 171: Introduction to Computer Science II
DictionaryADT and Trees. Overview What is the DictionaryADT? What are trees? Implementing DictionaryADT with binary trees Balanced trees DictionaryADT.
Kymberly Fergusson CSE1303 Part A Data Structures and Algorithms Summer Semester 2003 Lecture A12 – Binary Trees.
CS 104 Introduction to Computer Science and Graphics Problems Data Structure & Algorithms (4) Data Structures 11/18/2008 Yang Song.
BST Data Structure A BST node contains: A BST contains
Kymberly Fergusson CSE1303 Part A Data Structures and Algorithms Summer Semester 2003 Lecture A12 – Binary Trees.
B+ Trees Similar to B trees, with a few slight differences
1 abstract containers hierarchical (1 to many) graph (many to many) first ith last sequence/linear (1 to 1) set.
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
By : Budi Arifitama Pertemuan ke Objectives Upon completion you will be able to: Create and implement binary search trees Understand the operation.
Types of Binary Trees Introduction. Types of Binary Trees There are several types of binary trees possible each with its own properties. Few important.
TREES A tree's a tree. How many more do you need to look at? --Ronald Reagan.
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:
Review Binary Tree Binary Tree Representation Array Representation Link List Representation Operations on Binary Trees Traversing Binary Trees Pre-Order.
Lecture 17 Non-Linear data structures Richard Gesick.
CS Data Structures Chapter 15 Trees Mehmet H Gunes
1 Lecture 11 POLYNOMIALS and Tree sort 2 INTRODUCTION EVALUATING POLYNOMIAL FUNCTIONS Horner’s method Permutation Tree sort.
CISC220 Fall 2009 James Atlas Lecture 13: Trees. Skip Lists.
1 Trees A tree is a data structure used to represent different kinds of data and help solve a number of algorithmic problems Game trees (i.e., chess ),
Binary Trees 2 Overview Trees. Terminology. Traversal of Binary Trees. Expression Trees. Binary Search Trees.
Tree (new ADT) Terminology:  A tree is a collection of elements (nodes)  Each node may have 0 or more successors (called children)  How many does a.
Binary Trees, Binary Search Trees RIZWAN REHMAN CENTRE FOR COMPUTER STUDIES DIBRUGARH UNIVERSITY.
1 Chapter 10 Trees. 2 Definition of Tree A tree is a set of linked nodes, such that there is one and only one path from a unique node (called the root.
Trees A tree is a set of nodes which are connected by branches to other nodes in a 'tree-like' structure. There is a special node called the root from.
Computer Science: A Structured Programming Approach Using C Trees Trees are used extensively in computer science to represent algebraic formulas;
 Trees Data Structures Trees Data Structures  Trees Trees  Binary Search Trees Binary Search Trees  Binary Tree Implementation Binary Tree Implementation.
Binary trees -2 Chapter Threaded trees (depth first) Binary trees have a lot of wasted space: the leaf nodes each have 2 null pointers We can.
Preview  Graph  Tree Binary Tree Binary Search Tree Binary Search Tree Property Binary Search Tree functions  In-order walk  Pre-order walk  Post-order.
Topics Definition and Application of Binary Trees Binary Search Tree Operations.
Tree Traversals, TreeSort 20 February Expression Tree Leaves are operands Interior nodes are operators A binary tree to represent (A - B) + C.
Lecture - 10 on Data Structures. 6:05:57 PM Prepared by, Jesmin Akhter, Lecturer, IIT,JU.
Binary Search Trees Lecture 5 1. Binary search tree sort 2.
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.
1 Binary Trees and Binary Search Trees Based on Dale & Co: Object-Oriented Data Structures using C++ (graphics)
1 Lecture 21: Binary Search Tree delete etc. operations Lecturer: Santokh Singh CompSci 105 SS 2005 Principles of Computer Science.
ADT Binary Search Tree Ellen Walker CPSC 201 Data Structures Hiram College.
Binary Search Trees (BST)
Rooted Tree a b d ef i j g h c k root parent node (self) child descendent leaf (no children) e, i, k, g, h are leaves internal node (not a leaf) sibling.
Data Structures: A Pseudocode Approach with C, Second Edition 1 Chapter 7 Objectives Create and implement binary search trees Understand the operation.
Foundation of Computing Systems Lecture 4 Trees: Part I.
TREES General trees Binary trees Binary search trees AVL trees Balanced and Threaded trees.
Fundamentals of Algorithms MCS - 2 Lecture # 17. Binary Search Trees.
SUYASH BHARDWAJ FACULTY OF ENGINEERING AND TECHNOLOGY GURUKUL KANGRI VISHWAVIDYALAYA, HARIDWAR.
Lecture on Data Structures(Trees). Prepared by, Jesmin Akhter, Lecturer, IIT,JU 2 Properties of Heaps ◈ Heaps are binary trees that are ordered.
Binary Search Trees Chapter 7 Objectives
Binary Trees and Binary Search Trees
Binary Search Tree (BST)
Binary Search Tree Chapter 10.
Section 8.1 Trees.
Binary Trees, Binary Search Trees
Binary Search Trees.
Threaded Trees Binary trees have a lot of wasted space: the leaf nodes each have 2 null pointers We can use these pointers to help us in inorder traversals.
TREES General trees Binary trees Binary search trees AVL trees
Binary Trees.
Binary Trees.
Chapter 12: Binary Search Trees
Binary Search Trees.
Binary Trees, Binary Search Trees
Binary Search Trees Chapter 7 Objectives
Chapter 20: Binary Trees.
Non-Linear data structures
Binary Trees, Binary Search Trees
Chapter 11 Trees © 2011 Pearson Addison-Wesley. All rights reserved.
Amir Kamil 8/8/02 1 B+ Trees Similar to B trees, with a few slight differences All data is stored at the leaf nodes (leaf pages); all other nodes (index.
Presentation transcript:

Lecture - 11 on Data Structures

Prepared by, Jesmin Akhter, Lecturer, IIT,JU Threaded Trees Binary trees have a lot of wasted space: the leaf nodes each have 2 null pointers We can use these pointers to help us in inorder traversals We have the pointers reference the next node in an inorder traversal; called threads We need to know if a pointer is an actual link or a thread, so we keep a boolean for each pointer

Prepared by, Jesmin Akhter, Lecturer, IIT,JU Threaded Tree Example

Prepared by, Jesmin Akhter, Lecturer, IIT,JU Threaded Tree Traversal We start at the leftmost node in the tree, print it, and follow its right thread If we follow a thread to the right, we output the node and continue to its right If we follow a link to the right, we go to the leftmost node, print it, and continue

Prepared by, Jesmin Akhter, Lecturer, IIT,JU Threaded Tree Traversal Start at leftmost node, print it Output 1

Prepared by, Jesmin Akhter, Lecturer, IIT,JU Threaded Tree Traversal Follow thread to right, print node Output 1 3

Prepared by, Jesmin Akhter, Lecturer, IIT,JU Threaded Tree Traversal Follow link to right, go to leftmost node and print Output 1 3 5

Prepared by, Jesmin Akhter, Lecturer, IIT,JU Threaded Tree Traversal Follow thread to right, print node Output

Prepared by, Jesmin Akhter, Lecturer, IIT,JU Threaded Tree Traversal Follow link to right, go to leftmost node and print Output

Prepared by, Jesmin Akhter, Lecturer, IIT,JU Threaded Tree Traversal Follow thread to right, print node Output

Prepared by, Jesmin Akhter, Lecturer, IIT,JU Threaded Tree Traversal Follow link to right, go to leftmost node and print Output

Prepared by, Jesmin Akhter, Lecturer, IIT,JU Threaded Tree Traversal Follow thread to right, print node Output

Prepared by, Jesmin Akhter, Lecturer, IIT,JU Threaded Tree Traversal Follow link to right, go to leftmost node and print Output

Prepared by, Jesmin Akhter, Lecturer, IIT,JU Threaded Tree Modification We’re still wasting pointers, since half of our leafs’ pointers are still null We can add threads to the previous node in an inorder traversal as well, which we can use to traverse the tree backwards or even to do postorder traversals

Prepared by, Jesmin Akhter, Lecturer, IIT,JU Threaded Tree Modification

Prepared by, Jesmin Akhter, Lecturer, IIT,JU 16 Binary Search Trees

Prepared by, Jesmin Akhter, Lecturer, IIT,JU (Con..)

18 (Con..) Binary Search Tree (Con..) A Binary Search Tree is a binary tree with the following Basic properties: All items in the left subtree are less than the root. All items in the right subtree are greater or equal to the root. Each subtree is itself a binary search tree.

Prepared by, Jesmin Akhter, Lecturer, IIT,JU (Con..)

Figure 1. Inserting a new node into a BST

Prepared by, Jesmin Akhter, Lecturer, IIT,JU A BST after nodes with values of 20, 50, 90, 150, 175, and 200 have been added

Prepared by, Jesmin Akhter, Lecturer, IIT,JU Inserting a new key in a BST How to insert a new key? The same procedure used for search also applies: Determine the location by searching. Search will fail. Insert new key where the search failed. Example: Insert 4?

Prepared by, Jesmin Akhter, Lecturer, IIT,JU Building a BST Build a BST from a sequence of nodes read one a time Example: Inserting C A B L M (in this order!) 1) Insert C C 2) Insert A C A

Prepared by, Jesmin Akhter, Lecturer, IIT,JU Building a BST 3) Insert B C A B 4) Insert L 5) Insert M C A B L M C A B L

Prepared by, Jesmin Akhter, Lecturer, IIT,JU Building a BST Is there a unique BST for letters A B C L M ? NO! Different input sequences result in different trees C A B L M A B C L M Inserting: A B C L M Inserting: C A B L M

Prepared by, Jesmin Akhter, Lecturer, IIT,JU Sorting with a BST Given a BST can you output its keys in sorted order? Visit keys with Inorder: - visit left - print root - visit right How can you find the minimum? How can you find the maximum? C A B L M Example: A B C L M Inorder visit prints:

Prepared by, Jesmin Akhter, Lecturer, IIT,JU 28 Preorder Traversal

Prepared by, Jesmin Akhter, Lecturer, IIT,JU 29 Postorder Traversal

Prepared by, Jesmin Akhter, Lecturer, IIT,JU 30 Inorder Traversal Inorder traversal of a binary search tree produces a sequenced list

Prepared by, Jesmin Akhter, Lecturer, IIT,JU 31 Right-Node-Left Traversal Right-node-left traversal of a binary search tree produces a descending sequence

Prepared by, Jesmin Akhter, Lecturer, IIT,JU

Deleting from a BST To delete node with key x first you need to search for it. Once found, apply one of the following three cases CASE A: x is a leaf x delete xBST property maintained q r q r p p

Prepared by, Jesmin Akhter, Lecturer, IIT,JU Deleting from a BST cont. Case B: x is interior with only one subtree L x q r delete x L q r BST property maintained

Prepared by, Jesmin Akhter, Lecturer, IIT,JU Deleting from a BST cont. Case C: x is interior with two subtrees W x q r delete x Z t s r W q r delete x sZ r BST property maintained t

Prepared by, Jesmin Akhter, Lecturer, IIT,JU Deleting from a BST cont. Case C cont: … or you can also do it like this W q r Z s r t q < x < r  Q is smaller than the smaller element in Z  R is larger than the largest element in W

Prepared by, Jesmin Akhter, Lecturer, IIT,JU

Binary Search Trees Example 1: Find 47 < 71 > 35 < 57 > 45 < 49 Found 63-item list Example 2: Find 112 > 71 > 102 < 120 > 111 < 117 < 116 Not found

Prepared by, Jesmin Akhter, Lecturer, IIT,JU Insertions and Deletions in Binary Search Trees Example 1: Insert 48 < 71 > 35 < 57 > 45 < 49 Example 2: Delete 116 > 71 > 102 < 120 > 111 < 117 > Deleted