1 Functional Programming Lecture 8 - Binary Search Trees.

Slides:



Advertisements
Similar presentations
Introduction to Algorithms
Advertisements

COSC 2007 Data Structures II Chapter 12 Advanced Implementation of Tables II.
0 - 0.
Chapter 7. Binary Search Trees
CS16: Introduction to Data Structures & Algorithms
1 Parallel Algorithms (chap. 30, 1 st edition) Parallel: perform more than one operation at a time. PRAM model: Parallel Random Access Model. p0p0 p1p1.
Binary Search Trees Ravi Chugh March 28, Review: Linked Lists Goal: Program that keeps track of friends Problem: Arrays have fixed length Solution:
Chapter 12 Binary Search Trees
CS 206 Introduction to Computer Science II 04 / 10 / 2009 Instructor: Michael Eckmann.
Rizwan Rehman Centre for Computer Studies Dibrugarh University
Foundations of Data Structures Practical Session #7 AVL Trees 2.
§2 Binary Trees Note: In a tree, the order of children does not matter. But in a binary tree, left child and right child are different. A B A B andare.
COL 106 Shweta Agrawal and Amit Kumar
Comp 122, Spring 2004 Binary Search Trees. btrees - 2 Comp 122, Spring 2004 Binary Trees  Recursive definition 1.An empty tree is a binary tree 2.A node.
Trees Types and Operations
Binary Trees, Binary Search Trees CMPS 2133 Spring 2008.
Binary Search Trees vs. Binary Heaps. Binary Search Tree Condition Given a node i –i.value is the stored object –i.left and i.right point to other nodes.
1 Binary Search Trees (BSTs). 2 Consider the search operation FindKey (): find an element of a particular key value in a binary tree. This operation takes.
Chapter 9 contd. Binary Search Trees Anshuman Razdan Div of Computing Studies
Heaps and heapsort COMP171 Fall 2005 Part 2. Sorting III / Slide 2 Heap: array implementation Is it a good idea to store arbitrary.
1 Section 9.2 Tree Applications. 2 Binary Search Trees Goal is implementation of an efficient searching algorithm Binary Search Tree: –binary tree in.
Balanced Search Trees CS 3110 Fall Some Search Structures Sorted Arrays –Advantages Search in O(log n) time (binary search) –Disadvantages Need.
1 Multiway trees & B trees & 2_4 trees Go&Ta Chap 10.
Trees, Binary Search Trees, Recursion, Project 2 Bryce Boe 2013/08/01 CS24, Summer 2013 C.
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.
CMSC 341 Splay Trees. 8/3/2007 UMBC CMSC 341 SplayTrees 2 Problems with BSTs Because the shape of a BST is determined by the order that data is inserted,
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, Binary Search Trees RIZWAN REHMAN CENTRE FOR COMPUTER STUDIES DIBRUGARH UNIVERSITY.
Binary Search Trees Binary Search Trees (BST)  the tree from the previous slide is a special kind of binary tree called a binary.
CMSC 341 B- Trees D. Frey with apologies to Tom Anastasio.
Lecture – Searching a Tree Neil Ghani University of Strathclyde.
Data Structures Haim Kaplan and Uri Zwick November 2012 Lecture 3 Dynamic Sets / Dictionaries Binary Search Trees.
CS 206 Introduction to Computer Science II 02 / 13 / 2009 Instructor: Michael Eckmann.
1 Joe Meehean.  We wanted a data structure that gave us... the smallest item then the next smallest then the next and so on…  This ADT is called a priority.
Binary Search Trees Lecture 5 1. Binary search tree sort 2.
H EAPS. T WO KINDS OF HEAPS : MAX AND MIN Max: Every child is smaller than its parent Meaning the max is the root of the tree 10 / \ 9 7 / \ 6 8 / \ 2.
Binary Tree. Some Terminologies Short review on binary tree Tree traversals Binary Search Tree (BST)‏ Questions.
ADT Binary Search Tree Ellen Walker CPSC 201 Data Structures Hiram College.
Binary Search Trees (BST)
Tree Data Structures. Heaps for searching Search in a heap? Search in a heap? Would have to look at root Would have to look at root If search item smaller.
Lecture 19. Binary Search Tree 1. Recap Tree is a non linear data structure to present data in hierarchical form. It is also called acyclic data structure.
B-TREE. Motivation for B-Trees So far we have assumed that we can store an entire data structure in main memory What if we have so much data that it won’t.
Copyright © 2012 Pearson Education, Inc. Chapter 20: Binary Trees.
Course: Programming II - Abstract Data Types HeapsSlide Number 1 The ADT Heap So far we have seen the following sorting types : 1) Linked List sort by.
(c) University of Washington20c-1 CSC 143 Binary Search Trees.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 20: Binary Trees.
CS6045: Advanced Algorithms Data Structures. Dynamic Sets Next few lectures will focus on data structures rather than straight algorithms In particular,
CISC220 Fall 2009 James Atlas Lecture 13: Binary Trees.
Binary Search Tree (BST)
O(lg n) Search Tree Tree T is a search tree made up of n elements: x0 x1 x2 x3 … xn-1 No function (except transverse) takes more than O(lg n) in the.
Binary Trees, Binary Search Trees
Chapter 20: Binary Trees.
Chapter 21: Binary Trees.
Binary Search Tree AVL Tree
Height Balanced Trees 2-3 Trees.
Find in a linked list? first last 7  4  3  8 NULL
Binary Search Trees (BSTs)
CMSC 341 Splay Trees.
Chapter 12: Binary Search Trees
Binary Search Trees Chapter 9 2/22/2019 B.Ramamurthy.
CMSC 341 Splay Trees.
CSC 143 Binary Search Trees.
Heaps By JJ Shepherd.
CMSC 341 Splay Trees.
B-Trees.
CMSC 341 Splay Trees.
Chapter 12&13: Binary Search Trees (BSTs)
Algorithms CSCI 235, Spring 2019 Lecture 21 Binary Search Trees
Data Structures Using C++ 2E
Presentation transcript:

