-
Notifications
You must be signed in to change notification settings - Fork 96
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Solve the "Compute binary tree nodes in order of increasing depth" pr…
…oblem
- Loading branch information
Showing
3 changed files
with
107 additions
and
3 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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,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 | ||
} |
This file contains 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,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) | ||
} | ||
} |