added binary-trees
parent
e20a8336f0
commit
e0428744e4
@ -0,0 +1,96 @@
|
||||
// ====================================================
|
||||
// Data-Structures-with-Go Copyright(C) 2017 Furkan Türkal
|
||||
// This program comes with ABSOLUTELY NO WARRANTY; This is free software,
|
||||
// and you are welcome to redistribute it under certain conditions; See
|
||||
// file LICENSE, which is part of this source code package, for details.
|
||||
// ====================================================
|
||||
|
||||
package main
|
||||
|
||||
import "fmt"
|
||||
|
||||
type Node struct {
|
||||
data int
|
||||
left *Node
|
||||
right *Node
|
||||
}
|
||||
|
||||
//Returns an initialized list
|
||||
func (n *Node) Init(data int) *Node {
|
||||
n.data = data
|
||||
n.left = nil
|
||||
n.right = nil
|
||||
return n
|
||||
}
|
||||
|
||||
//Returns an new list
|
||||
func New(data int) *Node {
|
||||
return new(Node).Init(data)
|
||||
}
|
||||
|
||||
func PrintPostOrder(node *Node) {
|
||||
//1. Check if the given node is NULL
|
||||
if node == nil {
|
||||
return
|
||||
}
|
||||
|
||||
//2. First recur on left subtree
|
||||
PrintPostOrder(node.left)
|
||||
|
||||
//3. Then recur on right subtree
|
||||
PrintPostOrder(node.right)
|
||||
|
||||
//4. Now deal with the node
|
||||
fmt.Printf("%d ", node.data)
|
||||
}
|
||||
|
||||
func PrintInOrder(node *Node) {
|
||||
//1. Check if the given node is NULL
|
||||
if node == nil {
|
||||
return
|
||||
}
|
||||
|
||||
//2. First recur on left child
|
||||
PrintInOrder(node.left)
|
||||
|
||||
//3. Then print the data of node
|
||||
fmt.Printf("%d ", node.data)
|
||||
|
||||
//4. Now recur on right child
|
||||
PrintInOrder(node.right)
|
||||
}
|
||||
|
||||
func PrintPreOrder(node *Node) {
|
||||
//1. Check if the given node is NULL
|
||||
if node == nil {
|
||||
return
|
||||
}
|
||||
|
||||
//2. First print data of node
|
||||
fmt.Printf("%d ", node.data)
|
||||
|
||||
//3. Then recur on left sutree
|
||||
PrintPreOrder(node.left)
|
||||
|
||||
//4. Now recur on right subtree
|
||||
PrintPreOrder(node.right)
|
||||
}
|
||||
|
||||
func main() {
|
||||
|
||||
//To allocate dynamically a new Node in C language : root = (struct Node*) malloc(sizeof(struct Node));
|
||||
root := New(1)
|
||||
|
||||
root.left = New(2)
|
||||
root.right = New(3)
|
||||
|
||||
root.left.left = New(4)
|
||||
root.left.right = New(5)
|
||||
|
||||
fmt.Println("\nPreorder traversal of binary tree is :")
|
||||
PrintPreOrder(root)
|
||||
fmt.Println("\nInorder traversal of binary tree is :")
|
||||
PrintInOrder(root)
|
||||
fmt.Println("\nPostorder traversal of binary tree is :")
|
||||
PrintPostOrder(root)
|
||||
}
|
@ -0,0 +1,115 @@
|
||||
// ====================================================
|
||||
// Data-Structures-with-Go Copyright(C) 2017 Furkan Türkal
|
||||
// This program comes with ABSOLUTELY NO WARRANTY; This is free software,
|
||||
// and you are welcome to redistribute it under certain conditions; See
|
||||
// file LICENSE, which is part of this source code package, for details.
|
||||
// ====================================================
|
||||
|
||||
package main
|
||||
|
||||
import "fmt"
|
||||
|
||||
type Node struct {
|
||||
data int
|
||||
left *Node
|
||||
right *Node
|
||||
}
|
||||
|
||||
//Returns an initialized list
|
||||
func (n *Node) Init(data int) *Node {
|
||||
n.data = data
|
||||
n.left = nil
|
||||
n.right = nil
|
||||
return n
|
||||
}
|
||||
|
||||
//Returns an new list
|
||||
func New(data int) *Node {
|
||||
return new(Node).Init(data)
|
||||
}
|
||||
|
||||
// Standard Inorder traversal of tree
|
||||
func InOrder(root *Node) {
|
||||
//1. Check if the given node is NULL
|
||||
if root == nil {
|
||||
return
|
||||
}
|
||||
|
||||
InOrder(root.left)
|
||||
fmt.Printf("%d ", root.data)
|
||||
InOrder(root.right)
|
||||
}
|
||||
|
||||
// Changes left pointers to work as previous pointers in converted DLL
|
||||
// The function simply does inorder traversal of Binary Tree and updates
|
||||
// left pointer using previously visited node
|
||||
var pre *Node = nil
|
||||
|
||||
func FixPrevPtr(root *Node) {
|
||||
//1. Check if the given root is NULL
|
||||
if root == nil {
|
||||
return
|
||||
}
|
||||
|
||||
FixPrevPtr(root.left)
|
||||
root.left = pre
|
||||
pre = root
|
||||
FixPrevPtr(root.right)
|
||||
}
|
||||
|
||||
// Changes right pointers to work as next pointers in converted DLL
|
||||
var prev *Node = nil
|
||||
|
||||
func FixNextPtr(root *Node) *Node {
|
||||
// Find the right most node in BT or last node in DLL
|
||||
for root != nil && root.right != nil {
|
||||
root = root.right
|
||||
}
|
||||
|
||||
// Start from the rightmost node, traverse back using left pointers.
|
||||
// While traversing, change right pointer of nodes.
|
||||
for root != nil && root.left != nil {
|
||||
prev = root
|
||||
root = root.left
|
||||
root.right = prev
|
||||
}
|
||||
|
||||
// The leftmost node is head of linked list, return it
|
||||
return root
|
||||
}
|
||||
|
||||
func BTToDLL(root *Node) *Node {
|
||||
// Set the previous pointer
|
||||
FixPrevPtr(root)
|
||||
|
||||
// Set the next pointer and return head of DLL
|
||||
return FixNextPtr(root)
|
||||
}
|
||||
|
||||
func PrintList(root *Node) {
|
||||
for root != nil {
|
||||
fmt.Printf("%d ", root.data)
|
||||
root = root.right
|
||||
}
|
||||
}
|
||||
|
||||
func main() {
|
||||
|
||||
//To allocate dynamically a new Node in C language : root = (struct Node*) malloc(sizeof(struct Node));
|
||||
root := New(10)
|
||||
|
||||
root.left = New(12)
|
||||
root.right = New(15)
|
||||
|
||||
root.left.left = New(25)
|
||||
root.left.right = New(30)
|
||||
|
||||
root.right.left = New(36)
|
||||
|
||||
fmt.Println("\nInorder Tree Traversal")
|
||||
InOrder(root)
|
||||
|
||||
head := BTToDLL(root)
|
||||
fmt.Println("\nDLL Traversal")
|
||||
PrintList(head)
|
||||
}
|
@ -0,0 +1,98 @@
|
||||
// ====================================================
|
||||
// Data-Structures-with-Go Copyright(C) 2017 Furkan Türkal
|
||||
// This program comes with ABSOLUTELY NO WARRANTY; This is free software,
|
||||
// and you are welcome to redistribute it under certain conditions; See
|
||||
// file LICENSE, which is part of this source code package, for details.
|
||||
// ====================================================
|
||||
|
||||
package main
|
||||
|
||||
import "fmt"
|
||||
|
||||
const MaxUint = ^uint(0)
|
||||
const MinUint = 0
|
||||
const MaxInt = int(MaxUint >> 1)
|
||||
const MinInt = -MaxInt - 1
|
||||
|
||||
type Node struct {
|
||||
data int
|
||||
left *Node
|
||||
right *Node
|
||||
}
|
||||
|
||||
//Returns an initialized list
|
||||
func (n *Node) Init(data int) *Node {
|
||||
n.data = data
|
||||
n.left = nil
|
||||
n.right = nil
|
||||
return n
|
||||
}
|
||||
|
||||
//Returns an new list
|
||||
func New(data int) *Node {
|
||||
return new(Node).Init(data)
|
||||
}
|
||||
|
||||
// Returns minimum value in a given Binary Tree
|
||||
func FindMin(root *Node) int {
|
||||
//1. Check if the given node is NULL
|
||||
if root == nil {
|
||||
return MaxInt
|
||||
}
|
||||
|
||||
//2. Return maximum of 3 values: 1) Root's data 2) Max in Left Subtree 3) Max in right subtree
|
||||
res := root.data
|
||||
lres := FindMin(root.left)
|
||||
rres := FindMin(root.right)
|
||||
|
||||
if lres < res {
|
||||
res = lres
|
||||
}
|
||||
if rres < res {
|
||||
res = rres
|
||||
}
|
||||
|
||||
return res
|
||||
}
|
||||
|
||||
// Returns maximum value in a given Binary Tree
|
||||
func FindMax(root *Node) int {
|
||||
//1. Check if the given node is NULL
|
||||
if root == nil {
|
||||
return MinInt
|
||||
}
|
||||
|
||||
//2. Return maximum of 3 values: 1) Root's data 2) Max in Left Subtree 3) Max in right subtree
|
||||
res := root.data
|
||||
lres := FindMax(root.left)
|
||||
rres := FindMax(root.right)
|
||||
|
||||
if lres > res {
|
||||
res = lres
|
||||
}
|
||||
if rres > res {
|
||||
res = rres
|
||||
}
|
||||
|
||||
return res
|
||||
}
|
||||
|
||||
func main() {
|
||||
|
||||
//To allocate dynamically a new Node in C language : root = (struct Node*) malloc(sizeof(struct Node));
|
||||
root := New(2)
|
||||
|
||||
root.left = New(7)
|
||||
root.right = New(5)
|
||||
|
||||
root.left.right = New(6)
|
||||
root.right.right = New(9)
|
||||
|
||||
root.left.right.left = New(1)
|
||||
root.left.right.right = New(11)
|
||||
|
||||
root.right.right.left = New(1)
|
||||
|
||||
fmt.Printf("\nMaximum element is %d", FindMax(root))
|
||||
fmt.Printf("\nMinimum element is %d", FindMin(root))
|
||||
}
|
Loading…
Reference in New Issue