From 10ff64e86efc90bff345c4d615057acf337f3327 Mon Sep 17 00:00:00 2001 From: chenshuai <2268380485@qq.com> Date: Sat, 12 Apr 2025 21:29:48 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B5=8B=E8=AF=95=E6=80=A7=E6=8F=90=E4=BA=A4:?= =?UTF-8?q?=E5=8F=88=E5=88=9B=E5=BB=BA=E4=B8=80=E4=B8=AA63=E8=A1=8C?= =?UTF-8?q?=E7=9A=84=E6=B5=8B=E8=AF=95=E6=80=A7=E6=96=87=E4=BB=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/other/tree2.js | 63 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 63 insertions(+) create mode 100644 src/other/tree2.js diff --git a/src/other/tree2.js b/src/other/tree2.js new file mode 100644 index 0000000..95f1dd2 --- /dev/null +++ b/src/other/tree2.js @@ -0,0 +1,63 @@ +//实现二叉树 +class BinaryTree { + constructor(data) { + this.data = data; + this.left = null; + this.right = null; + } +} +BinaryTree.prototype.insert = function(data) { + if (data < this.data) { + if (this.left === null) { + this.left = new BinaryTree(data); + } + } +} +BinaryTree.prototype.traverse = function(callback) { + if (this.left !== null) { + this.left.traverse(callback); + } +} +// 查找树 +BinaryTree.prototype.find = function(data) { + if (this.data === data) { + return this; + } + for (let i = 0; i < this.children.length; i++) { + let result = this.children[i].find(data); + if (result) { + return result; + } + } + return null; +} +// 删除树 +BinaryTree.prototype.remove = function(data) { + for (let i = 0; i < this.children.length; i++) { + if (this.children[i].data === data) { + this.children.splice(i, 1); + return; + } + } +} +// 清空树 +BinaryTree.prototype.clear = function() { + this.children = []; +} +// 树的高度 +BinaryTree.prototype.height = function() { + let height = 0; +} +// 树的深度 +BinaryTree.prototype.depth = function() { + let depth = 0; +} +// 树的大小 +BinaryTree.prototype.size = function() { + let size = 0; +} +// 树的叶子节点 +BinaryTree.prototype.leaf = function() { + let leaf = 0; +} +module.exports = BinaryTree; \ No newline at end of file