NicholasAdamou

Binary Search Tree (Go)

Oct 7th, 2019
349
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Go 4.19 KB | None | 0 0
  1. package main
  2.  
  3. /**
  4.  * Project 1: programming in Go lang
  5.  * Using Go Lang, write a program that:
  6.  *    - fills an array with random integers in the range 10 to 99.
  7.  *    - builds a binary search tree by inserting the integers in the array into a tree.
  8.  *    - finds the length of a given BST.
  9.  *    - finds the number of leaf nodes.
  10.  *    - finds the minimum value in a given BST.
  11.  *    - prints the BST in 2D in reverse in-order traversal.
  12.  *    - traverses the tree with pre-order, in-order, and post-order algorithms, printing each value in the tree along the way.
  13.  *
  14.  * author: Nicholas Adamou
  15.  * date: 10/7/19
  16.  * Class: CSC-315
  17.  */
  18.  
  19. import (
  20.     "fmt"
  21.     "math/rand"
  22. )
  23.  
  24. type Node struct {
  25.     value int
  26.     left *Node
  27.     right *Node
  28. }
  29.  
  30. func insert(root *Node, value int) *Node {
  31.     // searching for the place to insert
  32.  
  33.     if root == nil {
  34.         root = &Node{value, nil, nil}
  35.     } else if value < root.value {               // x is greater. Should be inserted to right
  36.         root.left = insert(root.left, value)
  37.     } else {                                    // x is smaller. Should be inserted to left
  38.         root.right = insert(root.right, value)
  39.     }
  40.  
  41.     return root
  42. }
  43.  
  44. // function to find the length of a given BST
  45. func length(root *Node) (count int) {
  46.     if root == nil {
  47.         return 0
  48.     } else {
  49.         return 1 + length(root.left) + length(root.right)
  50.     }
  51. }
  52.  
  53. // function to find the total number of leaf nodes in a given BST
  54. func getNumberOfLeafNodes(root *Node) (count int) {
  55.     if root == nil {
  56.         return 0
  57.     }
  58.  
  59.     if root.left == nil && root.right == nil {
  60.         return 1
  61.     } else {
  62.         return getNumberOfLeafNodes(root.left) + getNumberOfLeafNodes(root.right)
  63.     }
  64. }
  65.  
  66. // function to find the minimum value in a node
  67. func findMinimum(root *Node) *Node {
  68.     if root == nil {
  69.         return nil
  70.     } else if root.left != nil {                // node with minimum value will have no left child
  71.         return findMinimum(root.left)           // left most element will be minimum
  72.     }
  73.  
  74.     return root
  75. }
  76.  
  77. // Function to print binary tree in 2D
  78. // It does reverse in-order traversal
  79. func print2DUtil(root *Node, depth int) {
  80.     const COUNT = 4                             // default number of spaces
  81.  
  82.     if root == nil {                            // base case
  83.         return
  84.     }
  85.  
  86.     depth += COUNT                              // increase distance between levels
  87.  
  88.     print2DUtil(root.right, depth)              // process right child first
  89.  
  90.     // print current Node after space count
  91.     fmt.Printf("\n")
  92.     for i := COUNT; i < depth; i++ {
  93.         fmt.Printf(" ")
  94.     }
  95.     fmt.Printf("%d\n", root.value)
  96.  
  97.     print2DUtil(root.left, depth)               // process left child last
  98. }
  99.  
  100. func print2D(root *Node) {
  101.     // pass initial depth as 0
  102.     print2DUtil(root, 0)
  103. }
  104.  
  105. func preorder(root *Node) {
  106.     if root != nil {                            // checking if the root is not null
  107.         fmt.Printf(" %d ", root.value)          // printing data at root
  108.         preorder(root.left)                     // visiting left child
  109.         preorder(root.right)                    // visiting right child
  110.     }
  111. }
  112.  
  113. func inorder(root *Node) {
  114.     if root != nil {                            // checking if the root is not null
  115.         inorder(root.left)                      // visiting left child
  116.         fmt.Printf(" %d ", root.value)          // printing data at root
  117.         inorder(root.right)                     // visiting right child
  118.     }
  119. }
  120.  
  121. func postorder(root *Node) {
  122.     if root != nil {                            // checking if the root is not null
  123.         postorder(root.left)                    // visiting left child
  124.         postorder(root.right)                   // visiting right child
  125.         fmt.Printf(" %d ", root.value)          // printing data at root
  126.     }
  127. }
  128.  
  129. func main() {
  130.     var n int                                   // random number
  131.  
  132.     const MIN = 10                              // min number a given node can hold
  133.     const MAX = 99                              // max number a given node can hold
  134.     const SIZE = 10                             // number of nodes in the BST
  135.  
  136.     var root *Node = &Node{rand.Int() % (MAX - MIN + 1) + MIN, nil, nil}
  137.  
  138.     for i:= 0; i < SIZE; i++ {
  139.         n = rand.Int() % (MAX - MIN + 1) + MIN
  140.  
  141.         insert(root, n)
  142.     }
  143.  
  144.     fmt.Printf("Preorder traversal of BST:\n")
  145.     preorder(root);
  146.     fmt.Printf("\n\n")
  147.  
  148.     fmt.Printf("Inorder traversal of BST:\n")
  149.     inorder(root);
  150.     fmt.Printf("\n\n")
  151.  
  152.     fmt.Printf("Postorder traversal of BST:\n")
  153.     postorder(root);
  154.     fmt.Printf("\n\n")
  155.  
  156.     fmt.Printf("Number of Nodes: %d\n", length(root))
  157.     fmt.Printf("Number of Leaf Nodes: %d\n", getNumberOfLeafNodes(root))
  158.     fmt.Printf("Minimum value: %d\n", findMinimum(root).value)
  159.  
  160.     print2D(root)
  161. }
Advertisement