Skip to content

Instantly share code, notes, and snippets.

@sdevani
Created April 28, 2014 18:47
Show Gist options
  • Save sdevani/11380620 to your computer and use it in GitHub Desktop.
Save sdevani/11380620 to your computer and use it in GitHub Desktop.
On the way to building a tree
class Tree
def initialize
@root = nil
end
def add_node(value)
end
def ensure_consistency(node)
if node.nil?
return true
end
if (node.left.value < node.value || node.right.value < node.value) ||
(node.left_children_count - node.right_children_count).abs > 1
return false
else
return ensure_consistency(node.left) && ensure_consistency(node.right)
end
end
def remove_top_node
end
class Node
attr_accessor :left, :right, :value, :left_children_count, :right_children_count
def initialize(value)
@left_children_count = 0
@right_children_count = 0
@value = value
@left = nil
@right = nil
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment