SlideShare a Scribd company logo
#include<stdio.h>
#include<stdlib.h>
int breakloop = 0;
struct Data{
int number;
int height;
Data *left, *right, *parent;
}*root;
Data *createNode(int number, Data *parent){
Data *curr = (Data*)malloc(sizeof(Data));
curr->number = number;
curr->left = curr->right = NULL;
curr->parent = parent;
curr->height = 1;
return curr;
}
void push(Data **curr,int number, Data *parent){
if(!(*curr)){
(*curr) = createNode(number, parent);
if((*curr)->parent)
printf("parent : %dn", (*curr)->parent->number);
}
else if (number == (*curr)->number){
printf("Data can't samen");
return;
}
else if(number < (*curr)->number)
push(&(*curr)->left,number,(*curr));
else if(number > (*curr)->number)
push(&(*curr)->right,number,(*curr));
}
int max(int a, int b){
if(a<=b)
return b;
if(a>b)
return a;
}
void rotateLL(Data *curr){
Data *mid = curr->left;
Data *last = mid->left;
printf("curr : %dn",curr->number);
printf("mid : %dn",mid->number);
printf("last : %dn",last->number);
Data *temp = NULL;
if(mid->right){
printf("ada tempn");
temp = mid->right;
}
if(curr == root){
printf("masuk curr == rootn");
root = mid;
mid->right = curr;
curr->parent = mid;
curr->left = NULL;
}else{
printf("masuk curr != rootn");
mid->parent = curr->parent;
printf("mid->parent : %dn",curr->parent->height);
if(curr->parent->right->height == curr->height){
curr->parent->right = mid;
printf("parent->rightn");
}
else if(curr->parent->left->height == curr->height){
curr->parent->left = mid;
printf("parent->leftn");
}
curr->parent = mid;
mid->right = curr;
curr->left = NULL;
}
if(temp){
curr->left = temp;
temp->parent = curr;
}
}
void rotateRR(Data *curr){
Data *mid = curr->right;
Data *last = mid->right;
printf("curr : %dn",curr->number);
printf("mid : %dn",mid->number);
printf("last : %dn",last->number);
Data *temp = NULL;
if(mid->left){
printf("ada tempn");
temp = mid->left;
}
if(curr == root){
printf("masuk curr == rootn");
root = mid;
mid->left = curr;
curr->parent = mid;
curr->right = NULL;
}else{
printf("masuk curr != rootn");
mid->parent = curr->parent;
if(curr->parent->left->height == curr->height)
curr->parent->left = mid;
else if (curr->parent->right->height == curr->height)
curr->parent->right = mid;
curr->parent = mid;
mid->left = curr;
curr->right = NULL;
}
if(temp){
curr->right = temp;
temp->parent = curr;
}
}
void rotateLR(Data *curr){
Data *mid = curr->left;
Data *last = mid->right;
printf("curr : %dn",curr->number);
printf("mid : %dn",mid->number);
printf("last : %dn",last->number);
Data *temp = NULL;
if(last->left){
printf("ada tempn");
temp = last->left;
}
printf("masuk curr == rootn");
last->left = mid;
last->parent = curr;
curr->left = last;
mid->parent = last;
mid->right = NULL;
if(temp){
mid->right = temp;
temp->parent = mid;
}
}
void rotateRL(Data *curr){
Data *mid = curr->right;
Data *last = mid->left;
printf("curr : %dn",curr->number);
printf("mid : %dn",mid->number);
printf("last : %dn",last->number);
Data *temp = NULL;
if(last->right){
printf("ada tempn");
temp = last->right;
}
last->right = mid;
last->parent = curr;
curr->right = last;
mid->parent = last;
mid->left = NULL;
if(temp){
mid->left = temp;
temp->parent = mid;
}
}
int checkBalance(Data *curr){
int balance = 0;
if(!curr)
return 0;
checkBalance(curr->left);
checkBalance(curr->right);
if(breakloop != 0){
printf("Berhentin");
return 1;
}
if(!curr->left && !curr->right)
balance = 0;
else if(!curr->left){
balance = curr->right->height;
}
else if(!curr->right){
balance = curr->left->height;
}
else{
balance= curr->left->height - curr->right->height;
}
printf("%d->%dn",curr->number, balance);
if(balance <-1 || balance > 1){
if(curr->left && curr->left->left){
printf("Rotate LLn");
rotateLL(curr);
}else if(curr->right && curr->right->right){
printf("Rotate RRn");
rotateRR(curr);
}else if(curr->left && curr->left->right){
printf("Rotate LRn");
rotateLR(curr);
rotateLL(curr);
}else if(curr->right && curr->right->left){
printf("Rotate RLn");
rotateRL(curr);
rotateRR(curr);
}
breakloop = 1;
return 1;
}
return 0;
}
void checkHeight(Data *curr){
if(!curr)
return;
checkHeight(curr->left);
checkHeight(curr->right);
{
if(!curr->left && !curr->right)
curr->height = 1;
else if(!curr->left){
curr->height = 1 + curr->right->height;
}
else if(!curr->right){
curr->height = 1 + curr->left->height;
}
else{
curr->height = 1+ max(curr->left->height,curr->right->height);
}
printf("%d->%dn",curr->number,curr->height);
}
}
void postOrder(Data **curr){
if(!(*curr))
return;
postOrder(&(*curr)->left);
postOrder(&(*curr)->right);
printf("%d ",(*curr)->number);
}
void inOrder(Data **curr){
if(!(*curr))
return;
inOrder(&(*curr)->left);
printf("%d ",(*curr)->number);
inOrder(&(*curr)->right);
}
void preOrder(Data **curr){
if(!(*curr))
return;
printf("%d ",(*curr)->number);
preOrder(&(*curr)->left);
preOrder(&(*curr)->right);
}
int searchTF(int number, Data *curr){
if(!curr){
return 0;
}
else if(number == curr->number)
return 1;
else if(number< curr->number){
searchTF(number,curr->left);
}
else if(number > curr->number){
searchTF(number,curr->right);
}
}
Data *search(int number, Data *curr){
if(number == curr->number)
return curr;
else if(number< curr->number){
search(number,curr->left);
}
else if(number > curr->number){
search(number,curr->right);
}
}
void deleteTree(int delet, Data *curr){
printf("Delete %dn",curr->number);
if(!curr->left && !curr->right){
printf("!curr->left && !curr->rightn");
if(!curr->parent){
printf("Masuk !curr->parentn");
root = NULL;
free(curr);
}
else if(curr->parent->left && curr->parent->left->number == curr->number){
printf("curr->parent->left->number == curr->numbern");
curr->parent->left = NULL;
free(curr);
}
else if(curr->parent->right && curr->parent->right->number == curr-
>number){
printf("curr->parent->right->number == curr->numbern");
curr->parent->right = NULL;
free(curr);
}
}else if(curr->left && curr->right){
printf("curr->left && curr->rightn");
Data *swap = curr;
swap = swap->left;
printf("swap : %dn",swap->number);
while(swap->right != NULL){
swap = swap->right;
printf("swap : %dn",swap->number);
}
int temp = swap->number;
swap->number = curr->number;
curr->number = temp;
deleteTree(delet,swap);
}else if(curr->left){
printf("curr->leftn");
Data *swap = curr;
while(swap->left != NULL){
swap = swap->left;
int temp = swap->number;
swap->number = curr->number;
curr->number = temp;
}
deleteTree(delet,swap);
}else if(curr->right){
printf("curr->rightn");
Data *swap = curr;
while(swap->right != NULL){
swap = swap->right;
int temp = swap->number;
swap->number = curr->number;
curr->number = temp;
}
deleteTree(delet,swap);
}
printf("selesain");
}
int main(){
int choose = 0;
do{
choose = 0;
int chooseView = 0;
printf("1. Insertn");
printf("2. Deleten");
printf("3. Viewn");
printf("4. Exitn");
scanf("%d",&choose);
fflush(stdin);
switch(choose){
case 1:
int check;
check = 0;
int insert;
printf("Insert Number : ");
scanf("%d",&insert);
fflush(stdin);
push(&root,insert, NULL);
do{
breakloop = 0;
printf("Check Height: n");
checkHeight(root);
printf("Check Balance: n");
check = checkBalance(root);
printf("check : %dn",check);
}while(check != 0);
break;
case 2:
int delet;
printf("Delete Number : ");
scanf("%d",&delet);
fflush(stdin);
if(searchTF(delet,root) ==0)
printf("No Datan");
else if (searchTF(delet,root) == 1){
printf("Masuk deletn");
deleteTree(delet,search(delet,root));
}
do{
breakloop = 0;
printf("Check Height: n");
checkHeight(root);
printf("Check Balance: n");
check = checkBalance(root);
printf("check : %dn",check);
}while(check != 0);
break;
case 3:
do{
chooseView = 0;
printf("1. PreOrdern");
printf("2. InOrdern");
printf("3. PostOrdern");
printf("4. Exitn");
scanf("%d",&chooseView);
fflush(stdin);
switch(chooseView){
case 1:
preOrder(&root);
puts("");
break;
case 2:
inOrder(&root);
puts("");
break;
case 3:
postOrder(&root);
puts("");
break;
}
}while(chooseView != 4);
break;
}
}while(choose != 4);
return 0;
}

