Skip to content

Commit

Permalink
Merge pull request TheAlgorithms#19 from himanshub16/datastructures/b…
Browse files Browse the repository at this point in the history
…inarytree

Add binary tree and binary search tree
  • Loading branch information
dynamitechetan authored Oct 11, 2017
2 parents dfcd181 + b83171a commit 02bdb71
Show file tree
Hide file tree
Showing 2 changed files with 232 additions and 0 deletions.
122 changes: 122 additions & 0 deletions data-structures/binary-tree/binary-search-tree.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
// Binary search tree
// https://en.wikipedia.org/wiki/Binary_search_tree

package binarySearchTree

// package main

import "fmt"

type node struct {
val int
left *node
right *node
}

type btree struct {
root *node
}

func max(a, b int) int {
if a > b {
return a
}
return b
}

func newNode(val int) *node {
return &node{val, nil, nil}
}

func inorder(n *node) {
if n != nil {
inorder(n.left)
fmt.Print(n.val, " ")
inorder(n.right)
}
}

func insert(root *node, val int) *node {
if root == nil {
return newNode(val)
}
if val < root.val {
root.left = insert(root.left, val)
} else {
root.right = insert(root.right, val)
}
return root
}

func inorderSuccessor(root *node) *node {
cur := root
for cur.left != nil {
cur = cur.left
}
return cur
}

func bst_delete(root *node, val int) *node {
if root == nil {
return nil
}
if val < root.val {
root.left = bst_delete(root.left, val)
} else if val > root.val {
root.right = bst_delete(root.right, val)
} else {
// this is the node to delete

// node with one child
if root.left == nil {
root = root.right
} else if root.right == nil {
root = root.left
} else {
// node with two children
d := inorderSuccessor(root)
root.val = d.val
root.right = bst_delete(root.right, d.val)
}
}
return root
}

// helper function for t.depth
func _calculate_depth(n *node, depth int) int {
if n == nil {
return depth
}
return max(_calculate_depth(n.left, depth+1), _calculate_depth(n.right, depth+1))
}

func (t *btree) depth() int {
return _calculate_depth(t.root, 0)
}

/*
func main() {
t := &btree{nil}
inorder(t.root)
t.root = insert(t.root, 10)
t.root = insert(t.root, 20)
t.root = insert(t.root, 15)
t.root = insert(t.root, 30)
fmt.Print(t.depth(), "\n")
inorder(t.root)
fmt.Print("\n")
t.root = bst_delete(t.root, 10)
inorder(t.root)
fmt.Print("\n")
t.root = bst_delete(t.root, 30)
inorder(t.root)
fmt.Print("\n")
t.root = bst_delete(t.root, 15)
inorder(t.root)
fmt.Print("\n")
t.root = bst_delete(t.root, 20)
inorder(t.root)
fmt.Print("\n")
fmt.Print(t.depth(), "\n")
}
*/
110 changes: 110 additions & 0 deletions data-structures/binary-tree/binary-tree.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
// basic binary tree and related operations

package binarytree

// package main

import "fmt"

type node struct {
val int
left *node
right *node
}

type btree struct {
root *node
}

func max(a, b int) int {
if a > b {
return a
}
return b
}

func newNode(val int) *node {
n := &node{val, nil, nil}
return n
}

func inorder(n *node) {
if n == nil {
return
}
inorder(n.left)
fmt.Print(n.val, " ")
inorder(n.right)
}

func preorder(n *node) {
if n == nil {
return
}
fmt.Print(n.val, " ")
inorder(n.left)
inorder(n.right)
}

func postorder(n *node) {
if n == nil {
return
}
inorder(n.left)
inorder(n.right)
fmt.Print(n.val, " ")
}

func levelorder(root *node) {
var q []*node // queue
var n *node // temporary node

q = append(q, root)

for len(q) != 0 {
n, q = q[0], q[1:]
fmt.Print(n.val, " ")
if n.left != nil {
q = append(q, n.left)
}
if n.right != nil {
q = append(q, n.right)
}
}
}

// helper function for t.depth
func _calculate_depth(n *node, depth int) int {
if n == nil {
return depth
}
return max(_calculate_depth(n.left, depth+1), _calculate_depth(n.right, depth+1))
}

func (t *btree) depth() int {
return _calculate_depth(t.root, 0)
}

/*
func main() {
t := btree{nil}
t.root = newNode(0)
t.root.left = newNode(1)
t.root.right = newNode(2)
t.root.left.left = newNode(3)
t.root.left.right = newNode(4)
t.root.right.left = newNode(5)
t.root.right.right = newNode(6)
t.root.right.right.right = newNode(10)
inorder(t.root)
fmt.Print("\n")
preorder(t.root)
fmt.Print("\n")
postorder(t.root)
fmt.Print("\n")
levelorder(t.root)
fmt.Print("\n")
fmt.Print(t.depth(), "\n")
}
*/

0 comments on commit 02bdb71

Please sign in to comment.