JavaScript數(shù)據(jù)結(jié)構(gòu)25—AVL平衡二叉樹算法

邊排序邊平衡

//二叉排序樹
function BtreeNode(data) {
  this.data = data;
  this.bf = 0;
}
BtreeNode.prototype.setL = function(node){
  this.lchild = node;
}
BtreeNode.prototype.setR = function(node){
  this.rchild = node;
}
BtreeNode.prototype.search = function(key){
  if(key == this.data){
    return {find:true,node:this};
  }else if(key < this.data){
    if(!this.lchild){
      return {find:false,node:this};
    }
    return this.lchild.search(key);
  }else{
    if(!this.rchild){
      return {find:false,node:this};
    }
    return this.rchild.search(key);
  }
}
BtreeNode.prototype.insert = function(key){
  var searchResult = this.search(key);
  if(!searchResult.find){
    var s = new BtreeNode(key);
    if(key<searchResult.node.data){
      searchResult.node.lchild = new BtreeNode(key);
    }else{
      searchResult.node.rchild = new BtreeNode(key);
    }
    return true;
  }
  return false;
}
BtreeNode.prototype.delete = function(){
  if(!this.rchild){
    var p = this;
    p = this.lchild;
  }else if(!this.lchild){
    var p = this
    p = this.rchild;
  }else{
    var q = this;
    var s = this.rchild;
    while(s.rchild){
      q = s;
      s = s.rchild;
    }
    this.data = s.data;
    if(q!=this){
      q.rchild = s.lchild;
    }else{
      q.lchild = s.lchild;
    }
  }
}
BtreeNode.prototype.deleteKey = function(key){
  if(this.data == key){
    this.delete();
  }else if(this.data>key){
    this.lchild.deleteKey(key);
  }else{
    this.rchild.deleteKey(key);
  }
}
//計算深度
function GetBtreeLength(t){
    var l = 0,r = 0; 
    if(!t){
      return 0;
    }
    else if(!t.lchild&&!t.rchild){
      return 1;
    }
    else{
      if(t.lchild){
        l = GetBtreeLength(t.lchild);
      }
      if(t.rchild){
        r = GetBtreeLength(t.rchild);
      }
      return l>r?l+1:r+1;
    }
}
BtreeNode.prototype.setBF = function(){
  if(!this){
    return;
  }
  var l = GetBtreeLength(this.lchild);
  var r = GetBtreeLength(this.rchild);
  this.bf = r-l;
  if(this.lchild){
    this.lchild.setBF();
  }
  if(this.rchild){
    this.rchild.setBF();
  }
}
//右旋
BtreeNode.prototype.rightRotate = function(){
  var l = this.lchild;
  this.lchild = l.rchild;
  var t = deepCopy(this);
  l.rchild = t;
  this.data = l.data;
  this.bf = l.bf;
  this.lchild = l.lchild;
  this.rchild = l.rchild;
}
//左旋
var deepCopy= function(source) { 
    var result={};
    for (var key in source) {
        result[key] = typeof source[key]==='object'?
        deepCopy(source[key]): source[key];
     } 
   return result; 
}
BtreeNode.prototype.leftRotate = function(){
  var l = this.rchild;
  this.rchild = l.lchild;
  var t = deepCopy(this);
  l.lchild = t;
  this.data = l.data;
  this.bf = l.bf;
  this.lchild = l.lchild;
  this.rchild = l.rchild;
}
//左旋轉(zhuǎn)平衡
BtreeNode.prototype.leftBalance = function(){
  var l = this.lchild,lr;
  switch (l.bf){
    case 1:
      this.bf = l.bf = 0;
      this.rightRotate();
      break;
    case -1:
      lr = l.rchild;
      switch (lr.bf){
        case 1:
          this.bf = -1;
          l.bf = 0;
          break;
        case 0:
          this.bf = l.bf = 0;
          break;
        case -1:
          this.bf = 0;
          l.bf = 1;
          break;
      }
      lr.bf = 0;
      this.lchild.leftRotate();
      this.rightRotate();
  }
}
//右旋轉(zhuǎn)平衡
BtreeNode.prototype.rightBalance = function(){
  var l = this.rchild,lr;
  switch (l.bf){
    case -1:
      this.bf = l.bf = 0;
      this.leftRotate();
      break;
    case 1:
      lr = l.lchild;
      switch (lr.bf){
        case 1:
          this.bf = 1;
          l.bf = 0;
          break;
        case 0:
          this.bf = l.bf = 0;
          break;
        case -1:
          this.bf = 0;
          l.bf = -1;
          break;
      }
      lr.bf = 0;
      this.rchild.rightRotate();
      this.leftRotate();
  }
}
//插入并平衡二叉樹
var taller = false;
BtreeNode.prototype.insertAVL = function(e){
  if(e.data == this.data){
    console.info('數(shù)值已經(jīng)存在,無法插入')
    taller = false;
    return false;
  }
  if(e.data < this.data){
    if(!this.lchild){
      this.lchild = e;
      this.lchild.bf = 0;
      taller = true;
      console.info('在'+this.data+'的左子樹添加一個節(jié)點'+e.data);
    }
    else{
      if(!this.lchild.insertAVL(e)){
        return false;
      }
    }
    if(taller){
      console.info('節(jié)點是'+this.data+',平衡因子是'+this.bf);
      switch (this.bf){
        case 1:
          this.leftBalance();
          console.info('對節(jié)點左平衡處理');
          taller = false;
          break;
        case 0:
          this.bf = 1;
          console.info('變更節(jié)點'+this.data+'的平衡因子為1');
          taller = true;
          break;
        case -1:
          this.bf = 0;
          console.info('變更節(jié)點'+this.data+'的平衡因子為0');
          taller = false;
          break;
      }
    }
  }
  else{
    if(!this.rchild){
      this.rchild = e;
      this.rchild.bf = 0;
      taller = true;
      console.info('在'+this.data+'的右子樹添加一個節(jié)點'+e.data);
    }else{
      if(!this.rchild.insertAVL(e)){
        return false;
      }
    }
    if(taller){
      console.info('節(jié)點是'+this.data+',平衡因子是'+this.bf);
      switch(this.bf){
        case 1:
          this.bf = 0;
          console.info('變更節(jié)點'+this.data+'的平衡因子為0');
          taller = false;
          break;
        case 0:
          this.bf = -1;
          console.info('變更節(jié)點'+this.data+'的平衡因子為-1');
          taller = true;
          break;
        case -1:
          this.rightBalance();
          console.info('對節(jié)點右平衡處理');
          taller = false;
          break;
      }
    }
  }
  return true;
}
var b = new BtreeNode(0);
b.insertAVL(new BtreeNode(1)).btree;
b.insertAVL(new BtreeNode(2)).btree;
b.insertAVL(new BtreeNode(3)).btree;
b.insertAVL(new BtreeNode(4)).btree;
b.insertAVL(new BtreeNode(5)).btree;
b.insertAVL(new BtreeNode(6)).btree;
b.insertAVL(new BtreeNode(7)).btree;
b.insertAVL(new BtreeNode(8)).btree;
b.insertAVL(new BtreeNode(9)).btree;
b.insertAVL(new BtreeNode(10)).btree;
b.insertAVL(new BtreeNode(11)).btree;
console.info(b);

