标准二叉树

<!doctype html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<title>BST</title>
</head>
<body>
	
</body>
</html>
<script>
	function Node(data,left,right){
		this.data = data;
		this.left = left;
		this.right = right;
		this.count = 1;
		this.show=function(){
			return this.data;
		}
	}
	function BST(){
		this.root = null;
	}
	BST.prototype={
		//增加计数
		insert:function(data){
			var target = this.find(data);
			if(target === null){
				var n = new Node(data,null,null);
				var temp,parent;
				if(this.root === null){
					this.root = n;
				}else{
					temp = this.root;
					while(temp !== null){
						parent = temp;
						if(n.data < temp.data){
							temp = temp.left;
							if(temp === null){
								parent.left = n;
								break;
							}
						}else{
							temp = temp.right;
							if(temp === null){
								parent.right = n;
								break;
							}
						}	
					}
				}	
			}else{
				target.count++;
			}
		},
		/*三种遍历方法*/
		//第一种,中序,最常见方法;
		midShow:function(node=this.root){
			var str = "";
			if(node !== null){
				str += this.midShow(node.left)
				str += node.show()+"-->"+node.count+",";
				str += this.midShow(node.right);
			}
			return str;
		},
		//第二种:先序
		preShow:function(node=this.root){
			var str = "";
			if(node !== null){
				str += node.show()+"-->"+node.count+",";
				str += this.preShow(node.left);
				str += this.preShow(node.right);
			}
			return str;
		},
		//第三种:后序
		laterShow:function(node=this.root){
			var str = "";
			if(node !== null){
				str += this.preShow(node.left);
				str += this.preShow(node.right);
				str += node.show()+"-->"+node.count+",";
			}
			return str;
		},
		/*查找最小节点和最大节点*/
		max:function(){
			var temp = this.root;
			while(temp !== null){
				if(temp.right !== null){
					temp = temp.right;
				}else{
					return temp;
				}
			}
		},
		min:function(){
			var temp = this.root;
			while(temp !== null){
				if(temp.left !== null){
					temp = temp.left;
				}else{
					return temp;
				}
			}
		},
		/*删除节点*/
		//找到节点
		find:function(data){
			var temp = this.root;
			while(temp !== null){
				if(temp.data === data){
					return temp;
				}else if(data < temp.data){
					temp = temp.left;
				}else{
					temp = temp.right;
				}
			}	
			return null;
		},
		//找到具体节点下最小节点
		getThemin:function(node=this.root){
			var temp = node;
			while(temp !== null){
				if(temp.left !== null){
					temp = temp.left;
				}else{
					return temp;
				}
			}
		},
		// 移除节点,三种情况,第一种,没有子节点;第二种:只有一个子节点;第三种:有两个子节点;
		remove:function(data){
			this.root = this.removeNode(this.root,data);
		},
		removeNode:function(node=this.root,data){
			var current = node;
			if(current === null){
				return null;
			}
			if(data === current.data){
				if(current.left === null && current.right === null){
					return null;
				}else if(current.left === null){
					return current.right;
				}else if(current.right === null){
					return current.left;
				}else{
					var temp = this.getThemin(current.right);
					current.data =  temp.data;
					current.right = this.removeNode(current.right,temp.data);
					return current;
				}
			}else if(data < current.data){
				current.left = this.removeNode(current.left,data);
				return current;
			}else{
				current.right = this.removeNode(current.right,data);
				return current;
			}
		},
		//返回节点个数
		getCount:function(node=this.root){
			var temp = node;
			var count = 0;
			if(temp !== null){
				count++;
				count+= this.getCount(temp.left);
				count+= this.getCount(temp.right);
			}
			return count;
		},
		//返回边的个数
		getSides:function(node=this.root){
			var count = this.getCount(node);
			return count > 0 ? count-1 : null;
		}

	}
	var bst = new BST();
	bst.insert(10);
	bst.insert(13);
	bst.insert(23);
	bst.insert(1);
	bst.insert(42);
	bst.insert(11);
	bst.insert(23);
	bst.insert(1);
	bst.insert(42);
	bst.insert(11);
	bst.insert(123);
	bst.insert(213);
	bst.insert(322);
	bst.insert(121);
	console.log(bst.midShow());
	console.log(bst.preShow());
	console.log(bst.laterShow());
	console.log(bst.max())
	console.log(bst.min())
	console.log(bst.find(121));
	bst.remove(213)
	console.log(bst)
	console.log(bst.midShow());
	console.log(bst.getCount())
	console.log(bst.getSides())
