Binary Search Trees.

Slides:



Advertisements
Similar presentations
Chapter 7. Binary Search Trees
Advertisements

Trees Types and Operations
S. Sudarshan Based partly on material from Fawzi Emad & Chau-Wen Tseng
Binary Trees Chapter 6. Linked Lists Suck By now you realize that the title to this slide is true… By now you realize that the title to this slide is.
Binary Trees, Binary Search Trees CMPS 2133 Spring 2008.
Binary Trees, Binary Search Trees COMP171 Fall 2006.
DictionaryADT and Trees. Overview What is the DictionaryADT? What are trees? Implementing DictionaryADT with binary trees Balanced trees DictionaryADT.
© 2004 Goodrich, Tamassia Binary Search Trees   
1 Trees. 2 Outline –Tree Structures –Tree Node Level and Path Length –Binary Tree Definition –Binary Tree Nodes –Binary Search Trees.
Binary Search Trees1 Part-F1 Binary Search Trees   
BST Data Structure A BST node contains: A BST contains
A Binary Search Tree Implementation Chapter Chapter Contents Getting Started An Interface for the Binary Search Tree Duplicate Entries Beginning.
A Binary Search Tree Implementation Chapter 25 Copyright ©2012 by Pearson Education, Inc. All rights reserved.
A Binary Search Tree Implementation Chapter 27 Slides by Steve Armstrong LeTourneau University Longview, TX  2007,  Prentice Hall.
Binary Search Trees CSE, POSTECH. Search Trees Search trees are ideal for implementing dictionaries – Similar or better performance than skip lists and.
Binary Search Trees Chapter 7 Objectives
1 BST Trees A binary search tree is a binary tree in which every node satisfies the following: the key of every node in the left subtree is.
By : Budi Arifitama Pertemuan ke Objectives Upon completion you will be able to: Create and implement binary search trees Understand the operation.
Properties: -Each node has a value -The left subtree contains only values less than the parent node’s value -The right subtree contains only values greater.
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:
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 ),
Data Structure & Algorithm II.  Delete-min  Building a heap in O(n) time  Heap Sort.
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.
Analysis of Red-Black Tree Because of the rules of the Red-Black tree, its height is at most 2log(N + 1). Meaning that it is a balanced tree Time Analysis:
Binary Search Trees (10.1) CSE 2011 Winter November 2015.
 Trees Data Structures Trees Data Structures  Trees Trees  Binary Search Trees Binary Search Trees  Binary Tree Implementation Binary Tree Implementation.
Binary Search Trees   . 2 Binary Search Tree Properties A binary search tree is a binary tree storing keys (or key-element pairs) at its.
Preview  Graph  Tree Binary Tree Binary Search Tree Binary Search Tree Property Binary Search Tree functions  In-order walk  Pre-order walk  Post-order.
Tree Implementations Chapter 16 Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013.
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.
Binary Search Trees (BSTs) 18 February Binary Search Tree (BST) An important special kind of binary tree is the BST Each node stores some information.
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.
Binary Search Trees (BST)
Data Structures: A Pseudocode Approach with C, Second Edition 1 Chapter 7 Objectives Create and implement binary search trees Understand the operation.
BINARY TREES Objectives Define trees as data structures Define the terms associated with trees Discuss tree traversal algorithms Discuss a binary.
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
A Binary Search Tree Implementation Chapter 25 © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. Data Structures and Abstractions.
BSTs, AVL Trees and Heaps Ezgi Shenqi Bran. What to know about Trees? Height of a tree Length of the longest path from root to a leaf Height of an empty.
Binary Search Trees Chapter 7 Objectives
Binary Search Trees < > = © 2010 Goodrich, Tamassia
Binary Search Trees < > =
AA Trees.
Trees Chapter 11 (continued)
Binary Search Trees < > =
Trees Chapter 11 (continued)
A Binary Search Tree Implementation
Week 6 - Wednesday CS221.
Binary Search Tree (BST)
Binary Search Trees (10.1) CSE 2011 Winter August 2018.
Chapter 10 Search Trees 10.1 Binary Search Trees Search Trees
Binary Search Tree Chapter 10.
Chapter 10.1 Binary Search Trees
Lecture 22 Binary Search Trees Chapter 10 of textbook
Chapter 16 Tree Implementations
Binary Trees, Binary Search Trees
Binary Tree and General Tree
Chapter 22 : Binary Trees, AVL Trees, and Priority Queues
Binary Search Trees.
Map interface Empty() - return true if the map is empty; else return false Size() - return the number of elements in the map Find(key) - if there is an.
Binary Search Trees (10.1) CSE 2011 Winter November 2018.
Binary Search Trees < > = © 2010 Goodrich, Tamassia
Binary Search Trees < > =
Find in a linked list? first last 7  4  3  8 NULL
Chapter 16 Tree Implementations
Binary Search Trees A special case of a Binary Tree
Binary Search Trees < > =
Binary Trees, Binary Search Trees
Binary Search Trees < > =
Binary Trees, Binary Search Trees
Presentation transcript:

Binary Search Trees

