二叉树排序
<!DOCTYPE HTML>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
</head>
<body>
<script type="text/javascript">
function Node(){
this.left=null;
this.right=null;
this.value=null;
}
Node.prototype.add = function(value){
if(value!=null && typeof(value)!='undefined'){
if(this.value==null){
this.value = value;
return;
}
var node = new Node();
node.value=value;
if(this.value>=value){
if(this.left==null){
this.left = node;
}else{
this.left.add(value);
}
}else{
if(this.right==null){
this.right = node;
}else{
this.right.add(value);
}
}
}
}
Node.prototype.print=function(data){
if(this.left!=null){
this.left.print(data);
}
data.push(this.value);
if(this.right!=null){
this.right.print(data);
}
}
function app(){
var data=[2,6,56,102,5,4,47,7000,200,45,24,85,63,954,6222,5];
var root = new Node();
for(var i=0;i<data.length;i++){
root.add(data[i]);
}
var rs = [];
root.print(rs);
console.log(rs.join(","));
}
app();
</script>
</body>
</html>
双向链表
1、将学生信息输入到一个双向链表;
2、按不同的方式对此双向链表排序:按学生成绩排序;按学生姓名排序;
3、正、逆向显示该链表内容;
思路
1,没啥说的,新建一个链表首节点,一个节点放一个数据就ok了。有prev 和next
2,排序的问题,其实可以参考数组的排序一个道理。用冒泡,两层循环搞定了。只不过把数组对调换成链表对调。
3,简单了,顺序或者逆序的输出链表就OK了。
数组1
有一组数字,从1到n,从中减少了3个数,顺序也被打乱,放在一个n-3的数组里
请找出丢失的数字,最好能有程序,最好算法比较快
假设n=10000
<script type="text/javascript">
var a = [1,2,3,4,5,6,7];
var b = [1,2,4,6];
var hash={};
var result=[];
for(var i=0;i<b.length;i++){
hash[b[i]] = true;
}
for(var j=0;j<a.length;j++){
if(!hash[a[j]]){
result.push(a[j]);
}
}
console.log(result);
</script>