tree_handler 0.1.0
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 +7 -0
- data/.gitignore +11 -0
- data/.rspec +3 -0
- data/.travis.yml +6 -0
- data/Gemfile +7 -0
- data/Gemfile.lock +34 -0
- data/LICENSE.txt +21 -0
- data/README.md +40 -0
- data/Rakefile +6 -0
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/lib/.tree_handler.rb.swp +0 -0
- data/lib/tree_handler.rb +152 -0
- data/lib/tree_handler/comparable.rb +19 -0
- data/lib/tree_handler/node.rb +27 -0
- data/lib/tree_handler/version.rb +3 -0
- data/tree_handler.gemspec +21 -0
- metadata +60 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: d4f84c2af33d9bc188f1d79837dd4131b7d0505d78625c5729a63747eace2d94
|
4
|
+
data.tar.gz: 89e71bc1056c0aa5c5904540dc1c405d80013f7f53b9a204bc09566da027f022
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 719a4923aad2588a5bf65102bf4643309248dc826177e4125e45fecd60855b062227819a43f221f10d443d44595de4e945a3817edaa205237908bec561c68cee
|
7
|
+
data.tar.gz: 8d5c93d3e4d0721a1e2c58cc582e2629b027c9b14c3cbf004b99c3079aae0336fdac4de500aaac49de03b326daf62be015a13ca18249bcc7eee881d3be493350
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/Gemfile.lock
ADDED
@@ -0,0 +1,34 @@
|
|
1
|
+
PATH
|
2
|
+
remote: .
|
3
|
+
specs:
|
4
|
+
tree_handler (0.1.0)
|
5
|
+
|
6
|
+
GEM
|
7
|
+
remote: https://rubygems.org/
|
8
|
+
specs:
|
9
|
+
diff-lcs (1.4.4)
|
10
|
+
rake (12.3.3)
|
11
|
+
rspec (3.9.0)
|
12
|
+
rspec-core (~> 3.9.0)
|
13
|
+
rspec-expectations (~> 3.9.0)
|
14
|
+
rspec-mocks (~> 3.9.0)
|
15
|
+
rspec-core (3.9.2)
|
16
|
+
rspec-support (~> 3.9.3)
|
17
|
+
rspec-expectations (3.9.2)
|
18
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
19
|
+
rspec-support (~> 3.9.0)
|
20
|
+
rspec-mocks (3.9.1)
|
21
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
22
|
+
rspec-support (~> 3.9.0)
|
23
|
+
rspec-support (3.9.3)
|
24
|
+
|
25
|
+
PLATFORMS
|
26
|
+
ruby
|
27
|
+
|
28
|
+
DEPENDENCIES
|
29
|
+
rake (~> 12.0)
|
30
|
+
rspec (~> 3.0)
|
31
|
+
tree_handler!
|
32
|
+
|
33
|
+
BUNDLED WITH
|
34
|
+
2.1.4
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2020 Tristan Hörler
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,40 @@
|
|
1
|
+
# TreeHandler
|
2
|
+
|
3
|
+
Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/tree_handler`. To experiment with that code, run `bin/console` for an interactive prompt.
|
4
|
+
|
5
|
+
TODO: Delete this and the text above, and describe your gem
|
6
|
+
|
7
|
+
## Installation
|
8
|
+
|
9
|
+
Add this line to your application's Gemfile:
|
10
|
+
|
11
|
+
```ruby
|
12
|
+
gem 'tree_handler'
|
13
|
+
```
|
14
|
+
|
15
|
+
And then execute:
|
16
|
+
|
17
|
+
$ bundle install
|
18
|
+
|
19
|
+
Or install it yourself as:
|
20
|
+
|
21
|
+
$ gem install tree_handler
|
22
|
+
|
23
|
+
## Usage
|
24
|
+
|
25
|
+
TODO: Write usage instructions here
|
26
|
+
|
27
|
+
## Development
|
28
|
+
|
29
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
30
|
+
|
31
|
+
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
32
|
+
|
33
|
+
## Contributing
|
34
|
+
|
35
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/tree_handler.
|
36
|
+
|
37
|
+
|
38
|
+
## License
|
39
|
+
|
40
|
+
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
data/Rakefile
ADDED
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "tree_handler"
|
5
|
+
|
6
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
7
|
+
# with your gem easier. You can also use a different console, if you like.
|
8
|
+
|
9
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
10
|
+
# require "pry"
|
11
|
+
# Pry.start
|
12
|
+
|
13
|
+
require "irb"
|
14
|
+
IRB.start(__FILE__)
|
data/bin/setup
ADDED
Binary file
|
data/lib/tree_handler.rb
ADDED
@@ -0,0 +1,152 @@
|
|
1
|
+
module TreeHandler
|
2
|
+
require 'tree_handler/node'
|
3
|
+
|
4
|
+
class Tree
|
5
|
+
attr_accessor :data, :root
|
6
|
+
include Comparable
|
7
|
+
|
8
|
+
def initialize(array)
|
9
|
+
@data = array.uniq
|
10
|
+
@root = build_tree
|
11
|
+
end
|
12
|
+
|
13
|
+
def build_tree(array = @data.dup)
|
14
|
+
root = Node.new(array.shift)
|
15
|
+
array.each do |value|
|
16
|
+
node = Node.new(value)
|
17
|
+
compare(root, node)
|
18
|
+
end
|
19
|
+
root
|
20
|
+
end
|
21
|
+
|
22
|
+
def insert(value)
|
23
|
+
@data.push(value)
|
24
|
+
node = Node.new(value)
|
25
|
+
compare(root, node)
|
26
|
+
end
|
27
|
+
|
28
|
+
def delete(value, parent_pointer = @root)
|
29
|
+
@data.delete(value)
|
30
|
+
node = find(value)
|
31
|
+
return nil if node.nil?
|
32
|
+
|
33
|
+
unless node == @root
|
34
|
+
parent = find_parent(value)
|
35
|
+
parent_pointer =
|
36
|
+
value < parent.value ? parent.left_child : parent.right_child
|
37
|
+
end
|
38
|
+
|
39
|
+
if node.left_child.nil?
|
40
|
+
return parent_pointer = node.right_child
|
41
|
+
elsif node.right_child.nil?
|
42
|
+
return parent_pointer = node.left_child
|
43
|
+
else
|
44
|
+
parent_pointer = node.left_child
|
45
|
+
compare(parent_pointer, node.right_child)
|
46
|
+
end
|
47
|
+
end
|
48
|
+
|
49
|
+
def find(value, node = @root)
|
50
|
+
if value == node.value
|
51
|
+
return node
|
52
|
+
elsif value < node.value
|
53
|
+
return nil if node.left_child.nil?
|
54
|
+
find(value, node.left_child)
|
55
|
+
elsif value > node.value
|
56
|
+
return nil if node.right_child.nil?
|
57
|
+
find(value, node.right_child)
|
58
|
+
end
|
59
|
+
end
|
60
|
+
|
61
|
+
def find_parent(value)
|
62
|
+
value == @root.value ? nil : find_parent_rec(value)
|
63
|
+
end
|
64
|
+
|
65
|
+
def find_parent_rec(value, node = @root)
|
66
|
+
value_left_child = node.left_child.nil? ? nil : node.left_child.value
|
67
|
+
value_right_child = node.right_child.nil? ? nil : node.right_child.value
|
68
|
+
|
69
|
+
if value == value_left_child || value == value_right_child
|
70
|
+
return node
|
71
|
+
elsif value < node.value
|
72
|
+
value_left_child.nil? ? nil : find_parent_rec(value, node.left_child)
|
73
|
+
elsif value > node.value
|
74
|
+
value_right_child.nil? ? nil : find_parent_rec(value, node.right_child)
|
75
|
+
end
|
76
|
+
end
|
77
|
+
|
78
|
+
def level_order(queue = [@root], no_block = [])
|
79
|
+
until queue.empty?
|
80
|
+
if block_given?
|
81
|
+
yield(queue.map(&:value))
|
82
|
+
else
|
83
|
+
no_block.push(queue.map(&:value))
|
84
|
+
end
|
85
|
+
(0...queue.count).each do
|
86
|
+
next_node = queue.shift
|
87
|
+
queue.push(next_node.left_child) unless next_node.left_child.nil?
|
88
|
+
queue.push(next_node.right_child) unless next_node.right_child.nil?
|
89
|
+
end
|
90
|
+
end
|
91
|
+
no_block
|
92
|
+
end
|
93
|
+
|
94
|
+
def level_order_rec(queue = [@root], no_block = [], &block)
|
95
|
+
queue_child = []
|
96
|
+
if block_given?
|
97
|
+
block.call(queue.map(&:value))
|
98
|
+
else
|
99
|
+
no_block.push(queue.map(&:value))
|
100
|
+
end
|
101
|
+
|
102
|
+
queue.each do |node|
|
103
|
+
queue_child.push(node.left_child) unless node.left_child.nil?
|
104
|
+
queue_child.push(node.right_child) unless node.right_child.nil?
|
105
|
+
end
|
106
|
+
level_order_rec(queue_child, no_block, &block) unless queue_child.empty?
|
107
|
+
no_block
|
108
|
+
end
|
109
|
+
|
110
|
+
%i[preorder inorder postorder].each do |method|
|
111
|
+
define_method(method) do |node = @root, no_block = [], &block|
|
112
|
+
parent =
|
113
|
+
Proc.new { block ? block.call(node.value) : no_block << node.value }
|
114
|
+
|
115
|
+
parent.call if method == :preorder
|
116
|
+
unless node.left_child.nil?
|
117
|
+
send(method, node.left_child, no_block, &block)
|
118
|
+
end
|
119
|
+
parent.call if method == :inorder
|
120
|
+
unless node.right_child.nil?
|
121
|
+
send(method, node.right_child, no_block, &block)
|
122
|
+
end
|
123
|
+
parent.call if method == :postorder
|
124
|
+
no_block
|
125
|
+
end
|
126
|
+
end
|
127
|
+
|
128
|
+
def depth(node)
|
129
|
+
node.nil? ? 0 : level_order([node]).size
|
130
|
+
end
|
131
|
+
|
132
|
+
def balanced?(root = @root)
|
133
|
+
left_height = depth(root.left_child)
|
134
|
+
right_height = depth(root.right_child)
|
135
|
+
(-1..1).include?(left_height - right_height)
|
136
|
+
end
|
137
|
+
|
138
|
+
def rebalance!
|
139
|
+
result = rebalance_ordering(@data.dup.sort)
|
140
|
+
@root = build_tree(result)
|
141
|
+
end
|
142
|
+
|
143
|
+
def rebalance_ordering(tree, result = [])
|
144
|
+
return if tree.size == 0
|
145
|
+
middle = tree.size / 2
|
146
|
+
result.push tree.slice!(middle)
|
147
|
+
rebalance_ordering(tree[0...middle], result)
|
148
|
+
rebalance_ordering(tree[(middle)..-1], result)
|
149
|
+
result
|
150
|
+
end
|
151
|
+
end
|
152
|
+
end
|
@@ -0,0 +1,19 @@
|
|
1
|
+
module TreeHandler
|
2
|
+
module Comparable
|
3
|
+
def compare(node_A, node_B)
|
4
|
+
if node_B.value < node_A.value
|
5
|
+
if node_A.left_child.nil?
|
6
|
+
node_A.left_child = node_B
|
7
|
+
else
|
8
|
+
compare(node_A.left_child, node_B)
|
9
|
+
end
|
10
|
+
else
|
11
|
+
if node_A.right_child.nil?
|
12
|
+
node_A.right_child = node_B
|
13
|
+
else
|
14
|
+
compare(node_A.right_child, node_B)
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
@@ -0,0 +1,27 @@
|
|
1
|
+
class Node
|
2
|
+
attr_accessor :value, :left_child, :right_child
|
3
|
+
|
4
|
+
def initialize(value)
|
5
|
+
@value = value
|
6
|
+
@left_child = nil
|
7
|
+
@right_child = nil
|
8
|
+
end
|
9
|
+
end
|
10
|
+
|
11
|
+
module Comparable
|
12
|
+
def compare(node_A, node_B)
|
13
|
+
if node_B.value < node_A.value
|
14
|
+
if node_A.left_child.nil?
|
15
|
+
node_A.left_child = node_B
|
16
|
+
else
|
17
|
+
compare(node_A.left_child, node_B)
|
18
|
+
end
|
19
|
+
else
|
20
|
+
if node_A.right_child.nil?
|
21
|
+
node_A.right_child = node_B
|
22
|
+
else
|
23
|
+
compare(node_A.right_child, node_B)
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
@@ -0,0 +1,21 @@
|
|
1
|
+
require_relative 'lib/tree_handler/version'
|
2
|
+
|
3
|
+
Gem::Specification.new do |spec|
|
4
|
+
spec.name = "tree_handler"
|
5
|
+
spec.version = TreeHandler::VERSION
|
6
|
+
spec.authors = ["Tristan Hörler"]
|
7
|
+
spec.email = ["tristan.horler@gmail.com"]
|
8
|
+
|
9
|
+
spec.summary = "Handling Search BinaryTree with ease"
|
10
|
+
spec.description = "Set of functions to create, modify and search in a binary tree from a list."
|
11
|
+
spec.homepage = "https://github.com/Ichenn92/tree_handler"
|
12
|
+
spec.license = "MIT"
|
13
|
+
spec.required_ruby_version = Gem::Requirement.new(">= 2.3.0")
|
14
|
+
|
15
|
+
spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
|
16
|
+
`git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
17
|
+
end
|
18
|
+
spec.bindir = "exe"
|
19
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
20
|
+
spec.require_paths = ["lib"]
|
21
|
+
end
|
metadata
ADDED
@@ -0,0 +1,60 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: tree_handler
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Tristan Hörler
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2020-09-22 00:00:00.000000000 Z
|
12
|
+
dependencies: []
|
13
|
+
description: Set of functions to create, modify and search in a binary tree from a
|
14
|
+
list.
|
15
|
+
email:
|
16
|
+
- tristan.horler@gmail.com
|
17
|
+
executables: []
|
18
|
+
extensions: []
|
19
|
+
extra_rdoc_files: []
|
20
|
+
files:
|
21
|
+
- ".gitignore"
|
22
|
+
- ".rspec"
|
23
|
+
- ".travis.yml"
|
24
|
+
- Gemfile
|
25
|
+
- Gemfile.lock
|
26
|
+
- LICENSE.txt
|
27
|
+
- README.md
|
28
|
+
- Rakefile
|
29
|
+
- bin/console
|
30
|
+
- bin/setup
|
31
|
+
- lib/.tree_handler.rb.swp
|
32
|
+
- lib/tree_handler.rb
|
33
|
+
- lib/tree_handler/comparable.rb
|
34
|
+
- lib/tree_handler/node.rb
|
35
|
+
- lib/tree_handler/version.rb
|
36
|
+
- tree_handler.gemspec
|
37
|
+
homepage: https://github.com/Ichenn92/tree_handler
|
38
|
+
licenses:
|
39
|
+
- MIT
|
40
|
+
metadata: {}
|
41
|
+
post_install_message:
|
42
|
+
rdoc_options: []
|
43
|
+
require_paths:
|
44
|
+
- lib
|
45
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
46
|
+
requirements:
|
47
|
+
- - ">="
|
48
|
+
- !ruby/object:Gem::Version
|
49
|
+
version: 2.3.0
|
50
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ">="
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
55
|
+
requirements: []
|
56
|
+
rubygems_version: 3.0.8
|
57
|
+
signing_key:
|
58
|
+
specification_version: 4
|
59
|
+
summary: Handling Search BinaryTree with ease
|
60
|
+
test_files: []
|