public class AVL {
class TreeNode{
int val;
int height;
TreeNode left;
TreeNode right;
TreeNode (int val){
this.val=val;
this.height=0;
}
public void print(){
if(left!=null) left.print();
System.out.printf("%s-",val);
if(right!=null) right.print();
}
public void printHeight(){
if(left!=null) left.printHeight();
System.out.printf("%s-",height);
if(right!=null) right.printHeight();
}
// 層序遍歷打印峰髓,方便看結(jié)果套才;
public void printLevel(TreeNode root){
TreeNode temp=root;
Queue<TreeNode> queue = new LinkedList<TreeNode>();
queue.offer(temp);
while(queue.size()>0){
int size =queue.size();
while(size-- > 0){
temp=queue.poll();
if(temp!=null){
System.out.printf("%s-%s ",temp.val,temp.height);
queue.offer(temp.left);
queue.offer(temp.right);
}else{
System.out.printf("%s ",null);
}
}
System.out.println();
}
}
}
// 計(jì)算高度
public int height(TreeNode node){
if(node==null) return -1;
int lh=node.left==null?-1:node.left.height;
int rh=node.right==null?-1:node.right.height;
return Math.max(lh+1,rh+1);
}
// 左旋
public TreeNode leftRotate(TreeNode root){
if(root==null) return null;
TreeNode temp= root.right;
root.right=temp.left;
temp.left=root;
// 修改經(jīng)過調(diào)整后對的高度計(jì)數(shù)
root.height=height(root);
temp.height=height(temp);
return temp;
}
// 右旋
public TreeNode rightRotate(TreeNode root){
if(root==null) return null;
TreeNode temp=root.left;
root.left=temp.right;
temp.right=root;
// 修改經(jīng)過調(diào)整后的高度計(jì)數(shù)
root.height=height(root);
temp.height=height(temp);
return temp;
}
// 插入
public TreeNode insert(TreeNode root,TreeNode node){
if(root==null) return node;
// 插入
if(root.val>node.val){
root.left=insert(root.left,node);
}else{
root.right=insert(root.right,node);
}
root.height=height(root);
// 判斷是否需要調(diào)整結(jié)構(gòu)及高度(也可以把下面的調(diào)整結(jié)構(gòu)部分寫在上面插入的條件判斷里昌阿,因?yàn)樯厦嬉呀?jīng)區(qū)分了左右)
TreeNode leftChild = root.left;
TreeNode rightChild = root.right;
int lh=leftChild==null?-1:root.left.height;
int rh=rightChild==null?-1:root.right.height;
if(lh-rh>1){
int llh= height(leftChild.left);
int lrh= height(leftChild.right);
//
if(lrh>llh){
root.left=leftRotate(root.left);
}
root=rightRotate(root);
}else if(rh-lh>1){
int rlh=height(rightChild.left);
int rrh=height(rightChild.right);
if(rlh>rrh){
root.right=rightRotate(root.right);
}
root=leftRotate(root);
}
return root;
}
public TreeNode createTreeNode(int[] arr){
if(arr.length<1) return null;
TreeNode head = new TreeNode(arr[0]);
for(int i=1;i<arr.length;i++){
TreeNode node = new TreeNode(arr[i]);
head = insert(head,node);
}
return head;
}
public static void main(String[] args) {
int[] arr=new int[1000];
for(int i=0;i<1000;i++){
arr[i]=(int)Math.round(Math.random()*1000);
}
AVL avl=new AVL();
TreeNode node = avl.createTreeNode(arr);
node.print();
System.out.println();
node.printHeight();
System.out.println();
node.printLevel(node);
Queue<Integer> queue=new LinkedList<Integer>();
queue.offer(null);
System.out.println();
System.out.println(queue.size());
}
}
手寫avl
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
- 文/潘曉璐 我一進(jìn)店門了赵,熙熙樓的掌柜王于貴愁眉苦臉地迎上來潜支,“玉大人,你說我怎么就攤上這事斟览』偻龋” “怎么了?”我有些...
- 文/不壞的土叔 我叫張陵,是天一觀的道長已烤。 經(jīng)常有香客問我鸠窗,道長,這世上最難降的妖魔是什么胯究? 我笑而不...
- 正文 為了忘掉前任稍计,我火速辦了婚禮,結(jié)果婚禮上裕循,老公的妹妹穿的比我還像新娘臣嚣。我一直安慰自己,他們只是感情好剥哑,可當(dāng)我...
- 文/花漫 我一把揭開白布硅则。 她就那樣靜靜地躺著,像睡著了一般株婴。 火紅的嫁衣襯著肌膚如雪怎虫。 梳的紋絲不亂的頭發(fā)上,一...
- 文/蒼蘭香墨 我猛地睜開眼姜骡,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了缠诅?” 一聲冷哼從身側(cè)響起溶浴,我...
- 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎管引,沒想到半個(gè)月后士败,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
- 正文 獨(dú)居荒郊野嶺守林人離奇死亡褥伴,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
- 正文 我和宋清朗相戀三年谅将,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片重慢。...
- 正文 年R本政府宣布,位于F島的核電站囚戚,受9級特大地震影響酵熙,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜驰坊,卻給世界環(huán)境...
- 文/蒙蒙 一匾二、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧拳芙,春花似錦察藐、人聲如沸。這莊子的主人今日做“春日...
- 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至睹限,卻和暖如春浸须,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背邦泄。 一陣腳步聲響...
- 正文 我出身青樓,卻偏偏與公主長得像蕉拢,于是被迫代替她去往敵國和親特碳。 傳聞我的和親對象是個(gè)殘疾皇子,可洞房花燭夜當(dāng)晚...
推薦閱讀更多精彩內(nèi)容
- 從小生活在偏遠(yuǎn)的山村晕换,沒有高明老師的指點(diǎn)午乓,僅僅循著內(nèi)心,一筆一劃模仿著老師簡單有力的鉛筆字闸准,一個(gè)字一個(gè)字益愈,如虔誠的...
- 由于拔牙術(shù)后感染在醫(yī)院住了12天,回家后體力不支夷家,烘焙無法開工蒸其,練字手腕沒勁,重活做不了库快,出門多走兩步路兩腿發(fā)軟摸袁,...
- 手寫板是基于電腦的輸入設(shè)備靠汁,其作用是給不喜歡用電腦鍵盤敲字或不會(huì)使用電腦鍵盤文字輸入的人群提供幫助蜂大。隨著時(shí)代的不斷...
- 今日是5.20日,關(guān)于手寫日記要手寫的原因: 1. 手寫日記是一種療法蝶怔,與普通日記以及博客不同奶浦。 2. 與在鍵盤上...