summaryrefslogtreecommitdiff
path: root/spec/binary_trees/delete_from_bst_spec.rb
blob: 3bc85d0f6a83c79e7d24257328b49bafaad4a7bc (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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
<<-DOC
A tree is considered a binary search tree (BST) if for each of its nodes the following is true:

The left subtree of a node contains only nodes with keys less than the node's key.
The right subtree of a node contains only nodes with keys greater than the node's key.
Both the left and the right subtrees must also be binary search trees.
Removing a value x from a BST t is done in the following way:

If there is no x in t, nothing happens;
Otherwise, let t' be a subtree of t such that t'.value = x.
If t' has a left subtree, remove the rightmost node from it and put it at the root of t';
Otherwise, remove the root of t' and its right subtree becomes the new t's root.
For example, removing 4 from the following tree has no effect because there is no such value in the tree:

    5
   / \
  2   6
 / \   \
1   3   8
       /
      7
Removing 5 causes 3 (the rightmost node in left subtree) to move to the root:

    3
   / \
  2   6
 /     \
1       8
       /
      7
And removing 6 after that creates the following tree:

    3
   / \
  2   8
 /   /
1   7
You're given a binary search tree t and an array of numbers queries. Your task is to remove queries[0], queries[1],
etc., from t, step by step, following the algorithm above. Return the resulting BST.

Example

For

t = {
    "value": 5,
    "left": {
        "value": 2,
        "left": {
            "value": 1,
            "left": null,
            "right": null
        },
        "right": {
            "value": 3,
            "left": null,
            "right": null
        }
    },
    "right": {
        "value": 6,
        "left": null,
        "right": {
            "value": 8,
            "left": {
                "value": 7,
                "left": null,
                "right": null
            },
            "right": null
        }
    }
}
and queries = [4, 5, 6], the output should be

deleteFromBST(t, queries) = {
    "value": 3,
    "left": {
        "value": 2,
        "left": {
            "value": 1,
            "left": null,
            "right": null
        },
        "right": null
    },
    "right": {
        "value": 8,
        "left": {
            "value": 7,
            "left": null,
            "right": null
        },
        "right": null
    }
}
Input/Output

[time limit] 4000ms (rb)
[input] tree.integer t

A tree of integers.

Guaranteed constraints:
0 ≤ t size ≤ 1000,
-109 ≤ node value ≤ 109.

[input] array.integer queries

An array that contains the numbers to be deleted from t.

Guaranteed constraints:
1 ≤ queries.length ≤ 1000,
-109 ≤ queries[i] ≤ 109.

[output] tree.integer

The tree after removing all the numbers in queries, following the algorithm above.
DOC

describe "#delete_from_bst" do
  <<-DOC
  If there is no x in t, nothing happens;
  Otherwise, let t' be a subtree of t such that t'.value = x.
  If t' has a left subtree, remove the rightmost node from it and put it at the root of t';
  Otherwise, remove the root of t' and its right subtree becomes the new t's root.

  If t' has a left subtree and the left subtree has a child on the right, 
  the node you should put at the root of t' is the rightmost node (not necessarily leaf) in this subtree. 
  When you remove the rightmost node, you must also discard its children 
  (rather than keeping them as children of the rightmost node's parent).

    3
   / \
  2   5
 /
1
remove 3:

  2
 / \
1   5

remove 2:

  1
   \
    5

remove 1

  5



And removing 6 after that creates the following tree:

{ value: 3, left: { value: 2, left: { value: 1 } }, right: { value: 6, left: { value: 5 }, right: { value: 8, left: { value: 7 } } } }
    3
   / \
  2   6
 /  /  \
1  5    8
       /
      7

{ value: 3, left: { value: 2, left: { value: 1 } }, right: { value: 5, right: { value: 8, left: { value: 7 } } } }
    3
   / \
  2   5
 /     \
1       8
       /
      7


Instead of recursively removing the largest node from the right subtree using the specified algorithm,
they want you to take the largest node's left subtree and make it the child of the largest node's parent
  DOC

  def remove(tree, target)
    return nil if tree.nil?

    if target < tree.value
      tree.left = remove(tree.left, target)
    elsif tree.value < target
      tree.right = remove(tree.right, target)
    else
      if tree.left.nil?
        return tree.right
      elsif tree.right.nil?
        return tree.left
      else
        max = tree.left
        max = max.right while max.right
        min = tree.right
        min = min.left while min.left

        puts [max&.value, min&.value].inspect

        tree.value = max.value
        tree.left = remove(tree.left, tree.value)

        #tree.value = min.value
        #tree.right = remove(tree.right, tree.value)

      end
    end
    tree
  end

  def delete_from_bst(tree, queries)
    return nil if tree.nil?
    queries.each { |query| tree = remove(tree, query) }
    tree
  end

  null = nil
  [
    { t: { "value": 5, "left": { "value": 2, "left": { "value": 1, "left": null, "right": null }, "right": { "value": 3, "left": null, "right": null } }, "right": { "value": 6, "left": null, "right": { "value": 8, "left": { "value": 7, "left": null, "right": null }, "right": null } } }, queries: [4, 5, 6], x: { "value": 3, "left": { "value": 2, "left": { "value": 1, "left": null, "right": null }, "right": null }, "right": { "value": 8, "left": { "value": 7, "left": null, "right": null }, "right": null } } },
    { t: null, queries: [1, 2, 3, 5], x: nil },
    { t: { "value": 3, "left": { "value": 2, "left": null, "right": null }, "right": null }, queries: [1, 2, 3, 5], x: nil },
    { t: { "value": 3, "left": { "value": 2, "left": null, "right": null }, "right": { "value": 5, "left": null, "right": null } }, queries: [1, 2, 3, 5], x: nil },
    { t: { "value": 3, "left": { "value": 2, "left": { "value": 1, "left": null, "right": null }, "right": null }, "right": { "value": 5, "left": null, "right": null } }, queries: [3, 2, 1], x: { "value": 5, "left": null, "right": null } },
    { t: { "value": 5, "left": { "value": 3, "left": { "value": 1, "left": null, "right": null }, "right": { "value": 4, "left": null, "right": null } }, "right": { "value": 7, "left": null, "right": null } }, queries: [1, 7, 4, 6], x: { "value": 5, "left": { "value": 3, "left": null, "right": null }, "right": null } },
    { t: { value: 3, left: { value: 2, left: { value: 1 } }, right: { value: 6, left: { value: 5 }, right: { value: 8, left: { value: 7 } } } }, queries: [6], x: { value: 3, left: { value: 2, left: { value: 1 } }, right: { value: 5, right: { value: 8, left: { value: 7 } } } } },
    { t: { "value": 3, "left": { "value": 2, "left": { "value": 1, "left": null, "right": null }, "right": null }, "right": { "value": 5, "left": null, "right": null } }, queries: [3], x: { "value": 2, "left": { "value": 1, "left": null, "right": null }, "right": { "value": 5, "left": null, "right": null } } },
    { t: null, queries: [100000000, -1000000000, 0, 1, -1, 2, -2], x: null },
    { t: { "value": 3, "left": { "value": 2, "left": null, "right": null }, "right": { "value": 5, "left": null, "right": null } }, queries: [2, 3, 0, 5], x: nil },
  ].each do |x|
    it do
      result = delete_from_bst(Tree.build_from(x[:t]), x[:queries])
      expected = x[:x] ? Tree.build_from(x[:x]).to_s : nil
      expect(result ? result.to_s : result).to eql(expected)
    end
  end

  it do
    require_relative 'spec_8'
    x = SPEC8
    result = delete_from_bst(Tree.build_from(x[:t]), x[:queries])
    expected = x[:x] ? Tree.build_from(x[:x]).to_s : nil
    expect(result ? result.to_s : result).to eql(expected)
  end

  it do
    require_relative 'spec_9'
    x = SPEC9
    result = delete_from_bst(Tree.build_from(x[:t]), x[:queries])
    expected = x[:x] ? Tree.build_from(x[:x]).to_s : nil
    expect(result ? result.to_s : result).to eql(expected)
  end

  it do
    require_relative 'spec_10'
    x = SPEC10
    result = delete_from_bst(Tree.build_from(x[:t]), x[:queries])
    expected = x[:x] ? Tree.build_from(x[:x]).to_s : nil
    expect(result ? result.to_s : result).to eql(expected)
  end
end