Binary search trees A binary search tree is a binary tree where all elements in the left subtree are less than elements in the right subtree 2

Binary search trees A binary search tree is a binary tree where all elements in the left subtree are less than elements in the right subtree As we saw earlier, inorder traversal is a b c d e f g h i j which is in sorted order So this is a binary search tree 3

Binary search trees A binary search tree is a binary tree whose inorder traversal is in sorted order As we saw earlier, inorder traversal is a b c d e f g h i j which is in sorted order So this is a binary search tree There are many more (binary) search trees that have inorder traversal a b c d e f g h i j 4

Binary search trees A binary search tree is a binary tree whose inorder traversal is in sorted order As we saw earlier, inorder traversal is a b c d e f g h i j which is in sorted order So this is a binary search tree All entries are unique! Typical use … representing a set 5

demo http://webdiis.unizar.es/asignaturas/EDA/AVLTree/avltree.html

Implementing search in a binary search tree Can implement binary search in O(log n) time on average Takes longer if the tree is badly balanced For every node X, value in all nodes in left subtree of X are less than value in X, and value of all nodes in right subtree are greater than value in X Algorithm is simple: if x < node then search left subtree if x > node then search right subtree When the tree is balanced the path length to the leaves is log(n) 8

Find 17

Find 17

Find 17

Find 17

Find 17

Find 17 Fails to find 17

Insert New Node Inserting a new node This method works whether we are using the tree to implement a set, sequence etc. as long as it is an injective binary search tree. Inserting a new node in general we must: Add to bottom of tree at all times (add a leaf) Keep the search tree property (left less than right) 15

Insert 40

Insert 40

Insert 40

Insert 40

Insert 40

Another illustration of insertion …

Adding a node with value x = 5. (Example) Insert New Node Adding a node with value x = 5. (Example) 6 3 2 4 23

Adding a node with value x = 5. (Example) Insert New Node Adding a node with value x = 5. (Example) 6 3 2 4 Compare 5 with node: 5 > 3 so go right 24

Adding a node with value x = 5. (Example) Insert New Node Adding a node with value x = 5. (Example) 6 3 2 4 Compare 5 with node: 5 < 6 so go left 25

Adding a node with value x = 5. (Example) Insert New Node Adding a node with value x = 5. (Example) 6 3 2 4 Compare 5 with node: 5 > 4 so go right 26

Adding a node with value x = 5. (Example) Insert New Node Adding a node with value x = 5. (Example) 3 2 6 4 Can’t go right as that is null Insert node right of here, as a leaf 27

Adding a node with value x = 5. (Example) Insert New Node Adding a node with value x = 5. (Example) 3 2 6 4 5 Can’t go right as that is null Insert node right of here, as a leaf 28

Why bother?

n=3, log(n+1)-1 = 1 ≤ height ≤ n-1

n=7, log(n+1)-1 = 2 ≤ height ≤ n-1

n=15, log(n+1)-1 = 3 ≤ height ≤ n-1

n=31, log(n+1)-1 = 4 ≤ height ≤ n-1

n=63, log(n+1)-1 = 5 ≤ height ≤ n-1

n=127, log(n+1)-1 = 6 ≤ height ≤ n-1

n=255, log(n+1)-1 = 7 ≤ height ≤ n-1

And this is crucial height (h) log2(n+1)-1 ≤ h ≤ n-1 If we get it right we can access data in logarithmic time!

Log to the base 2 of … 2 = 1 4 = 2 8 = 3 16 = 4 … 1024 = 10

Log to the base 2 of … 2 = 1 4 = 2 8 = 3 16 = 4 … 1024 = 10 Brilliant!

java implementation

Node

Node

Node

Node Default constructor

Node parameterised constructor note use of this.

Node getters

Node setters

Node predicates

Node For printing

The BSTree class BinarySearchTree (BSTree)

Your mission, should you choose to accept it …

Implement insert, find and delete …

BSTree

BSTree A binary search tree has a root where the root is a node It also has a size, where size is the number of nodes in the tree

BSTree Default constructor, an empty tree with an empty root

BSTree Get the root Test if tree is empty Get the size of the tree

BSTree Insert string s into the tree

BSTree Insert string s into the tree

BSTree Insert string s into the subtree rooted on the current node

BSTree If s is in the left subtree …

BSTree If s is in the left subtree and we need to create a new node

BSTree If s is in the right subtree …

BSTree If s is in the right subtree and we need to create a new node

finding a node

finding a node BSTree

finding a node BSTree The string s is present in the tree if the tree isn’t empty and we can find a node that contains s

finding a node BSTree find string s in the subtree rooted on the current node

finding a node BSTree If s is equal to the data in the current node then return the current node as a result.

finding a node BSTree If s is less than the data in the current node and the current node has a left child then try and find s in the subtree rooted on the left child

finding a node BSTree If s is greater than the data in the current node and the current node has a right child then try and find s in the subtree rooted on the right child

finding a node BSTree s is less than the current node and it has no left child or s is greater than the current node and it has no right child … deliver null!

Deletion of a node

Deletion of a node 16 11 30 7 13 20 40 18 25

