binary_trees 0.0.1 → 0.0.3

Sign up to get free protection for your applications and to get access to all the features.
Files changed (3) hide show
  1. checksums.yaml +4 -4
  2. data/lib/binary_trees.rb +16 -6
  3. metadata +3 -4
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 4ab09804a82e404db72425b9848e6f1d8ead807424e24770a3c2d640a30e2879
4
- data.tar.gz: b099346161b1732014822603e4f616ab4d0729b083952974c56b7c89746eca7f
3
+ metadata.gz: c16b0e76e938ac4c254d878e295f0339e27fde37ed973ab7022e16edd9baadb7
4
+ data.tar.gz: 343202d92502b14bb1165c7211290ac39efd7672f7b6ecad6eed678aa1bca3dc
5
5
  SHA512:
6
- metadata.gz: 45477346a529f82ea8241fcadfff3377b3c94e7889b7bb9c6651b776ae2035c863d7b1593092a69ae9bbd7ddd6759fce921c9dcb806ea59c7d0d3d281d2a36e7
7
- data.tar.gz: e11822bb88ced9f91e3b366bb07a835ce335825245c5e0167aa664bae4befcaf669def1d9651f42dbcd1197a63db65398bbe7e224ac97457f1f170a52e74bd31
6
+ metadata.gz: ca401a77cebd4b280ce457338441365dc3df2d5dd28a62c84b1133c9213cf03cb32bdd6551f6fe996d745c4ac62b8e381490b337b5bcc1063b5a0c68e666f34e
7
+ data.tar.gz: e723c1cc89c851f9bca0636d150bfa0390b13d4976176e7acf02656a0750e0e3eeb994fd1d14bea2fb798e024fd07e32cb6ff49ed134e4fd85c109814bdfc850
data/lib/binary_trees.rb CHANGED
@@ -1,10 +1,10 @@
1
1
  class Array
2
- def to_tree(i = 0)
2
+ def to_root(i = 0)
3
3
  root = nil
4
4
  if i < self.length
5
5
  root = TreeNode.new(self[i])
6
- root.left = to_tree(i * 2 + 1)
7
- root.right = to_tree(i * 2 + 2)
6
+ root.left = to_root(i * 2 + 1)
7
+ root.right = to_root(i * 2 + 2)
8
8
  end
9
9
  root
10
10
  end
@@ -21,6 +21,7 @@ class TreeNode
21
21
  end
22
22
 
23
23
  class Tree
24
+
24
25
  def initialize(root = nil)
25
26
  @root = root
26
27
  end
@@ -31,12 +32,21 @@ class Tree
31
32
  root.val + sum(root.left) + sum(root.right)
32
33
  end
33
34
 
34
- def to_array(root = @root, arr = [], idx = 0)
35
+ def invert(root = @root)
36
+ return nil unless root
37
+
38
+ temp = root.left
39
+ root.left = invert(root.right)
40
+ root.right = invert(temp)
41
+ root
42
+ end
43
+
44
+ def to_a(root = @root, arr = [], idx = 0)
35
45
  return [] if root.nil?
36
46
 
37
47
  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
48
+ to_a(root.left, arr, idx * 2 + 1) if root.left
49
+ to_a(root.right, arr, idx * 2 + 2) if root.right
40
50
  arr
41
51
  end
42
52
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: binary_trees
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Queenie Peng
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2023-01-30 00:00:00.000000000 Z
11
+ date: 2023-02-18 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description:
14
14
  email:
@@ -38,6 +38,5 @@ requirements: []
38
38
  rubygems_version: 3.1.4
39
39
  signing_key:
40
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.
41
+ summary: Convert a binary tree to array and vice versa.
43
42
  test_files: []