summaryrefslogtreecommitdiff
path: root/spec/data_structures/avl_tree_spec.rb
blob: ab63bdcbdcce82d1fe64d9c54d7c0a9fcc2586e9 (plain)
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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
require "spec_helper"

describe AVLTree do
  subject { AVLTree.new }

  context "when empty" do
    it "should have a size of 0" do
      expect(subject.size).to eq(0)
    end

    it "should have have a height of 0" do
      expect(subject.height).to eq(0)
    end
  end

  context "when a single item is added" do
    before :each do
      subject.push("A")
    end

    it "should have a size of 1" do
      expect(subject.size).to eq(1)
    end

    it "should have a height of 1" do
      expect(subject.height).to eq(1)
    end
  end

  context "when two items are added" do
    before :each do
      subject.add("a")
      subject.add("b")
    end

    it "should have a size of 2" do
      expect(subject.size).to eq(2)
    end
    it "should have a height of 2" do
      expect(subject.height).to eq(2)
    end
  end

  context "when a balanced tree has 3 items" do
    before :each do
      subject.add("b")
      subject.add("a")
      subject.add("c")
    end

    it "should have a size of 3" do
      expect(subject.size).to eq(3)
    end

    it "should have a height of 2" do
      expect(subject.height).to eq(2)
    end
  end

  context "when the tree is unbalanced" do
    context "with a right-right case" do
      before :each do
        subject.add("a")
        subject.add("b")
        subject.add("c")
      end

      it "should re-balance it self" do
        expect(subject.height).to eq(2)
      end

      it "should have a new root" do
        expect(subject.root.data).to eq("b")
      end

      it "should change the left side" do
        expect(subject.root.left.data).to eq("a")
      end

      it "should change the right side" do
        expect(subject.root.right.data).to eq("c")
      end
    end

    context "with a left-left case" do
      before :each do
        subject.add("c")
        subject.add("b")
        subject.add("a")
      end

      it "should re-balance it self" do
        expect(subject.height).to eq(2)
      end

      it "should have a new root" do
        expect(subject.root.data).to eq("b")
      end

      it "should change the left side" do
        expect(subject.root.left.data).to eq("a")
      end

      it "should change the right side" do
        expect(subject.root.right.data).to eq("c")
      end
    end

    context "with a left-right case" do
      before :each do
        subject.add(5)
        subject.add(3)
        subject.add(4)
      end

      it "should adjust the height" do
        expect(subject.height).to eq(2)
      end

      it "should have a new root" do
        expect(subject.root.data).to eq(4)
      end

      it "should have a proper left side" do
        expect(subject.root.left.data).to eq(3)
      end

      it "should have a proper right side" do
        expect(subject.root.right.data).to eq(5)
      end
    end

    context "with a right-left case" do
      before :each do
        subject.add(3)
        subject.add(5)
        subject.add(4)
      end

      it "should adjust the height" do
        expect(subject.height).to eq(2)
      end

      it "should have a new root" do
        expect(subject.root.data).to eq(4)
      end

      it "should have a proper left side" do
        expect(subject.root.left.data).to eq(3)
      end

      it "should have a proper right side" do
        expect(subject.root.right.data).to eq(5)
      end
    end
  end
end