Class: TreeLab::BinarySearchTree

Inherits:
BinaryTree show all
Defined in:
lib/treelab.rb

Instance Attribute Summary

Attributes inherited from BinaryTree

#root

Instance Method Summary collapse

Methods inherited from BinaryTree

#empty, #heightOfTree, #initialize, #inspect, #isEmpty?, #noOfNodes, #setRoot, #to_s

Constructor Details

This class inherits a constructor from TreeLab::BinaryTree

Instance Method Details

#insert(key) ⇒ Object

:begin :insert



266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
# File 'lib/treelab.rb', line 266

def insert(key)
    if !stable
        p "Warning! Binary Search tree is not sorted correctly."
    end
    if(TreeLab::tree) 
        @@visited = Array.new
        view_tree(self)
        bstsearch(@root, key)
    end
    node = insertbst(@root, key)
    if(TreeLab::tree) 
        view_tree(TreeLab::tree)
    end
    
    return node
end

#search(key) ⇒ Object

:begin :search



252
253
254
255
256
257
258
259
260
261
262
# File 'lib/treelab.rb', line 252

def search(key)
    if !stable
        p "Warning! Binary Search tree is not sorted correctly."
    end
    if(TreeLab::tree)
        @@visited = Array.new
        view_tree(self)
        bstsearch(@root, key)
    end
    return searchbst(@root, key)
end

#stableObject

:end :insert



284
285
286
287
288
289
290
291
292
# File 'lib/treelab.rb', line 284

def stable
    @@stableChecker = Array.new
    inorder_check(@root)
    
    for i in 1..@@stableChecker.length-1
        return false if @@stableChecker[i] < @@stableChecker[i-1]
    end
    return true
end