More Related Content

Similar to Avl tree

Avl tree
Avl treeAvl tree
C++ adt c++ implementations
C++   adt c++ implementationsC++   adt c++ implementations
C++ adt c++ implementations
Rex Mwamba
 
Cpds lab
Cpds labCpds lab
VTU Data Structures Lab Manual
VTU Data Structures Lab ManualVTU Data Structures Lab Manual
VTU Data Structures Lab Manual
Nithin Kumar,VVCE, Mysuru
 
#include stdio.h#include stdlib.h#include string.h#inclu.pdf
#include stdio.h#include stdlib.h#include string.h#inclu.pdf#include stdio.h#include stdlib.h#include string.h#inclu.pdf
#include stdio.h#include stdlib.h#include string.h#inclu.pdf
apleather
 
Questions has 4 parts.1st part Program to implement sorting algor.pdf
Questions has 4 parts.1st part Program to implement sorting algor.pdfQuestions has 4 parts.1st part Program to implement sorting algor.pdf
Questions has 4 parts.1st part Program to implement sorting algor.pdf
apexelectronices01
 
Data Structures Using C Practical File
Data Structures Using C Practical File Data Structures Using C Practical File
Data Structures Using C Practical File
Rahul Chugh
 
i am using C++ codingsource coding Program that sorts an arra.pdf
i am using C++ codingsource coding Program that sorts an arra.pdfi am using C++ codingsource coding Program that sorts an arra.pdf
i am using C++ codingsource coding Program that sorts an arra.pdf
ANJALIENTERPRISES1
 
