forked from AdaGold/tree-practice
-
Notifications
You must be signed in to change notification settings - Fork 44
/
Copy pathtree_test.rb
127 lines (97 loc) · 3.46 KB
/
tree_test.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
require_relative 'test_helper'
require 'pry'
Minitest::Reporters.use! Minitest::Reporters::SpecReporter.new
describe Tree do
let (:tree) {Tree.new}
let (:tree_with_nodes) {
tree.add(5, "Peter")
tree.add(3, "Paul")
tree.add(1, "Mary")
tree.add(10, "Karla")
tree.add(15, "Ada")
tree.add(25, "Kari")
tree
}
it "add & find values" do
tree.add(5, "Peter")
expect(tree.find(5)).must_equal "Peter"
tree.add(15, "Ada")
expect(tree.find(15)).must_equal "Ada"
tree.add(3, "Paul")
expect(tree.find(3)).must_equal "Paul"
end
it "can't find anything when the tree is empty" do
expect(tree.find(50)).must_be_nil
end
describe "inorder" do
it "will give an empty array for an empty tree" do
expect(tree.inorder).must_equal []
end
it "will return the tree in order" do
expect(tree_with_nodes.inorder).must_equal [{:key=>1, :value=>"Mary"}, {:key=>3, :value=>"Paul"},
{:key=>5, :value=>"Peter"}, {:key=>10, :value=>"Karla"},
{:key=>15, :value=>"Ada"}, {:key=>25, :value=>"Kari"}]
end
end
describe "preorder" do
it "will give an empty array for an empty tree" do
expect(tree.preorder).must_equal []
end
it "will return the tree in preorder" do
expect(tree_with_nodes.preorder).must_equal [{:key=>5, :value=>"Peter"}, {:key=>3, :value=>"Paul"},
{:key=>1, :value=>"Mary"}, {:key=>10, :value=>"Karla"},
{:key=>15, :value=>"Ada"}, {:key=>25, :value=>"Kari"}]
end
end
describe "postorder" do
it "will give an empty array for an empty tree" do
expect(tree.postorder).must_equal []
end
it "will return the tree in postorder" do
expect(tree_with_nodes.postorder).must_equal [{:key=>1, :value=>"Mary"}, {:key=>3, :value=>"Paul"},
{:key=>25, :value=>"Kari"}, {:key=>15, :value=>"Ada"},
{:key=>10, :value=>"Karla"}, {:key=>5, :value=>"Peter"}]
end
end
xdescribe "breadth first search" do
it "will give an empty array for an empty tree" do
expect(tree.bfs).must_equal []
end
it "will return an array of a level-by-level output of the tree" do
expect(tree_with_nodes.bfs).must_equal [{:key=>5, :value=>"Peter"}, {:key=>3, :value=>"Paul"},
{:key=>10, :value=>"Karla"}, {:key=>1, :value=>"Mary"},
{:key=>15, :value=>"Ada"}, {:key=>25, :value=>"Kari"}]
end
end
describe "height" do
it "will return 0 for an empty tree" do
my_tree = Tree.new
expect(my_tree.height).must_equal 0
end
it "will return 1 for a tree of height 1" do
my_tree = Tree.new
my_tree.add(100)
expect(my_tree.height).must_equal 1
end
it "will report the height for a balanced tree" do
expect(tree_with_nodes.height).must_equal 4
end
it "will report the height for unbalanced trees" do
my_tree = Tree.new
my_tree.add(100)
my_tree.add(110)
my_tree.add(120)
my_tree.add(130)
my_tree.add(140)
expect(my_tree.height).must_equal 5
my_tree = Tree.new
my_tree = Tree.new
my_tree.add(100)
my_tree.add(90)
my_tree.add(80)
my_tree.add(70)
my_tree.add(60)
expect(my_tree.height).must_equal 5
end
end
end