Code Monkey home page Code Monkey logo

Comments (2)

HcySunYang avatar HcySunYang commented on June 15, 2024 1

@Lucien-X 首先,感谢你的纠正。拼写错误已经修改。
然后你对 find 方法的改进体现了你对待问题的严谨性,这应该是一个程序员应该有的特质。本例中的 find 方法摘自 《数据结构与算法 JavaScript 描述》一书,论严谨,书中的例子有很多都是不严谨的地方,但这并不妨碍它教会我们思路,正如当你看到这个 find 方法的时候所提出的这些问题,我想当你将这些数据结构应用到具体的项目中时,或许除了思路对我们是有用的,里面的逻辑都要根据具体的项目进行相应的修改,比如各种边界情况的判断。所以我没有修改例子中的 find 方法,希望你能明白我的意思。
关于树的按层遍历,实际上在二叉树中是应用的不多的,特别是特殊的二叉树,比如二叉查找树(排序树),所以这里并没有介绍,而在介绍图的时候,其广度优先搜索即是按层遍历的例子,我想思路也是一样,不过我倒是可以考虑加进来。
最后再次感谢你所提出的问题。

from knowledge.

Lucien-X avatar Lucien-X commented on June 15, 2024

首先还是十分感谢您所做的翻译和整理,图示很清晰,去看了下原书的PDF,错误太多导致无力吐槽了,才发现你能描述成这样已经实属不易。《数据结构与算法 JavaScript 描述》一书中确实有不少不严谨的地方,但是吸收其中思路还是很赞的。
还有,刚刚还发现了,二叉搜索树中的key应该都是唯一的,但是算法中使用else覆盖了大于和等于的情况,导致重复节点会添加到右子树。

insert:function(data){
		// 1、根据输入的数据 data 创建一个新的节点。
		var n=new Node(data);
		// 2、检查是否有根节点,如果没有根节点证明这是一颗新树,将该节点作为根节点。
		if(!this.root){
			this.root=n;
			return;
		}
		// 3、否则,开始遍历树,将根节点设为当前节点,使用新节点与当前节点作比较,
		// 如果新节点的值小于当前节点:
		// 3.1、如果当前节点的左子节点为null,则将新节点设为当前节点的左子节点,退出循环。
		// 3.2、如果当前节点的左子节点不为null,则更新当前节点为当前节点的左子节点,执行下一次循环。
		var current=this.root;
		while(true){
			if(data<current.data){
				if(!current.left){
					current.left=n;
					break;
				}
				current=current.left;
			}
			if(data>current.data){
				if(!current.right){
					current.right=n;
					break;
				}
				current=current.right;
			}
			// 由于二叉树定义中,每个结点都有一个作为搜索依据的关键码(key),
			// 所有结点的关键码互不相同。
			// 当插入值已存在,不再重复插入
			if(data==current.data){
				break;
			}
		}
	},

from knowledge.

Related Issues (8)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.