Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Complete below two functions (bolded) in OCaml (******************) (** Starter Code **) (******************) (* Red-Black Tree Data Type *) (* type data = int *)

Complete below two functions (bolded) in OCaml

(******************) (** Starter Code **) (******************)

(* Red-Black Tree Data Type *) (* type data = int *) type color = Red | Black type 'v rbtree = | Empty | Rnode of 'v rbtree * 'v * 'v rbtree (* Red node *) | Bnode of 'v rbtree * 'v * 'v rbtree (* Black node *)

(* Return the color of an rbtree node *) let rbt_color t = match t with Rnode(_,_,_) -> Red | Bnode(_,_,_) | Empty -> Black

(* Result of comparing two values *) (* if a < b, then cmp a b -> Lesser *) (* if a = b, then cmp a b -> Equal *) (* if a > b, then cmp a b -> Greater *) type cmp_result = | Lesser | Equal | Greater

(* compare two data elements of type 'v *) type 'v cmp_fun = 'v -> 'v -> cmp_result

(* Test if t satisfies the red-black tree invariants. * * 1. Does every red node have only black children? * 2. Does every path from root to leaf have the same number of black nodes? *) let rbt_is_invariant (t : 'v rbtree) : bool = (* Check if every red node has only black children *) if t = Empty then true else let rec h tree = match tree with | Empty -> true | Rnode(Rnode(_,_,_),_,_) -> false | Rnode(_,_,Rnode(_,_,_)) -> false | Rnode(left_rbtree,_,right_rbtree) -> h left_rbtree && h right_rbtree | Bnode(left_rbtree,_,right_rbtree) -> h left_rbtree && h right_rbtree in (* Every path has same number of black nodes *) let rec count_black_path tree count = match tree with | Empty -> count | Bnode(left,_,right) -> let left_c = count_black_path left count+1 in let right_c = count_black_path right count+1 in if left_c <> right_c then 0 else left_c | Rnode(left,_,right) -> let left_c = count_black_path left count in let right_c = count_black_path right count in if left_c <> right_c then 0 else left_c in h t && (count_black_path t 0) > 0 (* Test if red-black tree t is sorted. *) let rec rbt_is_sorted (cmp : 'v cmp_fun) (t : 'v rbtree) : bool = (* TODO, remove true *) true

(* Search for element x in red-black tree t. * * Return true if the tree contains x and false if it does not. *) let rec rbt_search (cmp : 'v cmp_fun) (t : 'v rbtree) (x:'v) : bool = match t with | Empty -> false | Bnode(left,curr,right) -> (let res = cmp curr x in match res with | Equal -> true | Lesser -> rbt_search cmp right x | Greater -> rbt_search cmp left x) | Rnode(left,curr,right) -> (let res = cmp curr x in match res with | Equal -> true | Lesser -> rbt_search cmp right x | Greater -> rbt_search cmp left x)

(* Balance constructor for a red-black tree *) let rbt_balance (c:color) (l : 'v rbtree) (v : 'v ) (r : 'v rbtree) : 'v rbtree = match (c,l,v,r) with (* TODO, remove/modify the two cases below *) | (Red,_,_,_) -> Rnode(l,v,r) | (Black,_,_,_) -> Bnode(l,v,r)

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Professional Microsoft SQL Server 2012 Administration

Authors: Adam Jorgensen, Steven Wort

1st Edition

1118106881, 9781118106884

More Books

Students also viewed these Databases questions