added binary-trees

master
Furkan Türkal 6 years ago
parent e20a8336f0
commit e0428744e4

@ -0,0 +1,94 @@
// ====================================================
// 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)
}
//Function to search a given key in a given BST
func Search(root *Node, key int) *Node {
//1. Base Cases: root is null or key is present at root
if root == nil || root.data == key {
fmt.Println("The given previous node cannot be NULL")
return root
}
//2. Key is greater than root's key
if root.data < key {
return Search(root.right, key)
}
//3. Key is smaller than root's key
return Search(root.left, key)
}
//A utility function to do inorder traversal of BST
func PrintInOrder(root *Node) {
if root != nil {
PrintInOrder(root.left)
fmt.Printf("%d \n", root.data)
PrintInOrder(root.right)
}
}
//A utility function to insert a new node with given key in BST
func Insert(node *Node, key int) *Node {
//1. If the tree is empty, return a new node
if node == nil {
return New(key)
}
//2. Otherwise, recur down the tree
if key < node.data {
node.left = Insert(node.left, key)
} else if key > node.data {
node.right = Insert(node.right, key)
}
//3. Return the (unchanged) node pointer
return node
}
func main() {
/* Let us create following BST
              50
           /     \
          30      70
         /  \    /  \
       20   40  60   80 */
//To allocate dynamically a new Node in C language : root = (struct Node*) malloc(sizeof(struct Node));
root := New(50)
Insert(root, 30)
Insert(root, 20)
Insert(root, 40)
Insert(root, 70)
Insert(root, 60)
Insert(root, 80)
//Print inoder traversal of the BST
PrintInOrder(root)
}

@ -0,0 +1,66 @@
// ====================================================
// 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 main() {
//To allocate dynamically a new Node in C language : root = (struct Node*) malloc(sizeof(struct Node));
root := New(1)
/*
        1
      /   \
     NULL  NULL
*/
root.left = New(2)
root.right = New(3)
/* 2 and 3 become left and right children of 1
           1
         /   \
        2      3
     /    \    /  \
    NULL NULL NULL NULL
*/
root.left.left = New(4)
/* 4 becomes left child of 2
           1
       /       \
      2          3
    /   \       /  \
   4    NULL  NULL  NULL
  /  \
NULL NULL
*/
fmt.Println("Root data : ", root.data)
fmt.Println("Root->Left data : ", root.left.data)
fmt.Println("Root->Right data : ", root.right.data)
fmt.Println("Root->Left->Left data : ", root.left.left.data)
}

@ -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,94 @@
// ====================================================
// 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)
}
/* Compute the "height" of a tree -- the number of
    nodes along the longest path from the root node
    down to the farthest leaf node.*/
func GetHeight(node *Node) int {
//1. Check if the given node is NULL
if node == nil {
return 0
}
//2. Compute the height of each subtree
lheight := GetHeight(node.left)
rheight := GetHeight(node.right)
//3. Use the larger one
if lheight > rheight {
return lheight + 1
}
return rheight + 1
}
/* Print nodes at a given level */
func PrintGivenLevel(root *Node, level int) {
//1. Check if the given root is NULL
if root == nil {
return
}
if level == 1 {
fmt.Printf("%d ", root.data)
} else if level > 1 {
PrintGivenLevel(root.left, level-1)
PrintGivenLevel(root.right, level-1)
}
}
/* Function to print level order traversal a tree*/
func PrintLevelOrder(root *Node) {
//1. Check if the given root is NULL
if root == nil {
fmt.Println("The given root node cannot be NULL")
return
}
h := GetHeight(root)
for i := 1; i <= h; i++ {
PrintGivenLevel(root, i)
}
}
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("\nLevel Order traversal of binary tree is :")
PrintLevelOrder(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,65 @@
// ====================================================
// 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)
}
/* Compute the "height" of a tree -- the number of
    nodes along the longest path from the root node
    down to the farthest leaf node.*/
func DeleteTree(node *Node) {
//1. Check if the given node is NULL
if node == nil {
return
}
//2. First delete both subtrees
DeleteTree(node.left)
DeleteTree(node.right)
//3. Then delete the node
fmt.Printf("\nDeleting node: %d", node.data)
//free(node);
node = nil
}
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)
DeleteTree(root)
root = nil
fmt.Println("\nTree deleted")
}

@ -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…
Cancel
Save