From 7ecff11d2d84a3df7cd1d8676e0f2690de4fa2f3 Mon Sep 17 00:00:00 2001 From: Emir Pasic Date: Mon, 28 Mar 2016 07:17:39 +0200 Subject: [PATCH] - revert, build failing --- examples/redblacktreeextended.go | 90 ------------------------------ trees/redblacktree/redblacktree.go | 17 ++---- 2 files changed, 6 insertions(+), 101 deletions(-) diff --git a/examples/redblacktreeextended.go b/examples/redblacktreeextended.go index ecfe421..fbb8d6c 100644 --- a/examples/redblacktreeextended.go +++ b/examples/redblacktreeextended.go @@ -103,68 +103,6 @@ func print(tree *RedBlackTreeExtended) { fmt.Println(tree) } -// Find ceiling node of the input key, return the ceiling node or nil if no ceiling is found. -// Second return parameter is true if ceiling was found, otherwise false. -// -// Ceiling node is defined as the smallest node that is larger than or equal to the given node. -// A ceiling node may not be found, either because the tree is empty, or because -// all nodes in the tree is smaller than the given node. -// -// Key should adhere to the comparator's type assertion, otherwise method panics. -func (tree *RedBlackTreeExtended) Ceiling(key interface{}) (ceiling *rbt.Node, found bool) { - found = false - comparator := tree.Comparator() - - node := tree.Root - for node != nil { - compare := comparator(key, node.Key) - switch { - case compare == 0: - return node, true - case compare < 0: - ceiling, found = node, true - node = node.Left - case compare > 0: - node = node.Right - } - } - if found { - return ceiling, true - } - return nil, false -} - -// Find floor node of the input key, return the floor node or nil if no ceiling is found. -// Second return parameter is true if floor was found, otherwise false. -// -// Floor node is defined as the largest node that is smaller than or equal to the given node. -// A floor node may not be found, either because the tree is empty, or because -// all nodes in the tree is larger than the given node. -// -// Key should adhere to the comparator's type assertion, otherwise method panics. -func (tree *RedBlackTreeExtended) Floor(key interface{}) (floor *rbt.Node, found bool) { - found = false - comparator := tree.Comparator() - - node := tree.Root - for node != nil { - compare := comparator(key, node.Key) - switch { - case compare == 0: - return node, true - case compare < 0: - node = node.Left - case compare > 0: - floor, found = node, true - node = node.Right - } - } - if found { - return floor, true - } - return nil, false -} - func RedBlackTreeExtendedExample() { tree := RedBlackTreeExtended{rbt.NewWithIntComparator()} @@ -194,32 +132,4 @@ func RedBlackTreeExtendedExample() { // Value for min key: c // RedBlackTree // └── 3 - - /* - * Ceiling and Floor functions - */ - tree = RedBlackTreeExtended{rbt.NewWithIntComparator()} - tree.Put(1, "a") - tree.Put(2, "b") - tree.Put(4, "d") - tree.Put(6, "f") - tree.Put(7, "g") - - //index, ceiling, floor - testValues := [][]interface{}{ - {0, 1, nil}, - {1, 1, 1}, - {2, 2, 2}, - {3, 4, 2}, - {4, 4, 4}, - {5, 6, 4}, - {6, 6, 6}, - {7, 7, 7}, - {8, nil, 7}, - } - for _, tt := range testValues { - actualCeiling, _ := tree.Ceiling(tt[0]) - actualFloor, _ := tree.Floor(tt[0]) - fmt.Printf("test key %d, expected (%d, %d), actual (%d, %d)\n", tt[0], tt[1], tt[2], actualCeiling.Key, actualFloor.Key) - } } diff --git a/trees/redblacktree/redblacktree.go b/trees/redblacktree/redblacktree.go index 1234361..6e527fe 100644 --- a/trees/redblacktree/redblacktree.go +++ b/trees/redblacktree/redblacktree.go @@ -51,7 +51,7 @@ const ( type Tree struct { Root *Node size int - Comparator utils.Comparator + comparator utils.Comparator } type Node struct { @@ -65,17 +65,17 @@ type Node struct { // Instantiates a red-black tree with the custom comparator. func NewWith(comparator utils.Comparator) *Tree { - return &Tree{Comparator: comparator} + return &Tree{comparator: comparator} } // Instantiates a red-black tree with the IntComparator, i.e. keys are of type int. func NewWithIntComparator() *Tree { - return &Tree{Comparator: utils.IntComparator} + return &Tree{comparator: utils.IntComparator} } // Instantiates a red-black tree with the StringComparator, i.e. keys are of type string. func NewWithStringComparator() *Tree { - return &Tree{Comparator: utils.StringComparator} + return &Tree{comparator: utils.StringComparator} } // Inserts node into the tree. @@ -88,7 +88,7 @@ func (tree *Tree) Put(key interface{}, value interface{}) { node := tree.Root loop := true for loop { - compare := tree.Comparator(key, node.Key) + compare := tree.comparator(key, node.Key) switch { case compare == 0: node.Value = value @@ -192,11 +192,6 @@ func (tree *Tree) Clear() { tree.size = 0 } -// Return comparator of the tree -func (tree *Tree) Comparator() utils.Comparator { - return tree.comparator -} - func (tree *Tree) String() string { str := "RedBlackTree\n" if !tree.Empty() { @@ -268,7 +263,7 @@ func output(node *Node, prefix string, isTail bool, str *string) { func (tree *Tree) lookup(key interface{}) *Node { node := tree.Root for node != nil { - compare := tree.Comparator(key, node.Key) + compare := tree.comparator(key, node.Key) switch { case compare == 0: return node