Skip to content

Commit

Permalink
Solve the "Compute binary tree nodes in order of increasing depth" pr…
Browse files Browse the repository at this point in the history
…oblem
  • Loading branch information
mrekucci committed Oct 23, 2015
1 parent 3a686c8 commit 91c63e9
Show file tree
Hide file tree
Showing 3 changed files with 107 additions and 3 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ Stacks and Queues

| Problem | Test | Implemented |
|--------------------------------------------------------------------------|:------------:|:-----------:|
| [Compute binary tree nodes in order of increasing depth][136] | [tests][137] | |
| [Compute binary tree nodes in order of increasing depth][136] | [tests][137] | |
| [Implement a circular queue][138] | [tests][139] ||
| [Implement a queue using stacks][140] | [tests][141] ||
| [Implement a queue with max API][142] | [tests][143] | |
Expand Down Expand Up @@ -532,8 +532,8 @@ Honors Class
[133]: in_progress.md
[134]: in_progress.md
[135]: in_progress.md
[136]: in_progress.md
[137]: in_progress.md
[136]: queues/btorder.go
[137]: queues/btorder_test.go
[138]: queues/circqueue.go
[139]: queues/circqueue_test.go
[140]: queues/stackqueue.go
Expand Down
42 changes: 42 additions & 0 deletions queues/btorder.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
// Copyright (c) 2015, Peter Mrekaj. All rights reserved.
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE.txt file.

package queues

// IntBTree represents a binary tree with int key.
type IntBTree struct {
Data int
left *IntBTree
right *IntBTree
}

// DepthOrder returns a slice consisting of keys belonging to the
// same level (order is from left to right) of the binary tree t.
// The time complexity is O(n). The O(m) additional space is
// needed, where m is the maximum number of nodes at any level.
func DepthOrder(t *IntBTree) [][]int {
var r [][]int
var l []int
pq := NewArrayQueue(1)
pq.Enqueue(t) // Add root.
c := pq.Len() // Number of elements on the same level.
for pq.Len() != 0 {
n := pq.Dequeue()
c--
if n != (*IntBTree)(nil) {
st := n.(*IntBTree)
pq.Enqueue(st.left)
pq.Enqueue(st.right)
l = append(l, st.Data)
}
if c == 0 {
c = pq.Len() // Set count to the number of elements that should be processed on next level.
if len(l) != 0 {
r = append(r, l)
l = []int(nil)
}
}
}
return r
}
62 changes: 62 additions & 0 deletions queues/btorder_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
// Copyright (c) 2015, Peter Mrekaj. All rights reserved.
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE.txt file.

package queues

import (
"reflect"
"testing"
)

func TestDepthOrder(t *testing.T) {
for _, test := range []struct {
tree *IntBTree
want [][]int
}{
{&IntBTree{1e1, nil, nil}, [][]int{[]int{1e1}}},
{&IntBTree{1e2, &IntBTree{2e2, nil, nil}, nil}, [][]int{[]int{1e2}, []int{2e2}}},
{&IntBTree{1e3, &IntBTree{2e3, &IntBTree{3e3, nil, nil}, nil}, nil}, [][]int{[]int{1e3}, []int{2e3}, []int{3e3}}},
{&IntBTree{1e4,
&IntBTree{2e4,
&IntBTree{3e4,
&IntBTree{4e4, nil, nil},
&IntBTree{5e4, nil, nil}},
&IntBTree{6e4, nil, nil}},
&IntBTree{7e4, nil, nil}}, [][]int{[]int{1e4}, []int{2e4, 7e4}, []int{3e4, 6e4}, []int{4e4, 5e4}}},
{&IntBTree{1e5,
&IntBTree{2e5,
&IntBTree{3e5,
&IntBTree{4e5,
&IntBTree{5e5, nil, nil},
&IntBTree{6e5, nil, nil}},
&IntBTree{7e5, nil, nil}},
&IntBTree{8e5,
&IntBTree{9e5, nil, nil},
nil}},
nil}, [][]int{[]int{1e5}, []int{2e5}, []int{3e5, 8e5}, []int{4e5, 7e5, 9e5}, []int{5e5, 6e5}}},
} {
if got := DepthOrder(test.tree); !reflect.DeepEqual(got, test.want) {
t.Errorf("DepthOrder(%v) = %v; want %v", test.tree, got, test.want)
}
}
}

func BenchmarkDepthOrder(b *testing.B) {
tree := &IntBTree{1e5,
&IntBTree{2e5,
&IntBTree{3e5,
&IntBTree{4e5,
&IntBTree{5e5, nil, nil},
&IntBTree{6e5, nil, nil}},
&IntBTree{7e5, nil, nil}},
&IntBTree{8e5,
&IntBTree{9e5, nil, nil},
nil}},
nil}

b.ResetTimer()
for i := 0; i < b.N; i++ {
DepthOrder(tree)
}
}

0 comments on commit 91c63e9

Please sign in to comment.