Delete 7 Deletion of a node 16 11 30 7 13 20 40 18 25

Delete 7 Deletion of a node 16 11 30 7 13 20 40 18 25

Delete 7 Deletion of a node 16 11 30 13 20 40 18 25

Delete 11 Deletion of a node 16 11 30 13 20 40 18 25

Delete 11 Deletion of a node 16 11 30 13 20 40 18 25

Delete 11 Deletion of a node 16 11 30 13 20 40 Copy contents 18 25

Delete 11 Deletion of a node 16 13 30 13 20 40 Copy contents 18 25

Delete 11 Deletion of a node 16 13 30 13 20 40 Copy contents 18 25

Delete 11 Deletion of a node 16 13 30 13 20 40 Delete leaf 18 25

Delete 11 Deletion of a node 16 13 30 20 40 18 25

Delete 16 Deletion of a node 16 13 30 20 40 18 25

Delete 16 Deletion of a node 16 13 30 20 40 18 25

Deletion of a node Delete 16 16 13 30 20 40 18 25 Find the smallest node in the right subtree

Deletion of a node Delete 16 16 13 30 20 40 18 25 Find the smallest node in the right subtree

Deletion of a node Delete 16 16 13 30 20 40 18 25 Find the smallest node in the right subtree

Delete 16 Deletion of a node 16 13 30 20 40 18 25 Copy contents

Delete 16 Deletion of a node 18 13 30 20 40 18 25 Copy contents

Delete 16 Deletion of a node 18 13 30 20 40 18 25 Delete leaf

Delete 16 Deletion of a node 18 13 30 20 40 25 Delete leaf

Deletion of a node Delete 16 18 13 30 20 40 25 This process maintains the inorder property

There is a symmetric equivalent: find largest node in left branch … Delete 16 Deletion of a node 18 13 30 20 40 25 This process maintains the inorder property There is a symmetric equivalent: find largest node in left branch …

Deletion of a node Delete 16 16 13 30 20 40 18 25 Find the largest node in the left subtree

Deletion of a node Delete 16 16 13 30 20 40 18 25 Find the largest node in the left subtree

Delete 16 Deletion of a node 13 13 30 20 40 18 25 Copy contents

Delete 16 Deletion of a node 13 13 30 20 40 18 25 Delete leaf

Delete 16 Deletion of a node 13 30 20 40 18 25 Delete leaf

Deletion of a node Delete 16 40 13 20 30 25 18 Therefore we have 2 options resulting in two different trees, both valid 40 18 20 30 13 25

Well baby, is there an algorithm for this? Deletion of a node Well baby, is there an algorithm for this?

Deletion of a node

Deletion of a non-internal node (a node with less than 2 children) Non-internal – less than 2 children 6 cases to consider

Deletion of a non-internal node Non-internal – less than 2 children Actually only 4 cases to consider

Deletion of a non-internal node Assuming we have already found the node to delete

Deletion of a non-internal node subsumed Assuming we have already found the node to delete

Deletion of a non-internal node

Deletion of a non-internal node

Deletion of a non-internal node

Deletion of a non-internal node

Deletion of a non-internal node

Deletion of a non-internal node

Deletion of a non-internal node

Deletion of a non-internal node

Deletion of an internal node Only 2 cases to consider

Deletion of an internal node Internal – 2 children Actually only 1 case to consider

Deletion of an internal node get the node, call it w, to the right of this node

Deletion of an internal node get the node, call it w, to the right of this node w

Deletion of an internal node get the node, call it w, to the right of this node get the “smallest” node, call it y, in the subtree rooted at w w

Deletion of an internal node get the node, call it w, to the right of this node get the “smallest” node, call it y, in the subtree rooted at w w y

Deletion of an internal node get the node, call it w, to the right of this node get the “smallest” node, call it y, in the subtree rooted at w w y

Deletion of an internal node get the node, call it w, to the right of this node get the “smallest” node, call it y, in the subtree rooted at w replace what’s in node with what’s in y w y

Deletion of an internal node get the node, call it w, to the right of this node get the “smallest” node, call it y, in the subtree rooted at w replace what’s in node with what’s in y y w y

Deletion of an internal node get the node, call it w, to the right of this node get the “smallest” node, call it y, in the subtree rooted at w replace what’s in node with what’s in y node y is not-internal, delete it (as before) y w y

Deletion of an internal node get the node, call it w, to the right of this node get the “smallest” node, call it y, in the subtree rooted at w replace what’s in node with what’s in y node y is not-internal, delete it (as before) y w y

Deletion of an internal node get the node, call it w, to the right of this node get the “smallest” node, call it y, in the subtree rooted at w replace what’s in node with what’s in y node y is not-internal, delete it (as before)

Groovey. But what if the node is the root? Deletion of a node Groovey. But what if the node is the root?

Deletion of the root Only 4 cases to consider

Deletion of the root Only 4 cases to consider … or is there?

Deletion of the root

Deletion of the root

Deletion of the root

Deletion of the root

Deletion of the root

Finding the node to delete Deletion of a node As before …

All of the code for deletion

Deletion of a node All of the code

Is there a demo?