|
| 1 | +/* |
| 2 | +Implementation of a binary search tree operations (Insert, Search, Delete) |
| 3 | +*/ |
| 4 | + |
| 5 | +package main |
| 6 | + |
| 7 | +import ("fmt" |
| 8 | +//"container/list" |
| 9 | +) |
| 10 | + |
| 11 | +type Element struct { |
| 12 | + |
| 13 | +// The value in stored in the element |
| 14 | +Value interface{} |
| 15 | +left, right *Element |
| 16 | + |
| 17 | +// The binary search tree to which the element belongs |
| 18 | +bst *Bst |
| 19 | +} |
| 20 | + |
| 21 | +type Bst struct { |
| 22 | +root Element |
| 23 | +} |
| 24 | + |
| 25 | +// Creates a new Bst Element and initializes it |
| 26 | +func New() *Bst { |
| 27 | +return new(Bst).Init() |
| 28 | +} |
| 29 | + |
| 30 | +func (b *Bst) Init() *Bst { |
| 31 | +b.root.right = nil |
| 32 | +b.root.left = nil |
| 33 | +return b |
| 34 | +} |
| 35 | + |
| 36 | +func (b *Bst) Insert(val interface{}) { |
| 37 | +temp := &b.root |
| 38 | +for true { |
| 39 | +if (temp.Value == nil) { |
| 40 | +temp.Value = val |
| 41 | +temp.bst = b |
| 42 | +return |
| 43 | +} else if (val.(int) >= temp.Value.(int) && temp.right == nil) { |
| 44 | +temp.right = new(Element) |
| 45 | +temp.right.Value = val |
| 46 | +temp.right.bst = b |
| 47 | +return |
| 48 | +} else if (val.(int) < temp.Value.(int) && temp.left == nil) { |
| 49 | +temp.left = new(Element) |
| 50 | +temp.left.Value = val |
| 51 | +temp.left.bst = b |
| 52 | +return |
| 53 | +} else if (val.(int) >= temp.Value.(int) && temp.right != nil) { |
| 54 | +temp = temp.right |
| 55 | +} else { |
| 56 | +temp = temp.left |
| 57 | +} |
| 58 | +} |
| 59 | +} |
| 60 | + |
| 61 | +// Returns if a value exists in the Binary Search Tree |
| 62 | +func (b *Bst) Search(val interface{}) bool { |
| 63 | +temp := &b.root |
| 64 | +for true { |
| 65 | +if (temp.Value.(int) == val.(int)) { |
| 66 | +return true |
| 67 | +} else if (val.(int) >= temp.Value.(int) && temp.right != nil) { |
| 68 | +temp = temp.right |
| 69 | +} else if (val.(int) < temp.Value.(int) && temp.left != nil) { |
| 70 | +temp = temp.left |
| 71 | +} else { |
| 72 | +return false |
| 73 | +} |
| 74 | +} |
| 75 | +return false |
| 76 | +} |
| 77 | + |
| 78 | +func (b *Bst) Remove(val interface{}) { |
| 79 | + |
| 80 | +} |
| 81 | + |
| 82 | +// Inorder Traversal (Left, Root, Right) |
| 83 | +func (b *Bst) Inorder(root *Element) { |
| 84 | +if (root == nil) { |
| 85 | +return |
| 86 | +} |
| 87 | +b.Inorder(root.left) |
| 88 | +fmt.Println(root.Value) |
| 89 | +b.Inorder(root.right) |
| 90 | +} |
| 91 | + |
| 92 | +func (b *Bst) Preorder(root *Element) { |
| 93 | +if (root == nil) { |
| 94 | +return |
| 95 | +} |
| 96 | +fmt.Println(root.Value) |
| 97 | +b.Preorder(root.left) |
| 98 | +b.Preorder(root.right) |
| 99 | +} |
| 100 | + |
| 101 | +func (b *Bst) Postorder(root *Element) { |
| 102 | +if (root == nil) { |
| 103 | +return |
| 104 | +} |
| 105 | +b.Postorder(root.left) |
| 106 | +b.Postorder(root.right) |
| 107 | +fmt.Println(root.Value) |
| 108 | +} |
| 109 | + |
| 110 | +func (b *Bst) Levelorder() { |
| 111 | + |
| 112 | +} |
| 113 | + |
| 114 | +/* |
| 115 | +func ArrayToBST(array []int) { |
| 116 | +
|
| 117 | +} |
| 118 | +*/ |
| 119 | + |
| 120 | +func main() { |
| 121 | +// Instantiate a random BST with the root element |
| 122 | +array := []int{5,10,12,7,6,21,35,45,22,100} |
| 123 | +b := New() |
| 124 | +for _, val := range array { |
| 125 | +b.Insert(val) |
| 126 | +} |
| 127 | +fmt.Println(b.Search(7)) |
| 128 | +b.Inorder(&b.root) |
| 129 | +b.Preorder(&b.root) |
| 130 | +b.Postorder(&b.root) |
| 131 | +//fmt.Println(b.root) |
| 132 | +//fmt.Println(b.root.right) |
| 133 | +} |
0 commit comments