Created
August 10, 2013 02:58
-
-
Save charsyam/6198829 to your computer and use it in GitHub Desktop.
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
package main | |
import ( | |
"fmt" | |
"./tree" | |
) | |
func main() { | |
tree := tree.New() | |
tree.Add(4) | |
tree.Add(2) | |
tree.Add(1) | |
tree.Add(3) | |
tree.Add(8) | |
// tree.PreOrder() | |
tree.PostOrder() | |
node := tree.Find(1) | |
fmt.Println(node.GetValue()) | |
} |
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
package tree | |
import ( | |
"fmt" | |
) | |
type Node struct{ | |
left, right *Node | |
value int | |
} | |
func (o *Node) Init(v int) *Node { | |
o.left = nil | |
o.right = nil | |
o.value = v | |
return o | |
} | |
func (o *Node) GetLeft() *Node { | |
return o.left | |
} | |
func (o *Node) GetRight() *Node { | |
return o.right | |
} | |
func (o *Node) GetValue() int { | |
return o.value | |
} | |
func (o *Node) SetLeft(left *Node) { | |
o.left = left | |
} | |
func (o *Node) SetRight(right *Node) { | |
o.right = right | |
} | |
func (o *Node) SetValue(v int) { | |
o.value = v; | |
} | |
type Tree struct { | |
root *Node | |
} | |
func New() *Tree { | |
return new(Tree).Init() | |
} | |
func (o *Tree) Init() *Tree { | |
o.root = nil | |
return o | |
} | |
func (o *Tree) Find(v int) *Node { | |
return o._FindNode(o.root, v) | |
} | |
func (o *Tree) _FindNode(node *Node, v int) *Node { | |
if node == nil { | |
return nil | |
} | |
if node.GetValue() == v { | |
return node | |
} | |
if node.GetValue() < v { | |
return o._FindNode(node.GetRight(), v) | |
} else { | |
return o._FindNode(node.GetLeft(), v) | |
} | |
} | |
func (o *Tree) Add(v int) { | |
newNode := new(Node).Init(v) | |
if o.root == nil { | |
o.root = newNode | |
} else { | |
o.AddNode(o.root, newNode) | |
} | |
} | |
func (o *Tree) AddNode(parent *Node, newNode *Node) { | |
if parent.GetValue() <= newNode.GetValue() { | |
if parent.GetRight() == nil { | |
parent.SetRight(newNode) | |
} else { | |
o.AddNode(parent.GetRight(), newNode) | |
} | |
} else { | |
if parent.GetLeft() == nil { | |
parent.SetLeft(newNode) | |
} else { | |
o.AddNode(parent.GetLeft(), newNode) | |
} | |
} | |
} | |
func (o *Tree) PreOrder() { | |
o._PreOrder(o.root) | |
} | |
func (o *Tree) _PreOrder(node *Node) { | |
if node != nil { | |
o._PreOrder(node.GetLeft()) | |
fmt.Println(node.GetValue()) | |
o._PreOrder(node.GetRight()) | |
} | |
} | |
func (o *Tree) PostOrder() { | |
o._PostOrder(o.root) | |
} | |
func (o *Tree) _PostOrder(node *Node) { | |
if node != nil { | |
o._PostOrder(node.GetLeft()) | |
o._PostOrder(node.GetRight()) | |
fmt.Println(node.GetValue()) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment