2022-01-21 01:00:18 +05:30
|
|
|
|
---
|
|
|
|
|
id: 587d8258367417b2b2512c81
|
|
|
|
|
title: 1 つの子を持つノードを二分探索木で削除する
|
|
|
|
|
challengeType: 1
|
|
|
|
|
forumTopicId: 301638
|
|
|
|
|
dashedName: delete-a-node-with-one-child-in-a-binary-search-tree
|
|
|
|
|
---
|
|
|
|
|
|
|
|
|
|
# --description--
|
|
|
|
|
|
|
|
|
|
葉ノードを削除できるようになったので、2 つ目のケースに進みましょう。つまり、1 つの子を持つノードを削除します。 このケースでは、ノード 1 - 2 - 3 を持つ木があるとします。ここで、1 は根です。 2 を削除するには、1 から 3 への適切な参照を行うだけです。 より一般的には、子を 1 つだけ持つノードを削除するには、そのノードの親がツリー内の次のノードを参照するようにします。
|
|
|
|
|
|
|
|
|
|
# --instructions--
|
|
|
|
|
|
|
|
|
|
前回のチャレンジのタスクを実行する `remove` メソッドに、既にいくらかコードが記述されています。 削除の対象ノードとその親を見つけ、対象ノードが持つ子の数を定義してください。 ここでは、子を 1 つだけ持つ対象ノードに対して次のケースを追加しましょう。 ここで、その単一の子がツリー内の左側の枝なのか右側の枝なのかを判断し、このノードを指す正しい参照を親に設定する必要があります。 さらに、対象ノードが根ノードであるケース (すなわち親ノードが `null`) を考えてみましょう。 提供されている開始コードをすべて独自のものに書き換えても構いません (それがテストに合格するコードであれば)。
|
|
|
|
|
|
|
|
|
|
# --hints--
|
|
|
|
|
|
|
|
|
|
`BinarySearchTree` データ構造が存在する必要があります。
|
|
|
|
|
|
|
|
|
|
```js
|
|
|
|
|
assert(
|
|
|
|
|
(function () {
|
|
|
|
|
var test = false;
|
|
|
|
|
if (typeof BinarySearchTree !== 'undefined') {
|
|
|
|
|
test = new BinarySearchTree();
|
|
|
|
|
}
|
|
|
|
|
return typeof test == 'object';
|
|
|
|
|
})()
|
|
|
|
|
);
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
二分探索木に `remove` というメソッドが必要です。
|
|
|
|
|
|
|
|
|
|
```js
|
|
|
|
|
assert(
|
|
|
|
|
(function () {
|
|
|
|
|
var test = false;
|
|
|
|
|
if (typeof BinarySearchTree !== 'undefined') {
|
|
|
|
|
test = new BinarySearchTree();
|
|
|
|
|
} else {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
return typeof test.remove == 'function';
|
|
|
|
|
})()
|
|
|
|
|
);
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
存在しない要素を削除しようとすると、`null` が返される必要があります。
|
|
|
|
|
|
|
|
|
|
```js
|
|
|
|
|
assert(
|
|
|
|
|
(function () {
|
|
|
|
|
var test = false;
|
|
|
|
|
if (typeof BinarySearchTree !== 'undefined') {
|
|
|
|
|
test = new BinarySearchTree();
|
|
|
|
|
} else {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
if (typeof test.remove !== 'function') {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
return test.remove(100) == null;
|
|
|
|
|
})()
|
|
|
|
|
);
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
根ノードに子がない場合は、根ノードを削除すると根が `null` に設定される必要があります。
|
|
|
|
|
|
|
|
|
|
```js
|
|
|
|
|
assert(
|
|
|
|
|
(function () {
|
|
|
|
|
var test = false;
|
|
|
|
|
if (typeof BinarySearchTree !== 'undefined') {
|
|
|
|
|
test = new BinarySearchTree();
|
|
|
|
|
} else {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
if (typeof test.remove !== 'function') {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
test.add(500);
|
|
|
|
|
test.remove(500);
|
|
|
|
|
return test.inorder() == null;
|
|
|
|
|
})()
|
|
|
|
|
);
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
`remove` メソッドは、葉ノードをツリーから削除する必要があります。
|
|
|
|
|
|
|
|
|
|
```js
|
|
|
|
|
assert(
|
|
|
|
|
(function () {
|
|
|
|
|
var test = false;
|
|
|
|
|
if (typeof BinarySearchTree !== 'undefined') {
|
|
|
|
|
test = new BinarySearchTree();
|
|
|
|
|
} else {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
if (typeof test.remove !== 'function') {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
test.add(5);
|
|
|
|
|
test.add(3);
|
|
|
|
|
test.add(7);
|
|
|
|
|
test.add(6);
|
|
|
|
|
test.add(10);
|
|
|
|
|
test.add(12);
|
|
|
|
|
test.remove(3);
|
|
|
|
|
test.remove(12);
|
|
|
|
|
test.remove(10);
|
|
|
|
|
return test.inorder().join('') == '567';
|
|
|
|
|
})()
|
|
|
|
|
);
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
`remove` メソッドは、子を 1 つ持つノードを削除する必要があります。
|
|
|
|
|
|
|
|
|
|
```js
|
|
|
|
|
assert(
|
|
|
|
|
(function () {
|
|
|
|
|
var test = false;
|
|
|
|
|
if (typeof BinarySearchTree !== 'undefined') {
|
|
|
|
|
test = new BinarySearchTree();
|
|
|
|
|
} else {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
if (typeof test.remove !== 'function') {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
2022-02-04 00:46:32 +05:30
|
|
|
|
test.add(1);
|
|
|
|
|
test.add(4);
|
2022-01-21 01:00:18 +05:30
|
|
|
|
test.add(3);
|
2022-02-04 00:46:32 +05:30
|
|
|
|
test.add(2);
|
|
|
|
|
test.add(6);
|
|
|
|
|
test.add(8);
|
|
|
|
|
test.remove(6);
|
2022-01-21 01:00:18 +05:30
|
|
|
|
test.remove(3);
|
2022-02-04 00:46:32 +05:30
|
|
|
|
return test.inorder().join('') == '1248';
|
2022-01-21 01:00:18 +05:30
|
|
|
|
})()
|
|
|
|
|
);
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
2 つのノードを持つ木の根を削除すると、2 番目のノードが根として設定される必要があります。
|
|
|
|
|
|
|
|
|
|
```js
|
|
|
|
|
assert(
|
|
|
|
|
(function () {
|
|
|
|
|
var test = false;
|
|
|
|
|
if (typeof BinarySearchTree !== 'undefined') {
|
|
|
|
|
test = new BinarySearchTree();
|
|
|
|
|
} else {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
if (typeof test.remove !== 'function') {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
test.add(15);
|
|
|
|
|
test.add(27);
|
|
|
|
|
test.remove(15);
|
|
|
|
|
return test.inorder().join('') == '27';
|
|
|
|
|
})()
|
|
|
|
|
);
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
# --seed--
|
|
|
|
|
|
|
|
|
|
## --after-user-code--
|
|
|
|
|
|
|
|
|
|
```js
|
|
|
|
|
BinarySearchTree.prototype = Object.assign(
|
|
|
|
|
BinarySearchTree.prototype,
|
|
|
|
|
{
|
|
|
|
|
add: function(value) {
|
|
|
|
|
var node = this.root;
|
|
|
|
|
if (node == null) {
|
|
|
|
|
this.root = new Node(value);
|
|
|
|
|
return;
|
|
|
|
|
} else {
|
|
|
|
|
function searchTree(node) {
|
|
|
|
|
if (value < node.value) {
|
|
|
|
|
if (node.left == null) {
|
|
|
|
|
node.left = new Node(value);
|
|
|
|
|
return;
|
|
|
|
|
} else if (node.left != null) {
|
|
|
|
|
return searchTree(node.left);
|
|
|
|
|
}
|
|
|
|
|
} else if (value > node.value) {
|
|
|
|
|
if (node.right == null) {
|
|
|
|
|
node.right = new Node(value);
|
|
|
|
|
return;
|
|
|
|
|
} else if (node.right != null) {
|
|
|
|
|
return searchTree(node.right);
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return searchTree(node);
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
inorder: function() {
|
|
|
|
|
if (this.root == null) {
|
|
|
|
|
return null;
|
|
|
|
|
} else {
|
|
|
|
|
var result = new Array();
|
|
|
|
|
function traverseInOrder(node) {
|
|
|
|
|
if (node.left != null) {
|
|
|
|
|
traverseInOrder(node.left);
|
|
|
|
|
}
|
|
|
|
|
result.push(node.value);
|
|
|
|
|
if (node.right != null) {
|
|
|
|
|
traverseInOrder(node.right);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
traverseInOrder(this.root);
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
);
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
## --seed-contents--
|
|
|
|
|
|
|
|
|
|
```js
|
|
|
|
|
var displayTree = tree => console.log(JSON.stringify(tree, null, 2));
|
|
|
|
|
function Node(value) {
|
|
|
|
|
this.value = value;
|
|
|
|
|
this.left = null;
|
|
|
|
|
this.right = null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function BinarySearchTree() {
|
|
|
|
|
this.root = null;
|
|
|
|
|
this.remove = function(value) {
|
|
|
|
|
if (this.root === null) {
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
var target;
|
|
|
|
|
var parent = null;
|
|
|
|
|
// Find the target value and its parent
|
|
|
|
|
(function findValue(node = this.root) {
|
|
|
|
|
if (value == node.value) {
|
|
|
|
|
target = node;
|
|
|
|
|
} else if (value < node.value && node.left !== null) {
|
|
|
|
|
parent = node;
|
|
|
|
|
return findValue(node.left);
|
|
|
|
|
} else if (value < node.value && node.left === null) {
|
|
|
|
|
return null;
|
|
|
|
|
} else if (value > node.value && node.right !== null) {
|
|
|
|
|
parent = node;
|
|
|
|
|
return findValue(node.right);
|
|
|
|
|
} else {
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
}.bind(this)());
|
|
|
|
|
if (target === null) {
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
// Count the children of the target to delete
|
|
|
|
|
var children =
|
|
|
|
|
(target.left !== null ? 1 : 0) + (target.right !== null ? 1 : 0);
|
|
|
|
|
// Case 1: Target has no children
|
|
|
|
|
if (children === 0) {
|
|
|
|
|
if (target == this.root) {
|
|
|
|
|
this.root = null;
|
|
|
|
|
} else {
|
|
|
|
|
if (parent.left == target) {
|
|
|
|
|
parent.left = null;
|
|
|
|
|
} else {
|
|
|
|
|
parent.right = null;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
// Case 2: Target has one child
|
|
|
|
|
// Only change code below this line
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
# --solutions--
|
|
|
|
|
|
|
|
|
|
```js
|
|
|
|
|
// solution required
|
|
|
|
|
```
|