binary_trees 0.0.1 → 0.0.3
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.
- checksums.yaml +4 -4
- data/lib/binary_trees.rb +16 -6
- metadata +3 -4
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: c16b0e76e938ac4c254d878e295f0339e27fde37ed973ab7022e16edd9baadb7
|
4
|
+
data.tar.gz: 343202d92502b14bb1165c7211290ac39efd7672f7b6ecad6eed678aa1bca3dc
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
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
|
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 =
|
7
|
-
root.right =
|
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
|
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
|
-
|
39
|
-
|
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.
|
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-
|
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.
|
42
|
-
the tree node values.
|
41
|
+
summary: Convert a binary tree to array and vice versa.
|
43
42
|
test_files: []
|