avatarJerry An

Free AI web copilot to create summaries, insights and extended knowledge, download it at here

2296

Abstract

figure><h2 id="bc2d">Insert an Element & Insert a Tree node</h2><p id="3283">The method <code>InsertElement</code> takes a key and value then constructs a new tree node, then calls the <code>InsertNode</code> function to insert the node into the Tree.</p><p id="760c">The function <code>InsertNode</code> is not a Tree method because its’ implementation is not specific to the Tree.</p> <figure id="a616"> <div> <div>

            <iframe class="gist-iframe" src="/gist/jerryan999/e62b1ef3f83b8bb58f71bd771621ef41.js" allowfullscreen="" frameborder="0" height="undefined" width="undefined">
          </div>
        </div>
    </figure></iframe></div></div></figure><h2 id="1e2b">In-order Traverse</h2><p id="e389">The method<code>InOrderTraverseTree</code> takes a function as an argument and calls the <code>inOrderTraverseTree</code> function with the function as an argument.</p>
    <figure id="13f8">
        <div>
          <div>
            
            <iframe class="gist-iframe" src="/gist/jerryan999/e62b1ef3f83b8bb58f71bd771621ef41.js" allowfullscreen="" frameborder="0" height="undefined" width="undefined">
          </div>
        </div>
    </figure></iframe></div></div></figure><p id="277e">The traverse result of the example binary search tree is as follows.</p><p id="9964"><code>Value 1 -&gt; Value 3 &gt; Value 6 -&gt; Value 8 -&gt; Value 10</code></p><h2 id="d2f4">Preorder Traverse</h2>
    <figure id="4861">
        <div>
          <div>
            
            <iframe class="gist-iframe" src="/gist/jerryan999/e62b1ef3f83b8bb58f71bd771621ef41.js" allowfullscreen="" frameborder="0" height="undefined" width="undefined">
          </div>
        </div>
    </figure></iframe></div></div></figure><p id="cbed">The traverse result of the example binary search tree is as follows.</p><p id="8c64"><code>Value 8 -&gt; Value 3 &gt; Value 1-&gt; Value 6-&gt; Value 10</code></p><h2 id="9973">Postorder Traverse</h2>
    <figure id="da72">
        <div>
          <div>
            
            <iframe class="gist-iframe" src="/gist/jerryan999/e62b1ef3f83b8bb58f71bd771621ef41.js" allowfullscreen="" frameborder="0" hei

Options

ght="undefined" width="undefined"> </div> </div> </figure></iframe></div></div></figure><p id="f3de">The traverse result of the example binary search tree is as follows.</p><p id="6789"><code>Value 1 -> Value 6 > Value 3 -> Value 10-> Value 8</code></p><h2 id="de61">Min & Max</h2><p id="ed18">The method <code>MinNode/MaxNode</code> is used to find the node whose key (<b>not value</b>) is the minimum/maximum.</p><p id="4912">Remember that there is no need to traverse the entire Tree to find the minimum/maximum.</p><p id="71b8">Because the property of a BST assures the minimum/maximum is always the leftmost/rightmost node.</p> <figure id="3b96"> <div> <div>

            <iframe class="gist-iframe" src="/gist/jerryan999/e62b1ef3f83b8bb58f71bd771621ef41.js" allowfullscreen="" frameborder="0" height="undefined" width="undefined">
          </div>
        </div>
    </figure></iframe></div></div></figure><h2 id="a404">Binary Search</h2><p id="993f">The method <code>SearchNode</code> is used to search for a node in the Tree. If it is found, it returns the node. If not, it returns nil.</p>
    <figure id="c5ea">
        <div>
          <div>
            
            <iframe class="gist-iframe" src="/gist/jerryan999/e62b1ef3f83b8bb58f71bd771621ef41.js" allowfullscreen="" frameborder="0" height="undefined" width="undefined">
          </div>
        </div>
    </figure></iframe></div></div></figure><h2 id="4fae">Remove Node</h2><p id="180b">The method <code>RemoveNode</code>is used to remove a node from the Tree based on the key.</p>
    <figure id="1cae">
        <div>
          <div>
            
            <iframe class="gist-iframe" src="/gist/jerryan999/e62b1ef3f83b8bb58f71bd771621ef41.js" allowfullscreen="" frameborder="0" height="undefined" width="undefined">
          </div>
        </div>
    </figure></iframe></div></div></figure><p id="2380">I hope you enjoyed reading this. If you’d like to support me as a writer, consider becoming<a href="https://jerryan.medium.com/membership"> a Medium member</a>. You’ll also get unlimited access to every story on Medium.</p></article></body>

How to Implement the Binary Search Tree(BST) Data Structure in Golang

A binary search tree (BST) is a data structure whose internal nodes store a key greater than all the keys in the node’s left subtree and less than those in its right subtree.

In this tutorial, we will see how to implement the BST using Golang. If you are not familiar with its concepts, please go through the following post before starting this tutorial.

In this post, we will see the following

  • TreeNode & Tree
  • Insert an Element & Insert a Tree node
  • Inorder Traverse / Preorder Traverse / Postorder Traverse
  • Min / Max
  • Binary Search
  • Remove Node

TreeNode & Tree

We first define two structs, one for the TreeNode and one for Tree.

Example Binary Tree and Tree node
  • The TreeNode struct contains the key, value, and pointers to its’ left and right TreeNode.
  • The Tree struct contains the root node and a mutex to lock the Tree.

Insert an Element & Insert a Tree node

The method InsertElement takes a key and value then constructs a new tree node, then calls the InsertNode function to insert the node into the Tree.

The function InsertNode is not a Tree method because its’ implementation is not specific to the Tree.

In-order Traverse

The methodInOrderTraverseTree takes a function as an argument and calls the inOrderTraverseTree function with the function as an argument.

The traverse result of the example binary search tree is as follows.

Value 1 -> Value 3 > Value 6 -> Value 8 -> Value 10

Preorder Traverse

The traverse result of the example binary search tree is as follows.

Value 8 -> Value 3 > Value 1-> Value 6-> Value 10

Postorder Traverse

The traverse result of the example binary search tree is as follows.

Value 1 -> Value 6 > Value 3 -> Value 10-> Value 8

Min & Max

The method MinNode/MaxNode is used to find the node whose key (not value) is the minimum/maximum.

Remember that there is no need to traverse the entire Tree to find the minimum/maximum.

Because the property of a BST assures the minimum/maximum is always the leftmost/rightmost node.

Binary Search

The method SearchNode is used to search for a node in the Tree. If it is found, it returns the node. If not, it returns nil.

Remove Node

The method RemoveNodeis used to remove a node from the Tree based on the key.

I hope you enjoyed reading this. If you’d like to support me as a writer, consider becoming a Medium member. You’ll also get unlimited access to every story on Medium.

Golang
Algorithms
Data Structures
Programming
Data Science
Recommended from ReadMedium