I am not able to complete the last function. Could anyone please hel.pdf
I am not able to complete the last function. Could anyone please hel.pdfI am not able to complete the last function. Could anyone please hel.pdf
I am not able to complete the last function. Could anyone please hel.pdf
fantoosh1
 
C lab manaual
C lab manaualC lab manaual
C lab manaual
manoj11manu
 
pROgRAN C++ ApLiKaSI Binery tree
pROgRAN C++ ApLiKaSI Binery treepROgRAN C++ ApLiKaSI Binery tree
pROgRAN C++ ApLiKaSI Binery tree
Naufal R Pradana
 
4 operators, expressions &amp; statements
4  operators, expressions &amp; statements4  operators, expressions &amp; statements
4 operators, expressions &amp; statements
MomenMostafa
 
Ugly code
Ugly codeUgly code
Ugly code
Odd-e
 
C basics
C basicsC basics
C basics
MSc CST
 
6 c control statements branching &amp; jumping
6 c control statements branching &amp; jumping6 c control statements branching &amp; jumping
6 c control statements branching &amp; jumping
MomenMostafa
 
Basic c programs updated on 31.8.2020
Basic c programs updated on 31.8.2020Basic c programs updated on 31.8.2020
Basic c programs updated on 31.8.2020
vrgokila
 
C programms
C programmsC programms
C programms
Mukund Gandrakota
 
#include -stdio-h- #include -stdlib-h- #include -stdbool-h- #include - (1).docx
#include -stdio-h- #include -stdlib-h- #include -stdbool-h- #include - (1).docx#include -stdio-h- #include -stdlib-h- #include -stdbool-h- #include - (1).docx
#include -stdio-h- #include -stdlib-h- #include -stdbool-h- #include - (1).docx
PiersRCoThomsonw
 
Daapracticals 111105084852-phpapp02
Daapracticals 111105084852-phpapp02Daapracticals 111105084852-phpapp02
Daapracticals 111105084852-phpapp02
Er Ritu Aggarwal
 
Implement the Queue ADT using array – based approach. Using C++ prog.pdf
Implement the Queue ADT using array – based approach. Using C++ prog.pdfImplement the Queue ADT using array – based approach. Using C++ prog.pdf
Implement the Queue ADT using array – based approach. Using C++ prog.pdf
sktambifortune
 

Similar to Avl tree (20)

Avl tree
Avl treeAvl tree
Avl tree
 
C++ adt c++ implementations
C++   adt c++ implementationsC++   adt c++ implementations
C++ adt c++ implementations
 
Cpds lab
Cpds labCpds lab
Cpds lab
 
VTU Data Structures Lab Manual
VTU Data Structures Lab ManualVTU Data Structures Lab Manual
VTU Data Structures Lab Manual
 
#include stdio.h#include stdlib.h#include string.h#inclu.pdf
#include stdio.h#include stdlib.h#include string.h#inclu.pdf#include stdio.h#include stdlib.h#include string.h#inclu.pdf
#include stdio.h#include stdlib.h#include string.h#inclu.pdf
 
Questions has 4 parts.1st part Program to implement sorting algor.pdf
Questions has 4 parts.1st part Program to implement sorting algor.pdfQuestions has 4 parts.1st part Program to implement sorting algor.pdf
Questions has 4 parts.1st part Program to implement sorting algor.pdf
 
Data Structures Using C Practical File
Data Structures Using C Practical File Data Structures Using C Practical File
Data Structures Using C Practical File
 
i am using C++ codingsource coding Program that sorts an arra.pdf
i am using C++ codingsource coding Program that sorts an arra.pdfi am using C++ codingsource coding Program that sorts an arra.pdf
i am using C++ codingsource coding Program that sorts an arra.pdf
 
I am not able to complete the last function. Could anyone please hel.pdf
I am not able to complete the last function. Could anyone please hel.pdfI am not able to complete the last function. Could anyone please hel.pdf
I am not able to complete the last function. Could anyone please hel.pdf
 
C lab manaual
C lab manaualC lab manaual
C lab manaual
 
pROgRAN C++ ApLiKaSI Binery tree
pROgRAN C++ ApLiKaSI Binery treepROgRAN C++ ApLiKaSI Binery tree
pROgRAN C++ ApLiKaSI Binery tree
 
4 operators, expressions &amp; statements
4  operators, expressions &amp; statements4  operators, expressions &amp; statements
4 operators, expressions &amp; statements
 
Ugly code
Ugly codeUgly code
Ugly code
 
C basics
C basicsC basics
C basics
 
6 c control statements branching &amp; jumping
6 c control statements branching &amp; jumping6 c control statements branching &amp; jumping
6 c control statements branching &amp; jumping
 
Basic c programs updated on 31.8.2020
Basic c programs updated on 31.8.2020Basic c programs updated on 31.8.2020
Basic c programs updated on 31.8.2020
 
C programms
C programmsC programms
C programms
 
#include -stdio-h- #include -stdlib-h- #include -stdbool-h- #include - (1).docx
#include -stdio-h- #include -stdlib-h- #include -stdbool-h- #include - (1).docx#include -stdio-h- #include -stdlib-h- #include -stdbool-h- #include - (1).docx
#include -stdio-h- #include -stdlib-h- #include -stdbool-h- #include - (1).docx
 
Daapracticals 111105084852-phpapp02
Daapracticals 111105084852-phpapp02Daapracticals 111105084852-phpapp02
Daapracticals 111105084852-phpapp02
 
Implement the Queue ADT using array – based approach. Using C++ prog.pdf
Implement the Queue ADT using array – based approach. Using C++ prog.pdfImplement the Queue ADT using array – based approach. Using C++ prog.pdf
Implement the Queue ADT using array – based approach. Using C++ prog.pdf
 

Recently uploaded

Photoshop Tutorial for Beginners (2024 Edition)
Photoshop Tutorial for Beginners (2024 Edition)Photoshop Tutorial for Beginners (2024 Edition)
Photoshop Tutorial for Beginners (2024 Edition)
alowpalsadig
 
Modelling Up - DDDEurope 2024 - Amsterdam
Modelling Up - DDDEurope 2024 - AmsterdamModelling Up - DDDEurope 2024 - Amsterdam
Modelling Up - DDDEurope 2024 - Amsterdam
Alberto Brandolini
 
Why Apache Kafka Clusters Are Like Galaxies (And Other Cosmic Kafka Quandarie...
Why Apache Kafka Clusters Are Like Galaxies (And Other Cosmic Kafka Quandarie...Why Apache Kafka Clusters Are Like Galaxies (And Other Cosmic Kafka Quandarie...
Why Apache Kafka Clusters Are Like Galaxies (And Other Cosmic Kafka Quandarie...
Paul Brebner
 
Operational ease MuleSoft and Salesforce Service Cloud Solution v1.0.pptx
Operational ease MuleSoft and Salesforce Service Cloud Solution v1.0.pptxOperational ease MuleSoft and Salesforce Service Cloud Solution v1.0.pptx
Operational ease MuleSoft and Salesforce Service Cloud Solution v1.0.pptx
sandeepmenon62
 
Computer Science & Engineering VI Sem- New Syllabus.pdf
Computer Science & Engineering VI Sem- New Syllabus.pdfComputer Science & Engineering VI Sem- New Syllabus.pdf
Computer Science & Engineering VI Sem- New Syllabus.pdf
chandangoswami40933
 
Baha Majid WCA4Z IBM Z Customer Council Boston June 2024.pdf
Baha Majid WCA4Z IBM Z Customer Council Boston June 2024.pdfBaha Majid WCA4Z IBM Z Customer Council Boston June 2024.pdf
Baha Majid WCA4Z IBM Z Customer Council Boston June 2024.pdf
Baha Majid
 
A Comprehensive Guide on Implementing Real-World Mobile Testing Strategies fo...
A Comprehensive Guide on Implementing Real-World Mobile Testing Strategies fo...A Comprehensive Guide on Implementing Real-World Mobile Testing Strategies fo...
A Comprehensive Guide on Implementing Real-World Mobile Testing Strategies fo...
kalichargn70th171
 
Superpower Your Apache Kafka Applications Development with Complementary Open...
Superpower Your Apache Kafka Applications Development with Complementary Open...Superpower Your Apache Kafka Applications Development with Complementary Open...
Superpower Your Apache Kafka Applications Development with Complementary Open...
Paul Brebner
 
🏎️Tech Transformation: DevOps Insights from the Experts 👩‍💻
🏎️Tech Transformation: DevOps Insights from the Experts 👩‍💻🏎️Tech Transformation: DevOps Insights from the Experts 👩‍💻
🏎️Tech Transformation: DevOps Insights from the Experts 👩‍💻
campbellclarkson
 
Software Test Automation - A Comprehensive Guide on Automated Testing.pdf
Software Test Automation - A Comprehensive Guide on Automated Testing.pdfSoftware Test Automation - A Comprehensive Guide on Automated Testing.pdf
Software Test Automation - A Comprehensive Guide on Automated Testing.pdf
kalichargn70th171
 
一比一原版(USF毕业证)旧金山大学毕业证如何办理
一比一原版(USF毕业证)旧金山大学毕业证如何办理一比一原版(USF毕业证)旧金山大学毕业证如何办理
一比一原版(USF毕业证)旧金山大学毕业证如何办理
dakas1
 
一比一原版(sdsu毕业证书)圣地亚哥州立大学毕业证如何办理
一比一原版(sdsu毕业证书)圣地亚哥州立大学毕业证如何办理一比一原版(sdsu毕业证书)圣地亚哥州立大学毕业证如何办理
一比一原版(sdsu毕业证书)圣地亚哥州立大学毕业证如何办理
kgyxske
 
Safelyio Toolbox Talk Softwate & App (How To Digitize Safety Meetings)
Safelyio Toolbox Talk Softwate & App (How To Digitize Safety Meetings)Safelyio Toolbox Talk Softwate & App (How To Digitize Safety Meetings)
Safelyio Toolbox Talk Softwate & App (How To Digitize Safety Meetings)
safelyiotech
 
Streamlining End-to-End Testing Automation
Streamlining End-to-End Testing AutomationStreamlining End-to-End Testing Automation
Streamlining End-to-End Testing Automation
Anand Bagmar
 
Ensuring Efficiency and Speed with Practical Solutions for Clinical Operations
Ensuring Efficiency and Speed with Practical Solutions for Clinical OperationsEnsuring Efficiency and Speed with Practical Solutions for Clinical Operations
Ensuring Efficiency and Speed with Practical Solutions for Clinical Operations
OnePlan Solutions
 
Alluxio Webinar | 10x Faster Trino Queries on Your Data Platform
Alluxio Webinar | 10x Faster Trino Queries on Your Data PlatformAlluxio Webinar | 10x Faster Trino Queries on Your Data Platform
Alluxio Webinar | 10x Faster Trino Queries on Your Data Platform
Alluxio, Inc.
 
Penify - Let AI do the Documentation, you write the Code.
Penify - Let AI do the Documentation, you write the Code.Penify - Let AI do the Documentation, you write the Code.
Penify - Let AI do the Documentation, you write the Code.
KrishnaveniMohan1
 
The Rising Future of CPaaS in the Middle East 2024
The Rising Future of CPaaS in the Middle East 2024The Rising Future of CPaaS in the Middle East 2024
The Rising Future of CPaaS in the Middle East 2024
Yara Milbes
 
Transforming Product Development using OnePlan To Boost Efficiency and Innova...
Transforming Product Development using OnePlan To Boost Efficiency and Innova...Transforming Product Development using OnePlan To Boost Efficiency and Innova...
Transforming Product Development using OnePlan To Boost Efficiency and Innova...
OnePlan Solutions
 

Recently uploaded (20)

Photoshop Tutorial for Beginners (2024 Edition)
Photoshop Tutorial for Beginners (2024 Edition)Photoshop Tutorial for Beginners (2024 Edition)
Photoshop Tutorial for Beginners (2024 Edition)
 
Modelling Up - DDDEurope 2024 - Amsterdam
Modelling Up - DDDEurope 2024 - AmsterdamModelling Up - DDDEurope 2024 - Amsterdam
Modelling Up - DDDEurope 2024 - Amsterdam
 
Why Apache Kafka Clusters Are Like Galaxies (And Other Cosmic Kafka Quandarie...
Why Apache Kafka Clusters Are Like Galaxies (And Other Cosmic Kafka Quandarie...Why Apache Kafka Clusters Are Like Galaxies (And Other Cosmic Kafka Quandarie...
Why Apache Kafka Clusters Are Like Galaxies (And Other Cosmic Kafka Quandarie...
 
Operational ease MuleSoft and Salesforce Service Cloud Solution v1.0.pptx
Operational ease MuleSoft and Salesforce Service Cloud Solution v1.0.pptxOperational ease MuleSoft and Salesforce Service Cloud Solution v1.0.pptx
Operational ease MuleSoft and Salesforce Service Cloud Solution v1.0.pptx
 
Computer Science & Engineering VI Sem- New Syllabus.pdf
Computer Science & Engineering VI Sem- New Syllabus.pdfComputer Science & Engineering VI Sem- New Syllabus.pdf
Computer Science & Engineering VI Sem- New Syllabus.pdf
 
Baha Majid WCA4Z IBM Z Customer Council Boston June 2024.pdf
Baha Majid WCA4Z IBM Z Customer Council Boston June 2024.pdfBaha Majid WCA4Z IBM Z Customer Council Boston June 2024.pdf
Baha Majid WCA4Z IBM Z Customer Council Boston June 2024.pdf
 
A Comprehensive Guide on Implementing Real-World Mobile Testing Strategies fo...
A Comprehensive Guide on Implementing Real-World Mobile Testing Strategies fo...A Comprehensive Guide on Implementing Real-World Mobile Testing Strategies fo...
A Comprehensive Guide on Implementing Real-World Mobile Testing Strategies fo...
 
Superpower Your Apache Kafka Applications Development with Complementary Open...
Superpower Your Apache Kafka Applications Development with Complementary Open...Superpower Your Apache Kafka Applications Development with Complementary Open...
Superpower Your Apache Kafka Applications Development with Complementary Open...
 
🏎️Tech Transformation: DevOps Insights from the Experts 👩‍💻
🏎️Tech Transformation: DevOps Insights from the Experts 👩‍💻🏎️Tech Transformation: DevOps Insights from the Experts 👩‍💻
🏎️Tech Transformation: DevOps Insights from the Experts 👩‍💻
 
Software Test Automation - A Comprehensive Guide on Automated Testing.pdf
Software Test Automation - A Comprehensive Guide on Automated Testing.pdfSoftware Test Automation - A Comprehensive Guide on Automated Testing.pdf
Software Test Automation - A Comprehensive Guide on Automated Testing.pdf
 
bgiolcb
bgiolcbbgiolcb
bgiolcb
 
一比一原版(USF毕业证)旧金山大学毕业证如何办理
一比一原版(USF毕业证)旧金山大学毕业证如何办理一比一原版(USF毕业证)旧金山大学毕业证如何办理
一比一原版(USF毕业证)旧金山大学毕业证如何办理
 
一比一原版(sdsu毕业证书)圣地亚哥州立大学毕业证如何办理
一比一原版(sdsu毕业证书)圣地亚哥州立大学毕业证如何办理一比一原版(sdsu毕业证书)圣地亚哥州立大学毕业证如何办理
一比一原版(sdsu毕业证书)圣地亚哥州立大学毕业证如何办理
 
Safelyio Toolbox Talk Softwate & App (How To Digitize Safety Meetings)
Safelyio Toolbox Talk Softwate & App (How To Digitize Safety Meetings)Safelyio Toolbox Talk Softwate & App (How To Digitize Safety Meetings)
Safelyio Toolbox Talk Softwate & App (How To Digitize Safety Meetings)
 
Streamlining End-to-End Testing Automation
Streamlining End-to-End Testing AutomationStreamlining End-to-End Testing Automation
Streamlining End-to-End Testing Automation
 
Ensuring Efficiency and Speed with Practical Solutions for Clinical Operations
Ensuring Efficiency and Speed with Practical Solutions for Clinical OperationsEnsuring Efficiency and Speed with Practical Solutions for Clinical Operations
Ensuring Efficiency and Speed with Practical Solutions for Clinical Operations
 
Alluxio Webinar | 10x Faster Trino Queries on Your Data Platform
Alluxio Webinar | 10x Faster Trino Queries on Your Data PlatformAlluxio Webinar | 10x Faster Trino Queries on Your Data Platform
Alluxio Webinar | 10x Faster Trino Queries on Your Data Platform
 
Penify - Let AI do the Documentation, you write the Code.
Penify - Let AI do the Documentation, you write the Code.Penify - Let AI do the Documentation, you write the Code.
Penify - Let AI do the Documentation, you write the Code.
 
The Rising Future of CPaaS in the Middle East 2024
The Rising Future of CPaaS in the Middle East 2024The Rising Future of CPaaS in the Middle East 2024
The Rising Future of CPaaS in the Middle East 2024
 
Transforming Product Development using OnePlan To Boost Efficiency and Innova...
Transforming Product Development using OnePlan To Boost Efficiency and Innova...Transforming Product Development using OnePlan To Boost Efficiency and Innova...
Transforming Product Development using OnePlan To Boost Efficiency and Innova...
 

Avl tree

  • 1. #include<stdio.h> #include<stdlib.h> int breakloop = 0; struct Data{ int number; int height; Data *left, *right, *parent; }*root; Data *createNode(int number, Data *parent){ Data *curr = (Data*)malloc(sizeof(Data)); curr->number = number; curr->left = curr->right = NULL; curr->parent = parent; curr->height = 1; return curr; } void push(Data **curr,int number, Data *parent){ if(!(*curr)){ (*curr) = createNode(number, parent); if((*curr)->parent) printf("parent : %dn", (*curr)->parent->number); } else if (number == (*curr)->number){ printf("Data can't samen"); return; } else if(number < (*curr)->number) push(&(*curr)->left,number,(*curr)); else if(number > (*curr)->number) push(&(*curr)->right,number,(*curr)); } int max(int a, int b){ if(a<=b) return b; if(a>b) return a; } void rotateLL(Data *curr){ Data *mid = curr->left; Data *last = mid->left; printf("curr : %dn",curr->number); printf("mid : %dn",mid->number); printf("last : %dn",last->number); Data *temp = NULL; if(mid->right){ printf("ada tempn"); temp = mid->right; } if(curr == root){ printf("masuk curr == rootn"); root = mid; mid->right = curr;
  • 2. curr->parent = mid; curr->left = NULL; }else{ printf("masuk curr != rootn"); mid->parent = curr->parent; printf("mid->parent : %dn",curr->parent->height); if(curr->parent->right->height == curr->height){ curr->parent->right = mid; printf("parent->rightn"); } else if(curr->parent->left->height == curr->height){ curr->parent->left = mid; printf("parent->leftn"); } curr->parent = mid; mid->right = curr; curr->left = NULL; } if(temp){ curr->left = temp; temp->parent = curr; } } void rotateRR(Data *curr){ Data *mid = curr->right; Data *last = mid->right; printf("curr : %dn",curr->number); printf("mid : %dn",mid->number); printf("last : %dn",last->number); Data *temp = NULL; if(mid->left){ printf("ada tempn"); temp = mid->left; } if(curr == root){ printf("masuk curr == rootn"); root = mid; mid->left = curr; curr->parent = mid; curr->right = NULL; }else{ printf("masuk curr != rootn"); mid->parent = curr->parent; if(curr->parent->left->height == curr->height) curr->parent->left = mid; else if (curr->parent->right->height == curr->height) curr->parent->right = mid; curr->parent = mid; mid->left = curr; curr->right = NULL; } if(temp){ curr->right = temp; temp->parent = curr; } }
  • 3. void rotateLR(Data *curr){ Data *mid = curr->left; Data *last = mid->right; printf("curr : %dn",curr->number); printf("mid : %dn",mid->number); printf("last : %dn",last->number); Data *temp = NULL; if(last->left){ printf("ada tempn"); temp = last->left; } printf("masuk curr == rootn"); last->left = mid; last->parent = curr; curr->left = last; mid->parent = last; mid->right = NULL; if(temp){ mid->right = temp; temp->parent = mid; } } void rotateRL(Data *curr){ Data *mid = curr->right; Data *last = mid->left; printf("curr : %dn",curr->number); printf("mid : %dn",mid->number); printf("last : %dn",last->number); Data *temp = NULL; if(last->right){ printf("ada tempn"); temp = last->right; } last->right = mid; last->parent = curr; curr->right = last; mid->parent = last; mid->left = NULL; if(temp){ mid->left = temp; temp->parent = mid; } } int checkBalance(Data *curr){ int balance = 0; if(!curr) return 0; checkBalance(curr->left); checkBalance(curr->right); if(breakloop != 0){ printf("Berhentin"); return 1; } if(!curr->left && !curr->right) balance = 0; else if(!curr->left){
  • 4. balance = curr->right->height; } else if(!curr->right){ balance = curr->left->height; } else{ balance= curr->left->height - curr->right->height; } printf("%d->%dn",curr->number, balance); if(balance <-1 || balance > 1){ if(curr->left && curr->left->left){ printf("Rotate LLn"); rotateLL(curr); }else if(curr->right && curr->right->right){ printf("Rotate RRn"); rotateRR(curr); }else if(curr->left && curr->left->right){ printf("Rotate LRn"); rotateLR(curr); rotateLL(curr); }else if(curr->right && curr->right->left){ printf("Rotate RLn"); rotateRL(curr); rotateRR(curr); } breakloop = 1; return 1; } return 0; } void checkHeight(Data *curr){ if(!curr) return; checkHeight(curr->left); checkHeight(curr->right); { if(!curr->left && !curr->right) curr->height = 1; else if(!curr->left){ curr->height = 1 + curr->right->height; } else if(!curr->right){ curr->height = 1 + curr->left->height; } else{ curr->height = 1+ max(curr->left->height,curr->right->height); } printf("%d->%dn",curr->number,curr->height); } } void postOrder(Data **curr){ if(!(*curr)) return; postOrder(&(*curr)->left); postOrder(&(*curr)->right); printf("%d ",(*curr)->number);
  • 5. } void inOrder(Data **curr){ if(!(*curr)) return; inOrder(&(*curr)->left); printf("%d ",(*curr)->number); inOrder(&(*curr)->right); } void preOrder(Data **curr){ if(!(*curr)) return; printf("%d ",(*curr)->number); preOrder(&(*curr)->left); preOrder(&(*curr)->right); } int searchTF(int number, Data *curr){ if(!curr){ return 0; } else if(number == curr->number) return 1; else if(number< curr->number){ searchTF(number,curr->left); } else if(number > curr->number){ searchTF(number,curr->right); } } Data *search(int number, Data *curr){ if(number == curr->number) return curr; else if(number< curr->number){ search(number,curr->left); } else if(number > curr->number){ search(number,curr->right); } } void deleteTree(int delet, Data *curr){ printf("Delete %dn",curr->number); if(!curr->left && !curr->right){ printf("!curr->left && !curr->rightn"); if(!curr->parent){ printf("Masuk !curr->parentn"); root = NULL; free(curr); } else if(curr->parent->left && curr->parent->left->number == curr->number){ printf("curr->parent->left->number == curr->numbern"); curr->parent->left = NULL; free(curr); }
  • 6. else if(curr->parent->right && curr->parent->right->number == curr- >number){ printf("curr->parent->right->number == curr->numbern"); curr->parent->right = NULL; free(curr); } }else if(curr->left && curr->right){ printf("curr->left && curr->rightn"); Data *swap = curr; swap = swap->left; printf("swap : %dn",swap->number); while(swap->right != NULL){ swap = swap->right; printf("swap : %dn",swap->number); } int temp = swap->number; swap->number = curr->number; curr->number = temp; deleteTree(delet,swap); }else if(curr->left){ printf("curr->leftn"); Data *swap = curr; while(swap->left != NULL){ swap = swap->left; int temp = swap->number; swap->number = curr->number; curr->number = temp; } deleteTree(delet,swap); }else if(curr->right){ printf("curr->rightn"); Data *swap = curr; while(swap->right != NULL){ swap = swap->right; int temp = swap->number; swap->number = curr->number; curr->number = temp; } deleteTree(delet,swap); } printf("selesain"); } int main(){ int choose = 0; do{ choose = 0; int chooseView = 0; printf("1. Insertn"); printf("2. Deleten"); printf("3. Viewn"); printf("4. Exitn"); scanf("%d",&choose); fflush(stdin); switch(choose){ case 1: int check; check = 0;
  • 7. int insert; printf("Insert Number : "); scanf("%d",&insert); fflush(stdin); push(&root,insert, NULL); do{ breakloop = 0; printf("Check Height: n"); checkHeight(root); printf("Check Balance: n"); check = checkBalance(root); printf("check : %dn",check); }while(check != 0); break; case 2: int delet; printf("Delete Number : "); scanf("%d",&delet); fflush(stdin); if(searchTF(delet,root) ==0) printf("No Datan"); else if (searchTF(delet,root) == 1){ printf("Masuk deletn"); deleteTree(delet,search(delet,root)); } do{ breakloop = 0; printf("Check Height: n"); checkHeight(root); printf("Check Balance: n"); check = checkBalance(root); printf("check : %dn",check); }while(check != 0); break; case 3: do{ chooseView = 0; printf("1. PreOrdern"); printf("2. InOrdern"); printf("3. PostOrdern"); printf("4. Exitn"); scanf("%d",&chooseView); fflush(stdin); switch(chooseView){ case 1: preOrder(&root); puts(""); break; case 2: inOrder(&root); puts(""); break; case 3: postOrder(&root); puts(""); break; } }while(chooseView != 4);