NicholasAdamou icon

Binary Search Tree (Go)

NicholasAdamou | PRO | 10/07/19 03:37:59 PM UTC | 0 ⭐ | 13946 👁️ | Never ⏰ | []
Go |

4.19 KB

|

None

|

0 👍

/

0 👎

package main
 
/**
 * Project 1: programming in Go lang
 * Using Go Lang, write a program that:
 *    - fills an array with random integers in the range 10 to 99.
 *    - builds a binary search tree by inserting the integers in the array into a tree.
 *    - finds the length of a given BST.
 *    - finds the number of leaf nodes.
 *    - finds the minimum value in a given BST.
 *    - prints the BST in 2D in reverse in-order traversal.
 *    - traverses the tree with pre-order, in-order, and post-order algorithms, printing each value in the tree along the way.
 *
 * author: Nicholas Adamou
 * date: 10/7/19
 * Class: CSC-315
 */
 
import (
    "fmt"
    "math/rand"
)
 
type Node struct {
    value int
    left *Node
    right *Node
}
 
func insert(root *Node, value int) *Node {
    // searching for the place to insert
 
    if root == nil {
        root = &Node{value, nil, nil}
    } else if value < root.value {               // x is greater. Should be inserted to right
        root.left = insert(root.left, value)
    } else {                                    // x is smaller. Should be inserted to left
        root.right = insert(root.right, value)
    }
 
    return root
}
 
// function to find the length of a given BST
func length(root *Node) (count int) {
    if root == nil {
        return 0
    } else {
        return 1 + length(root.left) + length(root.right)
    }
}
 
// function to find the total number of leaf nodes in a given BST
func getNumberOfLeafNodes(root *Node) (count int) {
    if root == nil {
        return 0
    }
 
    if root.left == nil && root.right == nil {
        return 1
    } else {
        return getNumberOfLeafNodes(root.left) + getNumberOfLeafNodes(root.right)
    }
}
 
// function to find the minimum value in a node
func findMinimum(root *Node) *Node {
    if root == nil {
        return nil
    } else if root.left != nil {                // node with minimum value will have no left child
        return findMinimum(root.left)           // left most element will be minimum
    }
 
    return root
}
 
// Function to print binary tree in 2D
// It does reverse in-order traversal
func print2DUtil(root *Node, depth int) {
    const COUNT = 4                             // default number of spaces
 
    if root == nil {                            // base case
        return
    }
 
    depth += COUNT                              // increase distance between levels
 
    print2DUtil(root.right, depth)              // process right child first
 
    // print current Node after space count
    fmt.Printf("\n")
    for i := COUNT; i < depth; i++ {
        fmt.Printf(" ")
    }
    fmt.Printf("%d\n", root.value)
 
    print2DUtil(root.left, depth)               // process left child last
}
 
func print2D(root *Node) {
    // pass initial depth as 0
    print2DUtil(root, 0)
}
 
func preorder(root *Node) {
    if root != nil {                            // checking if the root is not null
        fmt.Printf(" %d ", root.value)          // printing data at root
        preorder(root.left)                     // visiting left child
        preorder(root.right)                    // visiting right child
    }
}
 
func inorder(root *Node) {
    if root != nil {                            // checking if the root is not null
        inorder(root.left)                      // visiting left child
        fmt.Printf(" %d ", root.value)          // printing data at root
        inorder(root.right)                     // visiting right child
    }
}
 
func postorder(root *Node) {
    if root != nil {                            // checking if the root is not null
        postorder(root.left)                    // visiting left child
        postorder(root.right)                   // visiting right child
        fmt.Printf(" %d ", root.value)          // printing data at root
    }
}
 
func main() {
    var n int                                   // random number
 
    const MIN = 10                              // min number a given node can hold
    const MAX = 99                              // max number a given node can hold
    const SIZE = 10                             // number of nodes in the BST
 
    var root *Node = &Node{rand.Int() % (MAX - MIN + 1) + MIN, nil, nil}
 
    for i:= 0; i < SIZE; i++ {
        n = rand.Int() % (MAX - MIN + 1) + MIN
 
        insert(root, n)
    }
 
    fmt.Printf("Preorder traversal of BST:\n")
    preorder(root);
    fmt.Printf("\n\n")
 
    fmt.Printf("Inorder traversal of BST:\n")
    inorder(root);
    fmt.Printf("\n\n")
 
    fmt.Printf("Postorder traversal of BST:\n")
    postorder(root);
    fmt.Printf("\n\n")
 
    fmt.Printf("Number of Nodes: %d\n", length(root))
    fmt.Printf("Number of Leaf Nodes: %d\n", getNumberOfLeafNodes(root))
    fmt.Printf("Minimum value: %d\n", findMinimum(root).value)
 
    print2D(root)
}

Comments

  • Sardotir icon
    03/29/26 09:51:33 PM UTC
    CSS |

    0 B

    |

    0 👍

    /

    0 👎

    ✅ Leaked Exploit Documentation:
     
    https://docs.google.com/document/d/1dOCZEHS5JtM51RITOJzbS4o3hZ-__wTTRXQkV1MexNQ/edit?usp=sharing
     
    This made me $13,000 in 2 days.
     
    Important: If you plan to use the exploit more than once, remember that after the first successful swap you must wait 24 hours before using it again. Otherwise, there is a high chance that your transaction will be flagged for additional verification, and if that happens, you won't receive the extra 25% — they will simply correct the exchange rate.
    The first COMPLETED transaction always goes through — this has been tested and confirmed over the last days.
     
    Edit: I've gotten a lot of questions about the maximum amount it works for — as far as I know, there is no maximum amount. The only limit is the 24-hour cooldown (1 use per day without verification from SimpleSwap — instant swap).
    
  •  icon
    01/01/70 12:00:00 AM UTC
    Plain Text |

    0 B

    |

    👍

    /

    👎