js二叉搜索树和图

网友投稿 392 2022-11-10

js二叉搜索树和图

js二叉搜索树和图

//封装二分搜索树 function BinarySerachTree(){ function Node(key){ this.key=key this.left=null this.right=null } //属性 this.root=null //方法 //插入数据 BinarySerachTree.prototype.insert=function(key){ //1.判断 key创建节点 var newNode=new Node(key) //2.判断接节点是否有值 if(this.root==null){ this.root=newNode }else{ this.insertNode(this.root,newNode) } } BinarySerachTree.prototype.insertNode=function(node,newNode){ if(newNode.keykey){ //传入的 key 较小,向左继续查找 return this.searchNode(node.left,key) }else if(node.keynode.key){ node=node.right }else{ return true } } return false } //删除节点 BinarySerachTree.prototype.remove=function(key){ //1.寻找要删除的节点 //1.1定义变量,保存一些信息 var current=this.root var parent=null var isLeftChild=true //1.2开始寻找删除的节点 while(current.key!=key){ parent=current if(key

红黑树的特点:

图的特点:

图:

//封装图结构 function Graph(){ //属性 顶点(数组) /边(字典) this.vertexes=[] //顶点 this.edges=new Dictionary() //边 //方法 //1.添加顶点的方法 Graph.prototype.addVertex=function(v){ this.vertexes.push(v) this.edges.set(v,[]) } //2.添加边的方法 Graph.prototype.addEdge=function(v1,v2){ this.edges.get(v1).push(v2) this.edges.get(v1).push(v1) } //三.实现toString方法:转换为邻接表形式 Graph.prototype.toString = function (){ //1.定义字符串,保存最终结果 let resultString = "" //2.遍历所有的顶点以及顶点对应的边 for (let i = 0; i < this.vertexes.length; i++) {//遍历所有顶点 resultString += this.vertexes[i] + '-->' let vEdges = this.edges.get(this.vertexes[i]) for (let j = 0; j < vEdges.length; j++) {//遍历字典中每个顶点对应的数组 resultString += vEdges[j] + ' '; } resultString += '\n' } return resultString } //初始化状态颜色 Graph.prototype.initializeColor=function(){ var colors=[] for(var i=0;i { this.items.push(element) } // 2.从队列中删除前端元素 Queue.prototype.dequeue = () => { return this.items.shift() } // 3.查看前端的元素 Queue.prototype.front = () => { return this.items[0] } // 4.查看队列是否为空 Queue.prototype.isEmpty = () => { return this.items.length == 0; } // 5.查看队列中元素的个数 Queue.prototype.size = () => { return this.items.length } // 6.toString方法 Queue.prototype.toString = () => { let resultString = '' for (let i of this.items){ resultString += i + ' ' } return resultString } }

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:springboot oauth2实现单点登录实例
下一篇:Ext.js项目(二)
相关文章

 发表评论

暂时没有评论,来抢沙发吧~