diff --git a/README.md b/README.md index 2365712..dfb41d5 100644 --- a/README.md +++ b/README.md @@ -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] | | @@ -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 diff --git a/queues/btorder.go b/queues/btorder.go new file mode 100644 index 0000000..25e56c5 --- /dev/null +++ b/queues/btorder.go @@ -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 +} diff --git a/queues/btorder_test.go b/queues/btorder_test.go new file mode 100644 index 0000000..ee1a65c --- /dev/null +++ b/queues/btorder_test.go @@ -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) + } +}