</script>
<script>
	function Node(data,left,right){
		this.content = data;
		this.data = getData(data);
		this.left = left;
		this.right = right;
		this.count = 1;
		this.show=function(){
			return this.content;
		}
		function getData(data){
			var num = 0;
			for(var i = 0,len=data.length;i<len;i++){
				num += data.charCodeAt(i)*len;
			}
			return num;
		}
	}
	function BST(){
		this.root = null;
	}
	BST.prototype={
		//增加计数
		insert:function(data){
			var target = this.find(data);
			if(target === null){
				var n = new Node(data,null,null);
				var temp,parent;
				if(this.root === null){
					this.root = n;
				}else{
					temp = this.root;
					while(temp !== null){
						parent = temp;
						if(n.data < temp.data){
							temp = temp.left;
							if(temp === null){
								parent.left = n;
								break;
							}
						}else{
							temp = temp.right;
							if(temp === null){
								parent.right = n;
								break;
							}
						}	
					}
				}	
			}else{
				target.count++;
			}
		},
		/*三种遍历方法*/
		//第一种,中序,最常见方法;
		midShow:function(node=this.root){
			var str = "";
			if(node !== null){
				str += this.midShow(node.left)
				str += node.show()+":"+node.count+"\n";
				str += this.midShow(node.right);
			}
			return str;
		},
		/*查找最小节点和最大节点*/
		max:function(){
			var temp = this.root;
			while(temp !== null){
				if(temp.right !== null){
					temp = temp.right;
				}else{
					return temp;
				}
			}
		},
		min:function(){
			var temp = this.root;
			while(temp !== null){
				if(temp.left !== null){
					temp = temp.left;
				}else{
					return temp;
				}
			}
		},
		/*删除节点*/
		//找到节点
		find:function(data){
			var dat = getData(data);
			var temp = this.root;
			while(temp !== null){
				if(temp.data === dat){
					return temp;
				}else if(dat < temp.data){
					temp = temp.left;
				}else{
					temp = temp.right;
				}
			}
			return null;
			function getData(data){
				var num = 0;
				for(var i = 0,len=data.length;i<len;i++){
					num += data.charCodeAt(i)*len;
				}
				return num;
			}
		},
		//返回节点个数
		getCount:function(node=this.root){
			var temp = node;
			var count = 0;
			if(temp !== null){
				count++;
				count+= this.getCount(temp.left);
				count+= this.getCount(temp.right);
			}
			return count;
		},
		//返回边的个数
		getSides:function(node=this.root){
			var count = this.getCount(node);
			return count > 0 ? count-1 : null;
		},
		//转换成单词计数格式
		getWords:function(str){
			var arr = str.replace(/[;?!]/g," ").split(",").join(" ").split(" ");
			var data;
			for(var i = 0,len = arr.length;i<len;i++){
				arr[i] && this.insert(arr[i].toLowerCase());
			}
		}
	}
	var hst = new BST();
	var txt = "Life is full of confusing and disordering Particular time,a particular location,Do the arranged thing of ten million time in the brain,Step by step,the life is hard to avoid delicacy and stiffness No enthusiasm forever,No unexpected happening of surprising and pleasing So,only silently ask myself in mind Next happiness,when will come?";
	hst.getWords(txt);
	console.log(hst.midShow())
	console.log(hst.getCount())
	console.log(hst.find("is"))
</script>

  

转载于:https://www.cnblogs.com/chenxiZ/p/6198405.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值