Functional Programming Lecture 8 - Binary Search Trees - PowerPoint PPT Presentation

About This Presentation
Title:

Functional Programming Lecture 8 - Binary Search Trees

Description:

We can use this type for bst's provided a is an Ord type and all operations ... To check for bst, whenever we take a right branch, we establish a minimum, and ... – PowerPoint PPT presentation

Number of Views:197
Avg rating:3.0/5.0
Slides: 19
Provided by: muffyc
Category:

less

Transcript and Presenter's Notes

Title: Functional Programming Lecture 8 - Binary Search Trees


1
Functional ProgrammingLecture 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) gt Tree a -gt 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 lt root t2) isbst t2
  • isbst (Node n t1 Nil) (n gt root t1) isbst t1
  • isbst (Node n t1 t2) (n gt root t1) (n lt root
    t2)
  • isbst t1
    isbst t2
  • root Tree a -gt 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
  • 4
  • 2 10
  • 8 22
  • 3 9
  • 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
  • 4
  • 2 10 When we consider 8, max 10
    min 4
  • 8 22 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 gt (Tree a) -gt 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)gtx (isbstr
    t2 x) -- 1st right
  • isbst (Node x t1 Nil) (root t1)ltx (isbstl
    t1 x) -- 1st left
  • isbst (Node x t1 t2 ) (root t1)ltx (root
    t2)gtx
  • (isbstl t1 x) (isbstr
    t2 x) -- 1st right and left

7
Checking a Binary Search Tree
  • isbstr Ord a gt (Tree a) -gt a -gt 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 gt min)
  • isbstr (Node x Nil t2) min
  • (x gt min) (root t2)gtx (isbstr t2 x)
    -- still right only

  • -- refine min
  • isbstr (Node x t1 Nil) min
  • (x gt min) (root t1)ltx (isbstminmax
    t1 min x)
  • isbstr (Node x t1 t2 ) min
  • (x gt min) (root t1)ltx (root t2)gtx
  • (isbstr t2 x) (isbstminmax t1 min
    x) -- refine min

8
Checking a Binary Search Tree
  • isbstl Ord a gt (Tree a) -gt a -gt 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 lt max)
  • isbstl (Node x Nil t2) max
  • (x lt max) (root t2)gtx (isbstminmax t2
    x max )
  • isbstl (Node x t1 Nil) max
  • (x lt max) (root t1)ltx (isbstl t1 x)
    -- still left only

  • -- refine max
  • isbstl (Node x t1 t2 ) max
  • (x lt max) (root t1)ltx (root t2)gtx
  • (isbstminmax t2 x max) (isbstl t1
    x) -- refine max

9
  • isbstminmax Ord a gt (Tree a) -gt a -gt a -gt
    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 gt min)
    (x lt max)
  • isbstminmax (Node x Nil t2) min max
  • (x gt min) (x lt max) (root t2)gtx
  • (isbstminmax t2 x max )
  • isbstminmax (Node x t1 Nil) min max
  • (x gt min) (x lt max) (root t1)ltx
  • (isbstminmax t1 min x )
  • isbstminmax (Node x t1 t2) min max
  • (x gt min) (x lt max) (root t1)ltx
    (root t2) gtx
  • (isbstminmax t1 min x ) (isbstminmax t2
    x max )
  • root Tree a -gt 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)))
  • -- Maingt isbst good
  • -- True
  • -- Maingt isbst bad
  • -- False
  • --Maingt

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 xltroot t.
  • - Insert x in right subtree of t if xgtroot t.
  • E.g.
  • insert 5 into 4 gt 4
  • 2 6
    2 6

  • 5

12
Insertion
  • insert Ord a gt a -gt Tree a -gt Tree a
  • -- insert a element in a bst at correct position
  • insert x Nil Node x Nil Nil
  • insert x (Node n t1 t2)
  • (xn) Node n t1 t2
  • (xltn) Node n (insert x t1) t2
  • (xgtn) 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 lt root t, then delete x in left subtree of
    t.
  • - If x gt root t, then delete x in right subtree
    of t.
  • - If xroot t and if either subtree is Nil,
    return the other subtree.
  • - If xroot t, and both subtrees are non-Nil,
    return the conjoined subtrees.
  • E.g. delete 2 4 4
  • 2 5 gt
    5
  • delete 2 4 gt 4
  • 2 5 1
    5
  • 1
  • delete 7 7
    8
  • 2 9 gt
    2 9
  • 8

14
Auxiliary Functions
  • isNil Tree a -gt Bool
  • -- determine whether a bst is empty
  • isNil Nil True
  • isNil (Node n t1 t2) False
  • mintree Ord a gt Tree a -gt 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) gtTree a -gt Tree a -gt 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 gt a -gt Tree a -gt Tree a
  • -- delete an item from a bst
  • delete x Nil Nil
  • delete x (Node n t1 t2)
  • (x lt n) Node n (delete x t1) t2
  • (x gt 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 -gt 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 gt a -gt STree a -gt 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)
  • (xltn) Node n (1 s) newt1 t2
  • (xgtn) 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.
Write a Comment
User Comments (0)
About PowerShow.com