-
Notifications
You must be signed in to change notification settings - Fork 25
Red Black Tree #136
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
MinionJakub
wants to merge
37
commits into
fram-lang:master
Choose a base branch
from
MinionJakub:master
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Red Black Tree #136
Changes from 1 commit
Commits
Show all changes
37 commits
Select commit
Hold shift + click to select a range
7f21e72
Ordered Map and Ordered Set
MinionJakub 4d6a53a
Update OrderedMap.fram
MinionJakub c017c3c
Merge branch 'fram-lang:master' into master
MinionJakub ef1d14f
Implementation of Queues
MinionJakub 8bcd905
Testing files for Map, Set, Queues.
MinionJakub 27ff11e
Update OrderedMap.fram
MinionJakub dae76bc
Changes to implementation and tests
MinionJakub e5d206f
Adding missing files
MinionJakub ce31a6d
Merge branch 'fram-lang:master' into master
MinionJakub 5b3cd88
Correcting a test file
MinionJakub eb36254
Correcting definitions
MinionJakub 274f324
Changing from [E] in [|E]
MinionJakub 8584322
Correcting style and renaming few things
MinionJakub 9aa2f4a
Merge branch 'fram-lang:master' into master
MinionJakub 7bb419d
Correcting a lot of things mainly coding style
MinionJakub bb826df
Changing tests and adding assert to prelude
MinionJakub a3f0b7b
little changes
MinionJakub c44f26c
Moving things from Ord and adding fromList and toList to Queue
MinionJakub b9eb957
onError methods
MinionJakub 88ee81d
correcting test for names changes
MinionJakub 6727f66
little change
MinionJakub e462f83
Merge branch 'fram-lang:master' into master
MinionJakub 9ade649
Small changes
MinionJakub 2904ee7
Adding comments
MinionJakub 9dc0c0c
Merge branch 'fram-lang:master' into master
MinionJakub 7469061
Some (hopefully) final changes
Foxinio c4675a9
Replacing '\t' with ' '
Foxinio cd894b1
Final touches before merge by Foxinio
MinionJakub 8f31eb6
Fix file permissions
forell ec465d9
Merge branch 'fram-lang:master' into master
MinionJakub 394a919
Correction due to change in syntax
MinionJakub dd86d99
Merge branch 'fram-lang:master' into master
MinionJakub 5450ed1
Correcting small mistakes and adding some longly requested papers and…
MinionJakub f9ccbf0
Merge branch 'fram-lang:master' into master
MinionJakub b447f1e
Merge branch 'fram-lang:master' into master
MinionJakub 2549f93
Changing to incremental changes - for now queues + red-black tree
MinionJakub da11171
Merge branch 'master' into master
MinionJakub File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,315 @@ | ||
| let false = False | ||
| let true = True | ||
MinionJakub marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| rec | ||
MinionJakub marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| data Color = Red | Black | ||
| data Tree Value = Leaf | Node of Color, Tree Value , Value , Tree Value | ||
MinionJakub marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| end | ||
|
|
||
| method empty {X,self : Tree X} = Leaf | ||
|
|
||
| let isEmpty tree = | ||
| match tree with | ||
| | Leaf => true | ||
| | _ => false | ||
| end | ||
|
|
||
| method isEmpty {X, self : Tree X} = isEmpty self | ||
|
|
||
| let id x = x | ||
|
|
||
| let balance tree = | ||
| match tree with | ||
| | Node Black (Node Red (Node Red a x b) y c) z d => | ||
| Node Red (Node Black a x b) y (Node Black c z d) | ||
| | Node Black (Node Red a x (Node Red b y c)) z d => | ||
| Node Red (Node Black a x b) y (Node Black c z d) | ||
| | Node Black a x (Node Red (Node Red b y c) z d) => | ||
| Node Red (Node Black a x b) y (Node Black c z d) | ||
| | Node Black a x (Node Red b y (Node Red c z d)) => | ||
| Node Red (Node Black a x b) y (Node Black c z d) | ||
| | x => x | ||
| end | ||
MinionJakub marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| let paintItBlack tree = | ||
MinionJakub marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| match tree with | ||
| | Node Red a x b => Node Black a x b | ||
| | _ => tree | ||
| end | ||
|
|
||
| let repaint tree = | ||
| match tree with | ||
| | Node Red _ _ _ => (False,paintItBlack tree) | ||
| | _ => (True, tree) | ||
| end | ||
|
|
||
| let chkDelL color tree = | ||
MinionJakub marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| match color,tree with | ||
| | (Red,Leaf) => (False,Leaf) | ||
| | (Black,Leaf) => (True,Leaf) | ||
| | (Black, Node Red Leaf x Leaf) => (False, Node Black Leaf x Leaf) | ||
| | _ => (False,tree) (*Impossible*) | ||
| end | ||
|
|
||
| let balL krotka = | ||
MinionJakub marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| match krotka with | ||
| | (Black,Node Red (Node Red a x b) y c, z ,d) | ||
| => Node Red (Node Black a x b) y (Node Black c z d) | ||
MinionJakub marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| | (Black,Node Red a x (Node Black b y c), z,d) | ||
| => Node Red (Node Black a x b) y (Node Black c z d) | ||
| | (color,a,x,b) => Node color a x b | ||
| end | ||
|
|
||
| let balR krotka = | ||
MinionJakub marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| match krotka with | ||
| | (Black,a,x,Node Red b y (Node Red c z d)) | ||
| => Node Red (Node Black a x b) y (Node Black c z d) | ||
| | (Black,a,x,Node Red (Node Red b y c) z d) | ||
| => Node Red (Node Black a x b) y (Node Black c z d) | ||
| | (color,a,x,b) => Node color a x b | ||
| end | ||
|
|
||
| let balDelL krotka = | ||
MinionJakub marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| match krotka with | ||
| | (False,color,left,value,right) | ||
| => (False,Node color left value right) | ||
| | (True,Red,left,value,Node Red b y c) | ||
| => (False,balR (Black,left,value,Node Red b y c)) | ||
| | (True,Black,_,x,Leaf) | ||
| => (false,Node Black Leaf x Leaf) | ||
| | (True,Black,left,x,Node Black b y c) | ||
| => repaint (balR (Black,left,x,Node Red b y c)) | ||
| | (True,Black,_,x,Node Red Leaf y Leaf) | ||
| => (False, Node Black Leaf x (Node Red Leaf y Leaf)) | ||
| | (true,Black,a,x,Node Red (Node Black b y c) z d) | ||
| => (false, Node Black (balL (Black,Node Red a x b,y,c)) z d) | ||
| | (bf,color,a,x,b) | ||
| => (bf,Node color a x b) (*Impossible*) | ||
| end | ||
|
|
||
| let rec delMin tree = | ||
| match tree with | ||
| | Node color Leaf value right => (Some value, chkDelL color right) | ||
| | Node color left value right => | ||
| let (min,(bf,newleft)) = delMin left | ||
| in (min,balDelL (bf,color,newleft,value,right)) | ||
| | Leaf => (None,(False,Leaf)) | ||
| end | ||
|
|
||
| let deletMin tree = let (value,(_,newtree)) = delMin tree in (value,newtree) | ||
MinionJakub marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| method deletMin {X,self : Tree X} = deletMin self | ||
|
|
||
| let chkDelR color tree = | ||
| match color,tree with | ||
| | (Black,Leaf) => (true,Leaf) | ||
| | (Red,Leaf) => (false,Leaf) | ||
| | (Black,Node Red Leaf value Leaf) => | ||
| (false,Node Black Leaf value Leaf) | ||
| | _ => (False,tree) (*Impossible*) | ||
| end | ||
|
|
||
| let balDelR krotka = | ||
| match krotka with | ||
| | (false,color,left,value,right) => (false, Node color left value right) | ||
| | (true,Red,Node Black a x b, value,right) | ||
| => (false, balL (Black,Node Red a x b,value,right)) | ||
| | (true,Black,Leaf,value,_) => (false,Node Black Leaf value Leaf) | ||
| | (true,Black,Node Black a x b,value,right) | ||
| => repaint (balL (Black,Node Red a x b,value,right)) | ||
| | (true, Black, Node Red Leaf x Leaf, value, right) | ||
| => (false,Node Black (Node Red Leaf x Leaf) value right) | ||
| | (true,Black, Node Red a x (Node Black b y c),value,right) | ||
| => (false, Node Black a x (balR (Black,b,y,Node Red c value right))) | ||
| | (bf,color,left,value,right) => (bf,Node color left value right) (*Impossible*) | ||
MinionJakub marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| end | ||
|
|
||
| let rec delMax tree = | ||
| match tree with | ||
| | Node color left value Leaf => (Some value, chkDelR color left) | ||
| | Node color left value right | ||
| => let (min,(bf,newright)) = delMax right | ||
| in (min,balDelR(bf,color,left,value,newright)) | ||
| | Leaf => (None,(false,Leaf)) | ||
| end | ||
|
|
||
| let deletMax tree = | ||
| let (min,(_,newtree)) = delMax tree | ||
| in (min,newtree) | ||
|
|
||
| method deletMax {X, self : Tree X} = deletMax self | ||
|
|
||
| let rec del elem tree lt = | ||
| match tree with | ||
| | Leaf => (false,Leaf) | ||
| | Node color left value right | ||
| => if lt elem value | ||
| then (let (done,newleft) = del elem left lt | ||
| in balDelL (done,color,newleft,value,right)) | ||
| else if lt value elem | ||
| then (let (done,newright) = del elem right lt | ||
| in balDelR (done,color,left,value,right)) | ||
| else if isEmpty left then chkDelL color right | ||
| else if isEmpty right then chkDelR color left | ||
| else (let (maks, (bf,newtree)) = delMax left in | ||
| match maks with | ||
| | Some v => balDelL (bf,color,newtree,v,right) | ||
| | None => balDelL(bf,color,newtree,elem,right) (*Impossible*) | ||
| end) | ||
| end | ||
|
|
||
| let delete elem tree lt = let (_,solution) = del elem tree lt in solution | ||
|
|
||
| method remove {X, self : Tree X} elem lt = delete elem self lt | ||
|
|
||
| let insert tree elem lt = | ||
| let rec ins tree = | ||
| match tree with | ||
| | Leaf => Node Red Leaf elem Leaf | ||
| | Node color a y b => | ||
| if lt elem y then balance (Node color (ins a) y b) | ||
| else if lt y elem then balance (Node color a y (ins b)) | ||
| else Node color a y b | ||
MinionJakub marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| end | ||
| let y = ins tree | ||
| in match y with | ||
| | Node _ y a b => Node Black y a b | ||
| | Leaf => Leaf | ||
| end | ||
|
|
||
| method add {X, self : Tree X} elem lt = insert self elem lt | ||
|
|
||
| let toList tree = | ||
| let rec _toList tree acc = | ||
| match tree with | ||
| | Leaf => acc | ||
| | Node _ a y b => _toList a (y :: _toList b acc) | ||
| end | ||
| in _toList tree [] | ||
|
|
||
| method toList {X , self : Tree X} = toList self | ||
|
|
||
| let fromList {X} (lista : List X) lt = | ||
| let rec _fromList lista (acc : Tree X) = | ||
| match lista with | ||
| | x :: xs => _fromList xs (acc.add x lt) | ||
| | [] => acc | ||
| end | ||
| in _fromList lista Leaf | ||
|
|
||
| let rec foldlRB fun acc from = | ||
| match from with | ||
| | Leaf => acc | ||
| | Node _ a y b => | ||
| let left = foldlRB fun acc a in | ||
| let middle = (fun y left) in | ||
| foldlRB fun middle b | ||
| end | ||
|
|
||
| let rec foldrRB fun acc from = | ||
| match from with | ||
| | Leaf => acc | ||
| | Node _ a y b => | ||
| let right = foldlRB fun acc b in | ||
MinionJakub marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| let middle = (fun y right) in | ||
| foldlRB fun middle a | ||
| end | ||
|
|
||
| method traversL {X, self : Tree X} fun acc = foldlRB fun acc self | ||
| method traversR {X, self : Tree X} fun acc = foldrRB fun acc self | ||
|
|
||
| //TODO: better merge | ||
| let merge {X} (from : Tree X) (to : Tree X) lt = | ||
| foldlRB (fn x (y : Tree X) => y.add x lt) to from | ||
|
|
||
| let rec member elem tree lt = | ||
| match tree with | ||
| | Leaf => False | ||
| | Node _ l v r => | ||
| if lt elem v then member elem l lt | ||
| else if lt v elem then member elem r lt | ||
| else True | ||
| end | ||
|
|
||
|
|
||
| method member {X, self : Tree X} elem lt = member elem self lt | ||
|
|
||
|
|
||
| let add lt tree key value = insert tree (key,value) lt | ||
| let join lt tree1 tree2 = merge tree1 tree2 lt | ||
| let rmVal lt key tree = match tree with | ||
| | Leaf => Leaf | ||
| | Node _ _ (_,v) _ => delete (key,v) tree lt | ||
| end | ||
|
|
||
| let rec _find lt elem tree = | ||
| match tree with | ||
| | Leaf => None | ||
| | Node _ l v r => if lt elem v then _find lt elem l | ||
| else if lt v elem then _find lt elem r else Some v | ||
| end | ||
|
|
||
| let find lt key tree = | ||
| match tree with | ||
| | Leaf => None | ||
| | Node _ l v r => let (_,a) = v in _find lt (key,a) tree | ||
| end | ||
|
|
||
| let update lt tree key val = let x = rmVal lt key tree in | ||
| add lt x key val | ||
|
|
||
| let fst (a,b) = a | ||
MinionJakub marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| let snd (a,b) = b | ||
|
|
||
| data OrderedMap Key Val = Map of | ||
| { | ||
| T | ||
| , empty : T | ||
| , method add : T -> Key -> Val -> [] T | ||
| , method join : T -> T -> [] T | ||
| , method isEmpty : T -> [] Bool | ||
| , method deleteElem : Key -> T -> [] T | ||
| , method toList : T -> [] List (Pair Key Val) | ||
| , method update : T -> Key -> Val -> [] T | ||
| , method deleteMax : T -> [] T | ||
| , method deleteMin : T -> [] T | ||
| } | ||
|
|
||
| pub let make {Key} {Val} (lt : Key -> [] Key -> [] Bool)= | ||
MinionJakub marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| Map { | ||
| T = Tree (Pair Key Val) | ||
| , empty = Leaf | ||
| , method add = add (fn (a,b) (c,d) => lt a c) | ||
| , method join = join (fn (a,b) (c,d) => lt a c) | ||
| , method isEmpty = isEmpty | ||
| , method deleteElem = rmVal (fn (a,b) (c,d) => lt a c) | ||
| , method toList = toList | ||
| , method update = update (fn (a,_) (key,_) => lt a key) | ||
| , method deleteMax = fn mapa => snd (deletMax mapa) | ||
| , method deleteMin = fn mapa => snd (deletMin mapa) | ||
| } | ||
|
|
||
| data OrderedSet Value = Set of { | ||
| T | ||
| , empty : T | ||
| , method insert : T -> Value -> [] T | ||
| , method singleton : Value -> [] T | ||
| , method remove : Value -> T -> [] T | ||
| , method union : T -> T -> [] T | ||
| , method find : Value -> T -> [] Option Value | ||
| , method deleteMax : T -> [] T | ||
| , method deleteMin : T -> [] T | ||
| } | ||
|
|
||
| pub let make {Value} (lt : Value -> [] Value -> [] Bool) = | ||
MinionJakub marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| Set { | ||
| T = Tree Value | ||
| , empty = Leaf | ||
| , method insert = fn tree elem => insert tree elem lt | ||
| , method singleton = fn elem => insert Leaf elem lt | ||
| , method remove = fn elem tree => delete elem tree lt | ||
| , method union = join lt | ||
| , method find = _find lt | ||
| , method deleteMax = fn mapa => snd (deletMax mapa) | ||
| , method deleteMin = fn mapa => snd (deletMin mapa) | ||
| } | ||
MinionJakub marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.