|
1 |
| -# File: BinarySearchTree.rb |
2 |
| -# |
3 |
| -# Author: Sasha Goldenson |
4 |
| -# |
5 |
| -# License: Free to use |
6 |
| -# |
7 |
| -# Inspiration: Ron Tsui's (https://github.com/vokoshyv) excellent technical guidance has been very helpful in my understanding of these fundamentals. |
8 |
| -# |
9 |
| -# Motivation: Continuing my education, solidifying my fundamentals. Spreading the love. |
10 |
| -# |
11 |
| -# Test-Driven Development: Developed using 10 unit tests with a total of 31 assertions which describe and document this Binary Search Tree. |
12 |
| -# |
13 |
| -# Binary Search Trees are a special pattern of the Tree pattern. They must follow the algorithmic rules which say the left child of the parent is always |
14 |
| -# lesser and the right child is always greater than its parent. If there is already a child in the direction you must move while traversing the tree, you |
15 |
| -# repeat the same rules until an avaialble space is found. |
16 |
| -# |
17 |
| -# Because BSTs are sorted in this fashion, operations of insertion, searching, and deleting is done faster than the linear time operations done on an |
18 |
| -# unsorted tree. Binary insert, search, and delete are completed in O(log n), "Logarmithmic" time, which is the fastest there is behind O(1), |
19 |
| -# "Constant time". |
20 |
| -# |
21 |
| -########## |
22 |
| -# Unit Tests |
23 |
| -########## |
24 |
| - |
25 |
| -require "test/unit" |
26 |
| - |
27 |
| -class NodeClassTest < Test::Unit::TestCase |
28 |
| - |
29 |
| - def test_node_can_be_created |
30 |
| - test = Node.new |
31 |
| - |
32 |
| - assert_not_equal(nil, test) |
33 |
| - assert_equal(nil, test.value) |
34 |
| - end |
35 |
| - |
36 |
| - def test_node_can_hold_value |
37 |
| - test = Node.new(5) |
38 |
| - |
39 |
| - assert_equal(5, test.value) |
40 |
| - assert_equal(nil, test.left_child) |
41 |
| - assert_equal(nil, test.right_child) |
42 |
| - end |
43 |
| - |
44 |
| - def test_node_can_point_to_other_nodes_through_children |
45 |
| - test = Node.new(14) |
46 |
| - test_left = Node.new(20) |
47 |
| - test_right = Node.new(9422) |
48 |
| - test.left_child = test_left |
49 |
| - test.right_child = test_right |
50 |
| - |
51 |
| - assert_equal(test.left_child.value, test_left.value) |
52 |
| - assert_equal(test.right_child.value, test_right.value) |
53 |
| - end |
54 |
| - |
55 |
| -end |
56 |
| - |
57 |
| -class BinarySearchTreeTest < Test::Unit::TestCase |
58 |
| - |
59 |
| - def test_binary_search_can_be_created |
60 |
| - test = BinarySearchTree.new |
61 |
| - |
62 |
| - assert_not_equal(nil, test) |
63 |
| - end |
64 |
| - |
65 |
| - def test_binary_search_tree_properties_exist |
66 |
| - test = BinarySearchTree.new |
67 |
| - |
68 |
| - assert_respond_to(test, :root) |
69 |
| - assert_respond_to(test, :size) |
70 |
| - end |
71 |
| - |
72 |
| - def test_binary_search_tree_methods_exist |
73 |
| - test = BinarySearchTree.new |
74 |
| - |
75 |
| - assert_respond_to(test, :insert) |
76 |
| - assert_respond_to(test, :search) |
77 |
| - assert_respond_to(test, :remove) |
78 |
| - end |
79 |
| - |
80 |
| - def test_binary_search_tree_insert_children |
81 |
| - test = BinarySearchTree.new |
82 |
| - test.insert(7) |
83 |
| - test.insert(20) |
84 |
| - test.insert(4) |
85 |
| - |
86 |
| - assert_equal(7, test.root.value) |
87 |
| - assert_equal(4, test.root.left_child.value) |
88 |
| - assert_equal(20, test.root.right_child.value) |
89 |
| - assert_equal(nil, test.root.right_child.right_child) |
90 |
| - assert_equal(nil, test.root.right_child.left_child) |
91 |
| - assert_equal(nil, test.root.left_child.right_child) |
92 |
| - assert_equal(nil, test.root.left_child.left_child) |
93 |
| - assert_equal(3, test.size) |
94 |
| - end |
95 |
| - |
96 |
| - def test_binary_search_tree_search_method_node_exists |
97 |
| - test = BinarySearchTree.new |
98 |
| - values = [4,9,141,48,7] |
99 |
| - values.each { |val| test.insert(val) } |
100 |
| - |
101 |
| - assert_equal(true, test.search(48)) |
102 |
| - end |
103 |
| - |
104 |
| - def test_binary_search_tree_search_method_node_does_not_exist |
105 |
| - test = BinarySearchTree.new |
106 |
| - values = [4,9,141,48,7] |
107 |
| - values.each { |val| test.insert(val) } |
108 |
| - |
109 |
| - assert_equal(false, test.search(8)) |
110 |
| - end |
111 |
| - |
112 |
| - |
113 |
| - def test_binary_search_tree_remove_method_deletes_value |
114 |
| - test = BinarySearchTree.new() |
115 |
| - values = [4,9,141,48,7] |
116 |
| - |
117 |
| - values.each { |val| test.insert(val) } |
118 |
| - |
119 |
| - assert_equal(5, test.size) |
120 |
| - assert_equal(true, test.search(4)) |
121 |
| - assert_equal(true, test.search(141)) |
122 |
| - assert_equal(true, test.search(9)) |
123 |
| - |
124 |
| - test.remove(4) |
125 |
| - assert_equal(4, test.size) |
126 |
| - assert_equal(false, test.search(4)) |
127 |
| - assert_equal(true, test.search(7)) |
128 |
| - assert_equal(true, test.search(141)) |
129 |
| - end |
130 |
| - |
131 |
| -end |
132 |
| - |
133 | 1 | #####################
|
134 | 2 | # Node class
|
135 | 3 | #####################
|
| 4 | + |
136 | 5 | class Node
|
137 | 6 |
|
138 | 7 | attr_accessor :value, :left_child, :right_child
|
@@ -241,3 +110,115 @@ def method_missing(name, *args, &block)
|
241 | 110 | @self.send(name, *args, &block)
|
242 | 111 | end
|
243 | 112 | end
|
| 113 | + |
| 114 | +############ |
| 115 | +# Unit Tests |
| 116 | +############ |
| 117 | + |
| 118 | +require "test/unit" |
| 119 | + |
| 120 | +class NodeClassTest < Test::Unit::TestCase |
| 121 | + |
| 122 | + def test_node_can_be_created |
| 123 | + test = Node.new |
| 124 | + |
| 125 | + assert_not_equal(nil, test) |
| 126 | + assert_equal(nil, test.value) |
| 127 | + end |
| 128 | + |
| 129 | + def test_node_can_hold_value |
| 130 | + test = Node.new(5) |
| 131 | + |
| 132 | + assert_equal(5, test.value) |
| 133 | + assert_equal(nil, test.left_child) |
| 134 | + assert_equal(nil, test.right_child) |
| 135 | + end |
| 136 | + |
| 137 | + def test_node_can_point_to_other_nodes_through_children |
| 138 | + test = Node.new(14) |
| 139 | + test_left = Node.new(20) |
| 140 | + test_right = Node.new(9422) |
| 141 | + test.left_child = test_left |
| 142 | + test.right_child = test_right |
| 143 | + |
| 144 | + assert_equal(test.left_child.value, test_left.value) |
| 145 | + assert_equal(test.right_child.value, test_right.value) |
| 146 | + end |
| 147 | + |
| 148 | +end |
| 149 | + |
| 150 | +class BinarySearchTreeTest < Test::Unit::TestCase |
| 151 | + |
| 152 | + def test_binary_search_can_be_created |
| 153 | + test = BinarySearchTree.new |
| 154 | + |
| 155 | + assert_not_equal(nil, test) |
| 156 | + end |
| 157 | + |
| 158 | + def test_binary_search_tree_properties_exist |
| 159 | + test = BinarySearchTree.new |
| 160 | + |
| 161 | + assert_respond_to(test, :root) |
| 162 | + assert_respond_to(test, :size) |
| 163 | + end |
| 164 | + |
| 165 | + def test_binary_search_tree_methods_exist |
| 166 | + test = BinarySearchTree.new |
| 167 | + |
| 168 | + assert_respond_to(test, :insert) |
| 169 | + assert_respond_to(test, :search) |
| 170 | + assert_respond_to(test, :remove) |
| 171 | + end |
| 172 | + |
| 173 | + def test_binary_search_tree_insert_children |
| 174 | + test = BinarySearchTree.new |
| 175 | + test.insert(7) |
| 176 | + test.insert(20) |
| 177 | + test.insert(4) |
| 178 | + |
| 179 | + assert_equal(7, test.root.value) |
| 180 | + assert_equal(4, test.root.left_child.value) |
| 181 | + assert_equal(20, test.root.right_child.value) |
| 182 | + assert_equal(nil, test.root.right_child.right_child) |
| 183 | + assert_equal(nil, test.root.right_child.left_child) |
| 184 | + assert_equal(nil, test.root.left_child.right_child) |
| 185 | + assert_equal(nil, test.root.left_child.left_child) |
| 186 | + assert_equal(3, test.size) |
| 187 | + end |
| 188 | + |
| 189 | + def test_binary_search_tree_search_method_node_exists |
| 190 | + test = BinarySearchTree.new |
| 191 | + values = [4,9,141,48,7] |
| 192 | + values.each { |val| test.insert(val) } |
| 193 | + |
| 194 | + assert_equal(true, test.search(48)) |
| 195 | + end |
| 196 | + |
| 197 | + def test_binary_search_tree_search_method_node_does_not_exist |
| 198 | + test = BinarySearchTree.new |
| 199 | + values = [4,9,141,48,7] |
| 200 | + values.each { |val| test.insert(val) } |
| 201 | + |
| 202 | + assert_equal(false, test.search(8)) |
| 203 | + end |
| 204 | + |
| 205 | + |
| 206 | + def test_binary_search_tree_remove_method_deletes_value |
| 207 | + test = BinarySearchTree.new() |
| 208 | + values = [4,9,141,48,7] |
| 209 | + |
| 210 | + values.each { |val| test.insert(val) } |
| 211 | + |
| 212 | + assert_equal(5, test.size) |
| 213 | + assert_equal(true, test.search(4)) |
| 214 | + assert_equal(true, test.search(141)) |
| 215 | + assert_equal(true, test.search(9)) |
| 216 | + |
| 217 | + test.remove(4) |
| 218 | + assert_equal(4, test.size) |
| 219 | + assert_equal(false, test.search(4)) |
| 220 | + assert_equal(true, test.search(7)) |
| 221 | + assert_equal(true, test.search(141)) |
| 222 | + end |
| 223 | + |
| 224 | +end |
0 commit comments