1 Functional Programming Lecture 8 - Binary Search Trees

2 Binary Search Trees A bst is a tree whose elements are ordered. A general tree is defined by: data Tree a = Nil | Node a (Tree a) (Tree a) We can use this type for bsts provided a is an Ord type and all operations either create or preserve the ordering. This means that an arbitrary t :: Tree a may not be a bst. We need to check that it is a bst.

3 Checking a Binary Search Tree A bst must have ordered elements, and no repeated elements; i.e. all values in left subtree are smaller than root and all values in right subtree are larger than root. isbst :: (Ord a) => Tree a -> Bool isbst Nil = True Many ways to define isbst for nodes. Here is one: isbst (Node n Nil Nil) = True isbst (Node n Nil t2) = (n < root t2) && isbst t2 isbst (Node n t1 Nil) = (n > root t1) && isbst t1 isbst (Node n t1 t2) = (n > root t1) && (n < root t2) && isbst t1 && isbst t2 root :: Tree a -> a root (Node n _ _) = n

4 Checking a Binary Search Tree Is this function sufficient? If a tree is a bst then isbst returns True, but if it isnt a bst, does it return False? Consider the tree bad = (Node 4 (Node 2 Nil Nil) (Node 10 (Node 8 (Node 3 Nil Nil) (Node 9 Nil Nil)) (Node 22 Nil Nil))) (

5 Checking a Binary Search Tree To check for bst, whenever we take a right branch, we establish a minimum, and whenever we take a left branch, we establish a maximum. So, we have the following, when considering a node in a path: if only root, no min or max yet established if only left branches so far, then we have a max if only right branches so far, then we have a min if left and right branches so far, then we have min and a max When we consider 8, max= 10 min = When we consider 3, max= 8 min = 4 3 9

6 Checking a Binary Search Tree data Tree a = Nil | Node a (Tree a) (Tree a) isbst :: Ord a => (Tree a) -> Bool -- is a binary search tree -- top level function isbst Nil = True isbst (Node x Nil Nil) = True isbst (Node x Nil t2) = (root t2)>x && (isbstr t2 x) -- 1st right isbst (Node x t1 Nil) = (root t1)<x && (isbstl t1 x) -- 1st left isbst (Node x t1 t2 ) = (root t1) x && (isbstl t1 x) && (isbstr t2 x) -- 1st right and left

7 Checking a Binary Search Tree isbstr :: Ord a => (Tree a) -> a -> Bool -- is a binary search tree -- searching a right only branch -- check node is greater than a min -- refine min if going right only isbstr (Node x Nil Nil) min = (x > min) isbstr (Node x Nil t2) min = (x > min) && (root t2)>x && (isbstr t2 x) -- still right only -- refine min isbstr (Node x t1 Nil) min = (x > min) && (root t1)<x && (isbstminmax t1 min x) isbstr (Node x t1 t2 ) min = (x > min) && (root t1) x && (isbstr t2 x) && (isbstminmax t1 min x) -- refine min

8 Checking a Binary Search Tree isbstl :: Ord a => (Tree a) -> a -> Bool -- is a binary search tree -- searching a leftonly branch -- check node is less than a max -- refine max if going left only isbstl (Node x Nil Nil) max = (x < max) isbstl (Node x Nil t2) max = (x x && (isbstminmax t2 x max ) isbstl (Node x t1 Nil) max = (x < max) && (root t1)<x && (isbstl t1 x) -- still left only -- refine max isbstl (Node x t1 t2 ) max = (x x && (isbstminmax t2 x max) && (isbstl t1 x) -- refine max

9 isbstminmax :: Ord a => (Tree a) -> a -> a -> Bool -- is a binary search tree -- searching a branch which is left and right -- check node is less than a max and greater than a min -- refine max and min isbstminmax (Node x Nil Nil) min max = (x > min) && (x < max) isbstminmax (Node x Nil t2) min max = (x > min) && (x x && (isbstminmax t2 x max ) isbstminmax (Node x t1 Nil) min max = (x > min) && (x < max) && (root t1)<x && (isbstminmax t1 min x ) isbstminmax (Node x t1 t2) min max = (x > min) && (x x && (isbstminmax t1 min x ) && (isbstminmax t2 x max ) root :: Tree a -> a root (Node x _ _ ) = x --

10 good :: Tree Int good = (Node 4 (Node 2 Nil Nil) (Node 10 (Node 8 (Node 6 Nil Nil) (Node 9 Nil Nil)) (Node 22 Nil Nil))) bad :: Tree Int bad = (Node 4 (Node 2 Nil Nil) (Node 10 (Node 8 (Node 3 Nil Nil) (Node 9 Nil Nil)) (Node 22 Nil Nil))) -- Main> isbst good -- True -- Main> isbst bad -- False --Main>

11 Insertion To insert x into bst t - Do not insert x if x is already in t. - Insert x in left subtree of t if x<root t. - Insert x in right subtree of t if x>root t. E.g. insert 5 into 4 =>

12 Insertion insert :: Ord a => a -> Tree a -> Tree a -- insert a element in a bst at correct position insert x Nil = Node x Nil Nil insert x (Node n t1 t2) | (x==n) = Node n t1 t2 | (x<n) = Node n (insert x t1) t2 | (x>n) = Node n t1 (insert x t2) Insert 5 (Node 4 (Node 2 Nil Nil) (Node 6 Nil Nil))

13 Deletion To delete x from bst t. - If x < root t, then delete x in left subtree of t. - If x > root t, then delete x in right subtree of t. - If x==root t and if either subtree is Nil, return the other subtree. - If x==root t, and both subtrees are non-Nil, return the conjoined subtrees. E.g. delete => 5 delete 2 4 => delete => 2 9 8

14 Auxiliary Functions isNil :: Tree a -> Bool -- determine whether a bst is empty isNil Nil = True isNil (Node n t1 t2) = False mintree :: Ord a => Tree a -> Maybe a -- return smallest element in a bst -- treat error properly! n mintree Nil = Nothing t1 t2 mintree (Node n t1 t2) = if (isNil t1) then (Just n) else (mintree t1) or, in another style: mintree t Maybe a = Nothing | isNil t = Nothing | Just a | isNil t1 = Just v | otherwise = mintree t1 where v = root t t1 = left t

15 Auxiliary Functions join :: (Ord a) =>Tree a -> Tree a -> Tree a -- join together two non-empty bsts -- the root is the minimum of the right subtree -- be careful with error values! Temptation to write: join t1 t2 = Node (mintree t2) t1 (delete (mintree t2) t2) -- new root left subtree new right subtree Why is this wrong? Because, mintree returns a Maybe type! join t1 t2 = Node m t1 t3 where (Just m) = mintree t2 t3 = delete m t2

16 Deletion delete :: (Ord) a => a -> Tree a -> Tree a -- delete an item from a bst delete x Nil = Nil delete x (Node n t1 t2) | (x < n) = Node n (delete x t1) t2 | (x > n) = Node n t1 (delete x t2) | isNil t1 = t2 -- x == n | isNil t2 = t1 -- x == n | otherwise join t1 t2 -- x == n

17 Adding Size to BSTs It is computationally expensive to compute the size of tree: size :: Tree a -> Int size Nil = 0 size (Node n t1 t2) = 1 + size t1 + size t2 If we have to do this operation often, then it is better to add an extra field to store the size. How can we do this? data STree a = Nil | Node a Int (STree a) (STree a) How will operations change?

18 Adding Size to BSTs insert :: Ord a => a -> STree a -> STree a -- insert a element in a sized bst at correct position -- assume element is not already in bst insert x Nil = Node x 1 Nil Nil insert x (Node n s t1 t2) | (x<n) = Node n (1 + s) newt1 t2 | (x>n) = Node n (1 + s) t1 newt2 where newt1 = insert x t1 newt2 = insert x t2 Note: You can extend this idea. For example, store maximum or minimum of subtrees store value of parent.