forked from TheAlgorithms/Go
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request TheAlgorithms#19 from himanshub16/datastructures/b…
…inarytree Add binary tree and binary search tree
- Loading branch information
Showing
2 changed files
with
232 additions
and
0 deletions.
There are no files selected for viewing
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
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") | ||
} | ||
*/ |
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
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") | ||
} | ||
*/ |