二叉树是非常重要的数据结构,其中一棵树最上面的点称为根节点,若是一个节点下面毗邻多个节点,那么该节点称为父节点,下面的节点称为子节点,二叉树的每一个节点最多有2个子节点,一个节点子节点的个数称为度,二叉树每个节点的度只能是0,1,2中的一个,度为0的节点称为叶节点。
用 *** 实现二叉树数据结构, 完成遍历、查找更大/小值、查找特定值以及删除节点的操作。
//界说节点 class Node { constructor(data){ this.root = this; this.data = data; this.left = null; this.right = null } } //建立二叉搜索树(BST)) class BinarySearchTree { constructor(){ this.root = null } //插入节点 insert(data){ const newNode = new Node(data); const insertNode = (node,newNode) => { if (newNode.data < node.data){ if(node.left === null){ node.left = newNode }else { insertNode(node.left,newNode) } }else { if(node.right === null){ node.right = newNode }else{ insertNode(node.right,newNode) } } }; if(!this.root){ this.root = newNode }else { insertNode(this.root,newNode) } } //中序遍历 inOrder(){ let backs = []; const inOrderNode = (node,callback) => { if(node !== null){ inOrderNode(node.left,callback); backs.push(callback(node.data)); inOrderNode(node.right,callback) } }; inOrderNode(this.root,callback); function callback(v){ return v } return backs } //前序遍历 preOrder(){ let backs = []; const preOrderNode = (node,callback) => { if(node !== null){ backs.push(callback(node.data)); preOrderNode(node.left,callback); preOrderNode(node.right,callback) } }; preOrderNode(this.root,callback); function callback(v){ return v } return backs } //后序遍历 postOrder(){ let backs = []; const postOrderNode = (node,callback) => { if(node !== null){ postOrderNode(node.left,callback); postOrderNode(node.right,callback); backs.push(callback(node.data)) } }; postOrderNode(this.root,callback); function callback(v){ return v } return backs } //查找最小值 getMin(node){ const minNode = node => { return node? (node.left? minNode(node.left):node):null }; return minNode( node || this.root) } //查找更大值 getMax(node){ const minNode = node => { return node? (node.right? minNode(node.right):node):null }; return minNode(node || this.root) } //查找特定值 find(data){ const findNode = (node,data) => { if(node===null) return false; if(node.data===data) return node; return findNode((data < node.data)? node.left: node.right,data) }; return findNode(this.root,data) } //删除节点 remove(data){ const removeNode = (node,data) => { if(node === null) return null; if(node.data === data){ if(node.left === null && node.right === null) return null; if(node.left === null) return node.right; if(node.right === null) return node.left; if(node.left !==null && node.right !==null){ let _node = this.getMin(node.right); node.data = _node.data; node.right = removeNode(node.right,data); return node } } else if(data < node.data){ node.left=removeNode(node.left,data); return node } else { node.right=removeNode(node.right,data); return node } }; return removeNode(this.root,data) } } //建立BST const tree = new BinarySearchTree(); tree.insert(11); tree.insert(7); tree.insert(5); tree.insert(3); tree.insert(9); tree.insert(8); tree.insert(10); tree.insert(13); tree.insert(12); tree.insert(14); tree.insert(20); tree.insert(18); tree.insert(25); console.log(tree); console.log(tree.root); //中序遍历BST console.log(tree.inOrder()); //前序遍历BST console.log(tree.preOrder()); //后序遍历BST console.log(tree.postOrder()); //搜索最小值 console.log(tree.getMin()); //搜索更大值 console.log(tree.getMax()); //查找特定值 console.log(tree.find(2)); console.log(tree.find(3)); console.log(tree.find(20)); //删除节点,返回新的二叉树,不改变原来的二叉树 console.log(tree.remove(11)); a=tree.remove(11); console.log(a.root); console.log(tree);
1.阿里云: 本站现在使用的是阿里云主机,平安/可靠/稳固。点击领取2000米代金券、领会最新阿里云产物的种种优惠流动点击进入
本文导读目录: 1、下列属于黑客常用的手段有( )。 2、黑客攻击主要有哪些手段? 3、黑客常用的是什么系统 4、黑客常用的攻击方式 5、黑客有哪些攻击手段? 下列属于黑客常用的手段...
如何可以盗取微信聊天记录,高手找回微信聊天记录步骤。如何查找删除的微信聊天记录?如何恢复微信聊天记录?许多用户因误操作而将微信联系人从最近的会话中删除,一些用户在使用微信的过程中遇到了系统崩溃。有许多...
米家的智能产物这几年较量受接待,尤其是一些小型的电器类产物。最近有留意到各人不知道米家智能插座三款有什么区别,许多人头疼不知道如何选购,下面就听智家网小编来聊一聊吧。米家智能插座三款有什么区别 ...
“唐山市伴游伴游旅行网25路-【鲜于访琴】” 中老年人伴游女学妹的发展前途4:知乎问答和抖音短视频也有伴游网址,均能够寻找北京市能给模特经纪人的联系电话。来自青岛消费者的评价:服务很很好,可以北京服务...
女性在怀孕期间,因为身体所需血液量的增加,加上为满足胎盘、胎儿生长发育所需,导致孕妇的血液容量会比平时增加4倍多。因为孕期的反应,致使孕妇对铁元素的营养摄入不足,最终导致孕妇的贫血现象,因此孕妇在怀孕...
声音总是比文字来的加倍直白和情深意切,因此我们在使用陌陌与人谈论主要的事情时,总是会发语音而不是打字。谈论的事情既然主要,同样的陌陌语音聊天记录也一样主要。那么陌陌怎么导出语音聊天记录呢...