binary_trees 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (3) hide show
  1. checksums.yaml +7 -0
  2. data/lib/binary_trees.rb +42 -0
  3. metadata +43 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 4ab09804a82e404db72425b9848e6f1d8ead807424e24770a3c2d640a30e2879
4
+ data.tar.gz: b099346161b1732014822603e4f616ab4d0729b083952974c56b7c89746eca7f
5
+ SHA512:
6
+ metadata.gz: 45477346a529f82ea8241fcadfff3377b3c94e7889b7bb9c6651b776ae2035c863d7b1593092a69ae9bbd7ddd6759fce921c9dcb806ea59c7d0d3d281d2a36e7
7
+ data.tar.gz: e11822bb88ced9f91e3b366bb07a835ce335825245c5e0167aa664bae4befcaf669def1d9651f42dbcd1197a63db65398bbe7e224ac97457f1f170a52e74bd31
@@ -0,0 +1,42 @@
1
+ class Array
2
+ def to_tree(i = 0)
3
+ root = nil
4
+ if i < self.length
5
+ root = TreeNode.new(self[i])
6
+ root.left = to_tree(i * 2 + 1)
7
+ root.right = to_tree(i * 2 + 2)
8
+ end
9
+ root
10
+ end
11
+ end
12
+
13
+ class TreeNode
14
+ attr_accessor :val, :left, :right
15
+
16
+ def initialize(val = 0, left = nil, right = nil)
17
+ @val = val
18
+ @left = left
19
+ @right = right
20
+ end
21
+ end
22
+
23
+ class Tree
24
+ def initialize(root = nil)
25
+ @root = root
26
+ end
27
+
28
+ def sum(root = @root)
29
+ return 0 if root.nil?
30
+
31
+ root.val + sum(root.left) + sum(root.right)
32
+ end
33
+
34
+ def to_array(root = @root, arr = [], idx = 0)
35
+ return [] if root.nil?
36
+
37
+ arr[idx] = root.val
38
+ to_array(root.left, arr, idx * 2 + 1) if root.left
39
+ to_array(root.right, arr, idx * 2 + 2) if root.right
40
+ arr
41
+ end
42
+ end
metadata ADDED
@@ -0,0 +1,43 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: binary_trees
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Queenie Peng
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2023-01-30 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description:
14
+ email:
15
+ executables: []
16
+ extensions: []
17
+ extra_rdoc_files: []
18
+ files:
19
+ - lib/binary_trees.rb
20
+ homepage: https://github.com/QueeniePeng/binary_trees
21
+ licenses: []
22
+ metadata: {}
23
+ post_install_message:
24
+ rdoc_options: []
25
+ require_paths:
26
+ - lib
27
+ required_ruby_version: !ruby/object:Gem::Requirement
28
+ requirements:
29
+ - - ">="
30
+ - !ruby/object:Gem::Version
31
+ version: '0'
32
+ required_rubygems_version: !ruby/object:Gem::Requirement
33
+ requirements:
34
+ - - ">="
35
+ - !ruby/object:Gem::Version
36
+ version: '0'
37
+ requirements: []
38
+ rubygems_version: 3.1.4
39
+ signing_key:
40
+ specification_version: 4
41
+ summary: Convert a binary tree to array and vice versa. Given a tree and can sum all
42
+ the tree node values.
43
+ test_files: []