-
-
Save apeiros/f37bffe200ff63420f1794e0f5d25974 to your computer and use it in GitHub Desktop.
Revisions
-
apeiros revised this gist
Feb 8, 2018 . 1 changed file with 21 additions and 16 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -21,6 +21,22 @@ def nodes result end def insert(new_node) if new_node < self if @left @left.insert(new_node) else @left = new_node end else if @right @right.insert(new_node) else @right = new_node end end end def to_s [left, data, right].join(" ") @@ -30,29 +46,18 @@ def to_s class BinaryTree attr_accessor :root def insert(new_node) if @root @root.insert(new_node) else # if root (first node in tree) does not exist @root = new_node #assign the new_node to root instance variable end end def nodes @root.nodes end #def to_s # self.root.to_s.squeeze(" ").strip #end -
apeiros revised this gist
Feb 8, 2018 . 1 changed file with 14 additions and 15 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -2,16 +2,26 @@ class Node attr_accessor :data, :left, :right def initialize(data) @left = nil @right = nil @data = data end include Comparable def <=>(node) data <=> node.data end def nodes result = [] result.concat(@left.nodes) if @left result << self result.concat(@right.nodes) if @right result end def to_s [left, data, right].join(" ") end @@ -38,18 +48,8 @@ def insert(new_node, node = root) end end def nodes @root.nodes end @@ -66,4 +66,3 @@ def nodes(current_node = self.root) puts @tree.nodes -
CodePint created this gist
Feb 8, 2018 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,69 @@ class Node attr_accessor :data, :left, :right def initialize(data) @data = data end include Comparable def <=>(node) data <=> node.data end def to_s [left, data, right].join(" ") end end class BinaryTree attr_accessor :root def insert(new_node, node = root) if root.nil? # if root (first node in tree) does not exist self.root = new_node #assign the new_node to root instance variable elsif node.nil? #first statement doesnt fire and node = nil new_node else if new_node < node node.left = insert(new_node, node.left) else node.right = insert(new_node, node.right) end node end end def nodes(current_node = self.root) all_nodes = [] all_nodes << current_node if current_node.left all_nodes << current_node.left all_nodes = all_nodes + nodes(current_node.left) end if current_node.right all_nodes << current_node.right all_nodes = all_nodes + nodes(current_node.right) end all_nodes end #def to_s # self.root.to_s.squeeze(" ").strip #end end @tree = BinaryTree.new %w(Dan Barry Ted Daniel Alice Andy Sally).each do |name| @tree.insert(Node.new(name)) end puts @tree.nodes