Advertisement
Guest User

Untitled

a guest
May 23rd, 2017
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 2.65 KB | None | 0 0
  1. class Node
  2.   attr_accessor :left_child, :right_child, :value
  3.  
  4.   def initialize(value)
  5.     @value = value
  6.     @left_child = nil
  7.     @right_child = nil
  8.   end
  9.  
  10.   #inserts a value to the tree at the right place
  11.   def insert(value)  
  12.     if value < @value  
  13.       if not @left_child.nil? then @left_child.insert(value) else @left_child = Node.new(value) end
  14.     else
  15.       if not @right_child.nil? then @right_child.insert(value) else @right_child = Node.new(value) end
  16.     end  
  17.   end
  18.  
  19.   #searches the tree recursivly for a value
  20.   def search(value)    
  21.     return value if value == @value
  22.     if value < @value then @left_child.search(value) else @right_child.search(value) end    
  23.   end
  24.  
  25.   #uberpwnage depth method. calculates the depth of the tree recursively
  26.   def depth
  27.     [@left_child.depth, @right_child.depth].max+1
  28.   end
  29.  
  30.   #searches the tree for the minimum value
  31.   def min_value
  32.     if @left_child.nil? then value else @left_child.min_value end
  33.   end
  34.  
  35.   #lolmethod calculating the size of the tree
  36.   def size
  37.     @left_child.size + @right_child.size + 1  
  38.   end
  39.  
  40.   def print_tree
  41.    
  42.   end
  43.  
  44.   #helper methods to get some freakin code, oh jeah baby
  45.   def nil.depth
  46.     0
  47.   end
  48.  
  49.   def nil.size
  50.     0
  51.   end
  52. end
  53.  
  54. require "node.rb"
  55. #class that represents a binary tree with methods to access
  56. class BinaryTree
  57.   attr_accessor :root, :size
  58.   def initialize
  59.  
  60.   end
  61.    
  62.   #insert values to the tree and creates root node if it does not exist
  63.   def insert(value)
  64.     if not @root.nil? then @root.insert(value) else @root = Node.new(value) end  
  65.   end
  66.  
  67.   #returns the first value of the tree that has the same value as the argument value
  68.   def search(value)
  69.     @root.search(value)
  70.   end
  71.  
  72.   def size
  73.     @root.size
  74.   end
  75.  
  76.   #return the depth of the tree
  77.   def depth
  78.     @root.depth if not @root.nil?    
  79.   end
  80.  
  81.   #returns the minimum value of the tree (the node most left)
  82.   def min_value
  83.     @root.min_value if not @root.nil?
  84.   end
  85.  
  86.   def print_tree  
  87.     @root.print_tree if not @root.nil?    
  88.   end
  89.  
  90. end
  91.  
  92. require "binary_tree"
  93.  
  94.  
  95. btree = BinaryTree.new
  96.  
  97. #testarrays
  98. array = [5, 1, 3, 4, 7, 8, 4, 4, 4]
  99. array2 = [5, 1, 7]
  100. array3 = [5, 1, 3, 4, 7, 8]
  101.  
  102. puts "Binary Tree"
  103. puts "InputArray: #{array}"
  104. puts "InputArraySize: #{array.length}"
  105.  
  106. #inserting the testvalues
  107. array.each do |value|
  108.   btree.insert(value)
  109. end
  110.  
  111. puts "================================"
  112. puts "binary tree created"
  113. puts "BinaryTreeSize: #{btree.size}"
  114. puts "BinaryTreeDeapth: #{btree.depth}"
  115. puts "BinaryTreeMinimumValue: #{btree.min_value}"
  116. puts "================================"
  117. puts btree.search(3)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement