Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add problem 6 of chapter 4 #8

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions src/chapter4/binary_tree_node.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package chapter4

// BTNode represents a node in binary tree
type BTNode struct {
Value int
Parent *BTNode
Left *BTNode
Right *BTNode
}

// SetRight sets child on right side
func (n *BTNode) SetRight(c *BTNode) {
n.Right = c
c.Parent = n
}

// SetLeft sets child on right side
func (n *BTNode) SetLeft(c *BTNode) {
n.Left = c
c.Parent = n
}
26 changes: 26 additions & 0 deletions src/chapter4/problem6.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package chapter4

// Successor checks the next node in binary tree with in-order traversal
func Successor(node *BTNode) *BTNode {
if node == nil {
return nil
}

// node has right child
if node.Right != nil {
for n := node.Right; ; n = n.Left {
if n.Left == nil {
return n
}
}
}

// node is right child of Parent
for ; node.Parent != nil; node = node.Parent {
if node.Parent.Left == node {
return node.Parent
}
}

return nil
}
88 changes: 88 additions & 0 deletions src/chapter4/problem6_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
package chapter4

import (
"reflect"
"testing"
)

func TestSuccessor(t *testing.T) {
// Initiate Node for parent traversal
n1 := &BTNode{}
n2 := &BTNode{}
n3 := &BTNode{}
n2.SetRight(n1)
n3.SetLeft(n2)

n4 := &BTNode{}
n5 := &BTNode{}
n6 := &BTNode{}
n5.SetRight(n4)
n6.SetRight(n5)

tests := map[string]struct {
in *BTNode
out *BTNode
}{

"Should return nil if input is nil": {
in: nil,
out: nil,
},

"Should return nil if input doesn't have parent and children": {
in: &BTNode{},
out: nil,
},

"Should return most left node of right subtree": {
in: &BTNode{
Right: &BTNode{
Left: &BTNode{
Value: 5,
Left: &BTNode{
Value: 10,
Right: &BTNode{},
},
},
},
},
out: &BTNode{
Value: 10,
Right: &BTNode{},
},
},

/*
* n3
* /
* n2
* \
* n1
*/
"Should return parent which has left child": {
in: n1,
out: n3,
},

/*
* n6
* \
* n5
* \
* n4
*/
"Should return nil whne input is the last node to traversal": {
in: n4,
out: nil,
},
}

for k, test := range tests {
t.Run(k, func(t *testing.T) {
out := Successor(test.in)
if !reflect.DeepEqual(out, test.out) {
t.Errorf("actual=%+v expected=%+v", out, test.out)
}
})
}
}