4.3 KiB
4.3 KiB
id, challengeType, videoUrl, localeTitle
id | challengeType | videoUrl | localeTitle |
---|---|---|---|
587d8258367417b2b2512c80 | 1 | 删除二进制搜索树中的叶节点 |
Description
remove
。我们将在这里为我们的删除操作构建逻辑。首先,您需要在remove中创建一个函数,该函数在当前树中找到我们尝试删除的节点。如果树中不存在该节点,则remove
应返回null
。现在,如果目标节点是没有子节点的叶节点,则应将其父节点引用设置为null
。这有效地从树中删除节点。为此,您必须跟踪我们尝试删除的节点的父节点。创建一种跟踪目标节点具有的子节点数的方法也很有用,因为这将确定我们的删除属于哪种情况。我们将在下一次挑战中处理第二和第三个案例。祝你好运! Instructions
Tests
tests:
- text: 存在<code>BinarySearchTree</code>数据结构。
testString: assert((function() { var test = false; if (typeof BinarySearchTree !== 'undefined') { test = new BinarySearchTree() }; return (typeof test == 'object')})());
- text: 二叉搜索树有一个名为<code>remove</code>的方法。
testString: assert((function() { var test = false; if (typeof BinarySearchTree !== 'undefined') { test = new BinarySearchTree() } else { return false; }; return (typeof test.remove == 'function')})());
- text: 尝试删除不存在的元素将返回<code>null</code> 。
testString: 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); })());
- text: 如果根节点没有子节点,则删除它会将根节点设置为<code>null</code> 。
testString: 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); })());
- text: <code>remove</code>方法从树中删除叶节点
testString: 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'); })());
Challenge Seed
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;
// case 1: target has no children, change code below this line
}
After Test
console.info('after the test');
Solution
// solution required
/section>