____  ___    _  _     _   _ _____     _______
 / ___|/ _ \  | || |   | | | |_ _\ \   / / ____|
| |  _| | | | | || |_  | |_| || | \ \ / /|  _|
| |_| | |_| | |__   _| |  _  || |  \ V / | |___
 \____|\___/     |_|   |_| |_|___|  \_/  |_____|

 --- A GOPHER-LIKE INTERFACE FOR HIVE BLOCKCHAIN ---

Ruby Programming Tutorial - Lesson 32 - Recursion and Binary Trees

BY: @bilal-haider | CREATED: March 22, 2018, 10:28 a.m. | VOTES: 39 | PAYOUT: $5.39 | [ VOTE ]

In this article we are going to learn about binary Tree data structure

[IMAGE: https://steemitimages.com/DQmevcFEvtehwqqy8yTyoxSr221quX6DeLqEmJU777sRxJC/1*UjSfPoMwCEkke1_iuNZ1EQ.jpeg]

Before we go learn it, we should be familiar with Recursion, and recursive methods.
so lets learn them first :)

What is Recursion ?

> Recursion is a process in which a function calls itself as a subroutine. This allows the function to be repeated several times, since it calls itself during its execution. Functions that incorporate recursion are called recursive functions.

Recursion is often seen as an efficient method of programming since it requires the least amount of code to perform the necessary functions. However, recursion must be incorporated carefully, since it can lead to an infinite loop if no condition is met that will terminate the function.

Condition that makes it terminate is called "Base condition".

Lets have a look at a simple recursion

def factorial(x)
   if x == 1
      return x
   else
      return x*factorial(x-1)
   end
end

if you will call this method

puts factorial(5)

[IMAGE: https://steemitimages.com/DQmQ6wJirZeF2Gw4VLywFaxvatRp3FTSmKMP8o9kqhmxi5M/Screenshot%20from%202018-03-21%2016-18-52.png]

If want to learn more about Recursion or recursive methods, search for it :) Its good idea to have a strong base on this topic before proceeding further.

Binary Trees Data Structure