output

在0的右子樹添加一個節(jié)點1
節(jié)點是0,平衡因子是0
變更節(jié)點0的平衡因子為-1
在1的右子樹添加一個節(jié)點2
節(jié)點是1,平衡因子是0
變更節(jié)點1的平衡因子為-1
節(jié)點是0,平衡因子是-1
對節(jié)點右平衡處理
在2的右子樹添加一個節(jié)點3
節(jié)點是2,平衡因子是0
變更節(jié)點2的平衡因子為-1
節(jié)點是1,平衡因子是0
變更節(jié)點1的平衡因子為-1
在3的右子樹添加一個節(jié)點4
節(jié)點是3,平衡因子是0
變更節(jié)點3的平衡因子為-1
節(jié)點是2,平衡因子是-1
對節(jié)點右平衡處理
在4的右子樹添加一個節(jié)點5
節(jié)點是4,平衡因子是0
變更節(jié)點4的平衡因子為-1
節(jié)點是3,平衡因子是0
變更節(jié)點3的平衡因子為-1
節(jié)點是1,平衡因子是-1
對節(jié)點右平衡處理
在5的右子樹添加一個節(jié)點6
節(jié)點是5,平衡因子是0
變更節(jié)點5的平衡因子為-1
節(jié)點是4,平衡因子是-1
對節(jié)點右平衡處理
在6的右子樹添加一個節(jié)點7
節(jié)點是6,平衡因子是0
變更節(jié)點6的平衡因子為-1
節(jié)點是5,平衡因子是0
變更節(jié)點5的平衡因子為-1
節(jié)點是3,平衡因子是0
變更節(jié)點3的平衡因子為-1
在7的右子樹添加一個節(jié)點8
節(jié)點是7,平衡因子是0
變更節(jié)點7的平衡因子為-1
節(jié)點是6,平衡因子是-1
對節(jié)點右平衡處理
在8的右子樹添加一個節(jié)點9
節(jié)點是8,平衡因子是0
變更節(jié)點8的平衡因子為-1
節(jié)點是7,平衡因子是0
變更節(jié)點7的平衡因子為-1
節(jié)點是5,平衡因子是-1
對節(jié)點右平衡處理
在9的右子樹添加一個節(jié)點10
節(jié)點是9,平衡因子是0
變更節(jié)點9的平衡因子為-1
節(jié)點是8,平衡因子是-1
對節(jié)點右平衡處理
在10的右子樹添加一個節(jié)點11
節(jié)點是10,平衡因子是0
變更節(jié)點10的平衡因子為-1
節(jié)點是9,平衡因子是0
變更節(jié)點9的平衡因子為-1
節(jié)點是7,平衡因子是0
變更節(jié)點7的平衡因子為-1
節(jié)點是3,平衡因子是-1
對節(jié)點右平衡處理
BtreeNode {
data: 7,
bf: 0,
rchild:
BtreeNode {
data: 9,
bf: -1,
rchild: BtreeNode { data: 10, bf: -1, rchild: [Object] },
lchild:
{ data: 8,
bf: 0,
rchild: undefined,
setL: [Function],
setR: [Function],
search: [Function],
insert: [Function],
delete: [Function],
deleteKey: [Function],
setBF: [Function],
rightRotate: [Function],
leftRotate: [Function],
leftBalance: [Function],
rightBalance: [Function],
insertAVL: [Function] } },
lchild:
{ data: 3,
bf: 0,
rchild:
{ data: 5,
bf: 0,
rchild: [Object],
lchild: [Object],
setL: [Function],
setR: [Function],
search: [Function],
insert: [Function],
delete: [Function],
deleteKey: [Function],
setBF: [Function],
rightRotate: [Function],
leftRotate: [Function],
leftBalance: [Function],
rightBalance: [Function],
insertAVL: [Function] },
lchild:
{ data: 1,
bf: 0,
rchild: [Object],
lchild: [Object],
setL: [Function],
setR: [Function],
search: [Function],
insert: [Function],
delete: [Function],
deleteKey: [Function],
setBF: [Function],
rightRotate: [Function],
leftRotate: [Function],
leftBalance: [Function],
rightBalance: [Function],
insertAVL: [Function] },
setL: [Function],
setR: [Function],
search: [Function],
insert: [Function],
delete: [Function],
deleteKey: [Function],
setBF: [Function],
rightRotate: [Function],
leftRotate: [Function],
leftBalance: [Function],
rightBalance: [Function],
insertAVL: [Function] } }
[Finished in 0.1s]

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市,隨后出現(xiàn)的幾起案子勾栗,更是在濱河造成了極大的恐慌,老刑警劉巖颤专,帶你破解...
    沈念sama閱讀 212,599評論 6 492
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異,居然都是意外死亡触幼,警方通過查閱死者的電腦和手機拆祈,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 90,629評論 3 385
  • 文/潘曉璐 我一進店門恨闪,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人缘屹,你說我怎么就攤上這事凛剥。” “怎么了轻姿?”我有些...
    開封第一講書人閱讀 158,084評論 0 348
  • 文/不壞的土叔 我叫張陵,是天一觀的道長逻炊。 經(jīng)常有香客問我互亮,道長,這世上最難降的妖魔是什么余素? 我笑而不...
    開封第一講書人閱讀 56,708評論 1 284
  • 正文 為了忘掉前任豹休,我火速辦了婚禮,結(jié)果婚禮上桨吊,老公的妹妹穿的比我還像新娘威根。我一直安慰自己,他們只是感情好视乐,可當我...
    茶點故事閱讀 65,813評論 6 386
  • 文/花漫 我一把揭開白布洛搀。 她就那樣靜靜地躺著,像睡著了一般佑淀。 火紅的嫁衣襯著肌膚如雪留美。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 50,021評論 1 291
  • 那天伸刃,我揣著相機與錄音谎砾,去河邊找鬼。 笑死捧颅,一個胖子當著我的面吹牛景图,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播碉哑,決...
    沈念sama閱讀 39,120評論 3 410
  • 文/蒼蘭香墨 我猛地睜開眼挚币,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了谭梗?” 一聲冷哼從身側(cè)響起忘晤,我...
    開封第一講書人閱讀 37,866評論 0 268
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎激捏,沒想到半個月后设塔,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 44,308評論 1 303
  • 正文 獨居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 36,633評論 2 327
  • 正文 我和宋清朗相戀三年闰蛔,在試婚紗的時候發(fā)現(xiàn)自己被綠了痕钢。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點故事閱讀 38,768評論 1 341
  • 序言:一個原本活蹦亂跳的男人離奇死亡序六,死狀恐怖任连,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情例诀,我是刑警寧澤随抠,帶...
    沈念sama閱讀 34,461評論 4 333
  • 正文 年R本政府宣布,位于F島的核電站繁涂,受9級特大地震影響拱她,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜扔罪,卻給世界環(huán)境...
    茶點故事閱讀 40,094評論 3 317
  • 文/蒙蒙 一秉沼、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧矿酵,春花似錦唬复、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 30,850評論 0 21
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至倔矾,卻和暖如春妄均,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背哪自。 一陣腳步聲響...
    開封第一講書人閱讀 32,082評論 1 267
  • 我被黑心中介騙來泰國打工丰包, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人壤巷。 一個月前我還...
    沈念sama閱讀 46,571評論 2 362
  • 正文 我出身青樓邑彪,卻偏偏與公主長得像,于是被迫代替她去往敵國和親胧华。 傳聞我的和親對象是個殘疾皇子寄症,可洞房花燭夜當晚...
    茶點故事閱讀 43,666評論 2 350

推薦閱讀更多精彩內(nèi)容