Skip to content

Commit

Permalink
- remove inOrder function in red-black tree and use iterator
Browse files Browse the repository at this point in the history
  • Loading branch information
emirpasic committed Jul 9, 2016
1 parent 28b8261 commit 19bf8e5
Showing 1 changed file with 6 additions and 33 deletions.
39 changes: 6 additions & 33 deletions trees/redblacktree/redblacktree.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ package redblacktree

import (
"fmt"
"github.com/emirpasic/gods/stacks/linkedliststack"
"github.com/emirpasic/gods/trees"
"github.com/emirpasic/gods/utils"
)
Expand Down Expand Up @@ -153,17 +152,19 @@ func (tree *Tree) Size() int {
// Keys returns all keys in-order
func (tree *Tree) Keys() []interface{} {
keys := make([]interface{}, tree.size)
for i, node := range tree.inOrder() {
keys[i] = node.Key
it := tree.Iterator()
for i := 0; it.Next(); i++ {
keys[i] = it.Key()
}
return keys
}

// Values returns all values in-order based on the key.
func (tree *Tree) Values() []interface{} {
values := make([]interface{}, tree.size)
for i, node := range tree.inOrder() {
values[i] = node.Value
it := tree.Iterator()
for i := 0; it.Next(); i++ {
values[i] = it.Value()
}
return values
}
Expand Down Expand Up @@ -267,34 +268,6 @@ func (node *Node) String() string {
return fmt.Sprintf("%v", node.Key)
}

// Returns all nodes in order
func (tree *Tree) inOrder() []*Node {
nodes := make([]*Node, tree.size)
if tree.size > 0 {
current := tree.Root
stack := linkedliststack.New()
done := false
count := 0
for !done {
if current != nil {
stack.Push(current)
current = current.Left
} else {
if !stack.Empty() {
currentPop, _ := stack.Pop()
current = currentPop.(*Node)
nodes[count] = current
count++
current = current.Right
} else {
done = true
}
}
}
}
return nodes
}

// String returns a string representation of container
func output(node *Node, prefix string, isTail bool, str *string) {
if node.Right != nil {
Expand Down

0 comments on commit 19bf8e5

Please sign in to comment.