[IMAGE: https://steemitimages.com/DQmRDbbWrPGYKUmVBKAvfhP6pN5VmoPe8sYiY5ty92Mt8qm/17fig12.gif]

Tree is a data structure, which also consists upon nodes but nodes are connected in a way that every node has two children nodes, left and right, that is why they are called binary trees.

Each node can have 1, 2 or 0 children ..
Every Binary tree has a root node, or some people call it parent node.
Every Binary tree has leaf nodes, nodes which don't have any child nodes

Lets start by Identifying Entities

After learning about binary trees we came to conclusion that a binary tree consists upon two entities.

A binary tree node has three properties, it has a reference to left child, right child and a data variable
So we can create a class to represent it like so.

class Node
    attr_accessor :val, :left, :right

    def initialize(val, left_node, right_node)
        @val   = val
        @left  = left_node
        @right = right_node
    end
end

[IMAGE: https://steemitimages.com/DQmXd56k1hXCsyd3bgeDKm5FZy6GfF9pDF7bPHk6pUHNAdm/Screenshot%20from%202018-03-21%2016-44-45.png]

After creating Node We are ready to use it and implement Binary Tree

require_relative 'node'

class Tree
    attr_accessor :root

    def initialize(val)
        @root = Node.new(val, nil, nil)
    end

    def insert_node(val, current)
        puts val[:id]
        if current == nil
            puts "base condition"
            return Node.new(val, nil, nil)
        end

        if val[:id] < current.val[:id]
            puts "going left"
            current.left = insert_node(val, current.left)
        else
            puts "going right"
            current.right = insert_node(val, current.right)
        end
    end
    def traverse(node)
        _node = node
        if(_node == nil)
            return 
        end
        puts _node.val
        traverse(_node.left)
        traverse(_node.right)
    end

end

[IMAGE: https://steemitimages.com/DQmPgUxKQZkwSekWGVWRm5X9qg273ykBDvTcgdp1HS4Zmv9/Screenshot%20from%202018-03-22%2015-14-03.png]

You can see that, our Binary Tree has three methods.

First method allows us to create a root node, when a tree object is created from it.
Notice that when a tree object is initialized, it creates a root node, which has no children nodes. e.g it's left and right children nodes are nil ...

lets see how we are using this Tree class in main.rb

[IMAGE: https://steemitimages.com/DQmVZ3EUewN9cLjDpuKeVCN3aw299yskcAgnBT69NBZFQGo/Screenshot%20from%202018-03-22%2015-17-46.png]

require_relative 'tree'
require_relative 'node'

_data = {id: 5000, name: "bilalhaider", age: 27, reward: 500}
_myTree = Tree.new(_data)

while $entry.to_i != 3
    print "1 - Insert new Node\n2 - Traverse tree\n3 - Exit\n Your Choice: "
    $entry = gets.chomp 
    _random = Random.new.rand(1..10000)

    if $entry.to_i == 1
        print "Insert your Name: "
        _username = gets.chomp

        print "Insert your age: "
        _age      = gets.chomp

        _data = {id: _random, name: _username, age: _age, reward: 500}
        puts "Data to be inserted in node #{_data}"

        _myTree.insert_node(_data, _myTree.root)
    elsif $entry.to_i == 2
        puts _myTree.traverse(_myTree.root)
    elsif $entry.to_i == 3
        puts "Exiting .... "
    else 
        puts "Invalid choice "
    end
end

Notice that, our Second Method(insert_node) in Tree class uses Recursion, and finds perfect spot for New node, where it can be inserted.. New node that is created from user's input :)

What is perfect location for a new node?
We have few rules for that .. These rules help us, later on for searching a record.

Take a look at following picture, for a better understanding :)

[IMAGE: https://steemitimages.com/DQmSncBsXRjJRdPpMMJKiY7USzdz6D6Qm3zQ6xtifs1PBUh/main-qimg-bea93ee0051a9e148f556c15a45ce7f7.png]

There you go my friends, you have implemented a data structure which is best when it comes to retrieving a recored of a user.

If you have 8 million records of users, it will take only 23 comparisons and you will reach your record :)

When using linked list, you may need to compare, or traverse the list and perform 8 million comparisons in its worst case to reach your record

Same number of steps required when adding a record, so binary trees will help you save time.

TAGS: [ #ruby ] [ #programmer ] [ #tree ] [ #binarytree ] [ #datastructure ]

Replies

@bamsbrayen | March 22, 2018, 10:33 a.m. | Votes: 1 | [ VOTE ]

incredible this article. but unfortunately I do not quite understand about this encoding in this application.

@bilal-haider | March 22, 2018, 10:55 a.m. | Votes: 0 | [ VOTE ]

I am glad you like it :)

@bakhtiar7 | March 22, 2018, 10:40 a.m. | Votes: 0 | [ VOTE ]

Dear whqt is this and how i will learn this?

@bilal-haider | March 22, 2018, 10:56 a.m. | Votes: 0 | [ VOTE ]

It's a smart data structure that is used to handle user's records, or big data

@bakhtiar7 | March 22, 2018, 10:59 a.m. | Votes: 0 | [ VOTE ]

Oh but i tried it

@dearkafka | March 22, 2018, 11:16 a.m. | Votes: 1 | [ VOTE ]

Once again thank you for taking the time for uploading these. Keep them coming!

@bilal-haider | March 22, 2018, 11:55 a.m. | Votes: 0 | [ VOTE ]

glad you like them :) will keep publishing more ..

@juandelima | March 22, 2018, 1:32 p.m. | Votes: 0 | [ VOTE ]

Hi good post!!!

[ BACK TO TRENDING ] [ BACK TO MENU ]
CMD>