Find top three elements of binary tree in golang

Go program for Find top three elements of binary tree. Here mentioned other language solution.
package main
import "fmt"
/*
Go program for
Find top three elements in binary tree
*/
// Node of Binary Tree
type TreeNode struct {
data int
left * TreeNode
right * TreeNode
}
func getTreeNode(data int) * TreeNode {
// return new TreeNode
return &TreeNode {
data,
nil,
nil,
}
}
type BinaryTree struct {
root * TreeNode
first * TreeNode
second * TreeNode
third * TreeNode
}
func getBinaryTree() * BinaryTree {
// return new BinaryTree
return &BinaryTree {
nil,
nil,
nil,
nil,
}
}
// Recursive function
// Display preorder view of binary tree
func(this BinaryTree) preOrder(node * TreeNode) {
if node != nil {
//Print node value
fmt.Print(" ", node.data)
this.preOrder(node.left)
this.preOrder(node.right)
}
}
// Find top three largest nodes in binary tree
func(this *BinaryTree) getTopThree(node * TreeNode) {
if node != nil {
if this.first == nil {
// First node of tree
this.first = node
} else if this.first.data < node.data {
// When get new first largest node
// Interchange the node values
this.third = this.second
this.second = this.first
this.first = node
} else if this.second == nil {
if this.first.data != node.data {
// Get second largest node
this.second = node
}
} else {
if this.second.data < node.data &&
this.first.data > node.data {
// When we get new second largest
// Replace the third node with the second node
this.third = this.second
// This is new second largest element
this.second = node
} else if this.third == nil {
if this.second.data > node.data &&
this.first.data > node.data {
// Get the third largest node
this.third = node
}
} else if this.third.data < node.data &&
this.first.data > node.data &&
this.second.data > node.data {
this.third = node
}
}
// Recursively, execute left and right subtree
this.getTopThree(node.left)
this.getTopThree(node.right)
}
}
// This are handle the request to finding
// top three nodes in binary tree.
func(this *BinaryTree) printTopThree() {
if this.root == nil {
return
}
// Display tree elements
fmt.Print("\n Preorder : ")
this.preOrder(this.root)
// Set the initial all three nodes
this.first = nil
this.second = nil
this.third = nil
// Find three largest element
this.getTopThree(this.root)
// Display calculated result
fmt.Println("\n First : ", this.first.data)
if this.second != nil && this.second != this.first &&
this.second.data < this.first.data {
fmt.Println(" Second : ", this.second.data)
} else {
fmt.Println(" Second : None ")
}
if this.third != nil && this.third != this.second &&
this.third != this.first && this.third.data < this.second.data {
fmt.Println(" Third : ", this.third.data)
} else {
fmt.Println(" Third : None")
}
}
func main() {
// Create two trees
var x * BinaryTree = getBinaryTree()
var y * BinaryTree = getBinaryTree()
/*
Construct Binary Tree
-----------------------
10
/ \
/ \
6 8
/ \ / \
12 3 7 5
/ \ \
9 -6 13
*/
// Add nodes
x.root = getTreeNode(10)
x.root.left = getTreeNode(6)
x.root.left.left = getTreeNode(12)
x.root.right = getTreeNode(8)
x.root.right.right = getTreeNode(5)
x.root.right.left = getTreeNode(7)
x.root.right.left.right = getTreeNode(-6)
x.root.left.right = getTreeNode(3)
x.root.left.left.left = getTreeNode(9)
x.root.right.right.right = getTreeNode(13)
/*
Construct Tree
--------------
1
/ \
/ \
1 2
/ / \
1 2 2
*/
// Add second tree node
y.root = getTreeNode(1)
y.root.left = getTreeNode(1)
y.root.right = getTreeNode(2)
y.root.right.right = getTreeNode(2)
y.root.right.left = getTreeNode(2)
y.root.left.left = getTreeNode(1)
// Test One
x.printTopThree()
// Test Two
y.printTopThree()
}
Output
Preorder : 10 6 12 9 3 8 7 -6 5 13
First : 13
Second : 12
Third : 10
Preorder : 1 1 1 2 2 2
First : 2
Second : 1
Third : None
Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.
New Comment