二分法查找
采用二分法查找时,数据需是排好序的。
主要思想是:(设查找的数组区间为array[s, e])
(1)确定该区间的中间位置m
(2)将查找的值T与array[m]比较,若相等,查找成功返回此位置;否则确定新的查找区域,继续二分查找。
function binarySeach(arr,val,leftIndex,rightIndex){
var middleIndex=Math.floor((leftIndex+rightIndex)/2);
var middleVal=arr[middleIndex];
if(leftIndex>rightIndex){
console.log('前一位数据是:'+middleVal,'下标是:'+middleIndex);
return;
}
if(middleVal>val){
binarySeach(arr,val,leftIndex,middleIndex-1)
}else if(middleVal<val){
binarySeach(arr,val,middleIndex+1,rightIndex)
}else{
console.log('找到了,下标为:'+middleIndex);
return middleIndex;
}
}
var arr=[1,3,12,21,24,44,54,67];
binarySeach(arr,24,0,arr.length-1);
插入排序
function insertSort(arr) {
for (var i = 1; i < arr.length; i++) {
var temp = arr[i];
var j = i - 1;
while (j >= 0 && arr[j] > temp) {
arr[j + 1] = arr[j];
j--;
}
arr[j + 1] = temp;
}
return arr;
}
var arr = [1, 45, 37, 5, 48, 15, 37, 26, 29, 2, 46, 4, 17, 50, 52];
console.log(insertSort(arr));
冒泡排序
function sort(arr){
for(var i=0;i<arr.length;i++){
for(var j=0;j<arr.length-i-1;j++){
if(arr[j]>arr[j+1]){
var temp=arr[j];
arr[j]=arr[j+1];
arr[j+1]=temp;
}
}
}
return arr;
}
双向链表
function Node(element) {
this.element=element;
this.next=null;
this.previous=null;
}
function find(element) {
var currentNode=this.head;
while(currentNode.element!=element){
if(currentNode.next==null){
console.log('无法找到');
return 'error';
}
currentNode=currentNode.next;
}
return currentNode;
}
function insert(newElement,currentElement) {
var newNode=new Node(newElement);
var currentNode=this.find(currentElement);
if(currentNode=='error'){
console.log('无法插入');
return;
}
if(currentNode.next!=null){
newNode.next=currentNode.next;
currentNode.next=newNode;
newNode.previous=currentNode;
newNode.next.previous=newNode;
}
else{
currentNode.next=newNode;
newNode.previous=currentNode;
}
}
function remove(element) {
var currentNode=this.find(element);
if(currentNode=='error'){
console.log('要移除的节点不存在');
return;
}
//不是尾节点也不是头节点
if(currentNode.next!=null&¤tNode.previous!=null){
currentNode.previous.next=currentNode.next;
currentNode.next.previous=currentNode.previous;
currentNode.next=null;
currentNode.previous=null;
}
//头节点
else if(currentNode.next!=null&¤tNode.previous==null){
this.head=currentNode.next;
currentNode.next=null;
currentNode.previous=null;
}
//尾节点
else{
currentNode.next=null;
currentNode.previous=null;
}
}
function showList(){
var head=this.head;
while(head!=null){
console.log(head.element);
head=head.next;
}
}
function lastNode() {
var head=this.head;
while(head.next!=null){
head=head.next;
}
return head;
}
function append(element) {
var lastNode=this.lastNode(element);
var newNode=new Node(element);
lastNode.next=newNode;
newNode.previous=lastNode;
}
function initList() {
this.head = new Node('one');
this.find = find;
this.insert = insert;
this.remove = remove;
this.showlist = showList;
this.lastNode = lastNode;
this.append = append;
}
var list=new initList();
list.insert('two','one');
list.insert('three','two');
list.insert('four','three');
list.insert('five','four');
//console.log(list.showlist());//one two three four five
list.append('A');
list.append('B');
list.append('C');
list.insert('B2','B');
//console.log(list.showlist()); //one two three four five A B B2 C
list.remove('A');
//console.log(list.showlist());//one two three four five B B2 C
console.log(list.head);//
二叉树
class Node{
constructor(data){
this.root=this;
this.data=data;
this.left=null;
this.right=null;
}
}
//创建二叉搜索树
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);
}
}
//先序遍历
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;
}
//中序遍历
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;
}
//后序遍历
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 maxNode=node=>{
return node?(node.right?maxNode(node.right):node):null;
}
return maxNode(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);
}
//查找父结点
finParent(data){
const findNode=(node,data)=>{
if(node.left===null||node.right===null){
return false;
}
if(node.left.data==data){
return {node:node,position:'left'};
}
if(node.right.data==data){
return {node:node,position:'right'};
}
return findNode((data<node.left.data)?node.left.left:node.right.right,data);
}
return findNode(this.root,data);
}
//删除节点
remove(data){
const removeData=(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){
var position=this.finParent(node.data).position;
var node=this.finParent(node.data).node;
node[position]=null;
return node
}
}
else if(data<node.data){
removeData(node.left,data);
return node;
}
else{
removeData(node.right,data);
return node;
}
}
removeData(this.root,data);
}
}
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.find(11));