Skip to content

Commit 9d638d4

Browse files
authored
Add files via upload
1 parent 380b6bb commit 9d638d4

File tree

10 files changed

+817
-0
lines changed

10 files changed

+817
-0
lines changed

AVL Tree/AVL_Tree.cpp

+231
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,231 @@
1+
// HASAN CEYHAN 20010011091
2+
3+
// 2 1 7 4 5 3 8 -> EKLE
4+
// 4 2 1 3 7 5 8 -> SIRALA
5+
// 3 -------------> SIL
6+
// 4 2 1 7 5 8 ---> SIRALA
7+
8+
9+
// 10 15 9 12 13 79 45 36 22 -> EKLE
10+
// 13 10 9 12 45 22 15 36 79 -> SIRALA
11+
// 45 ------------------------> SIL
12+
// 17 ------------------------> EKLE
13+
// 13 10 9 12 22 15 17 79 36 -> SIRALA
14+
#include <stdio.h>
15+
#include <stdlib.h>
16+
17+
struct node{
18+
int data;
19+
struct node *sol;
20+
struct node *sag;
21+
int yukseklik;
22+
};
23+
24+
struct node *kokDugumOlustur(int veri){
25+
struct node *root = (struct node *) malloc(sizeof(struct node));
26+
root->data = veri;
27+
root->sol = NULL;
28+
root->sag = NULL;
29+
root->yukseklik = 1;
30+
return (root);
31+
}
32+
33+
int yukseklik(struct node *root) {
34+
if (root == NULL){
35+
return 0;
36+
}
37+
return root->yukseklik;
38+
}
39+
40+
int max(int a, int b){
41+
return (a > b) ? a : b;
42+
}
43+
44+
struct node *sagaDondur(struct node *y) {
45+
struct node *x = y->sol;
46+
struct node *T2 = x->sag;
47+
48+
x->sag = y;
49+
y->sol = T2;
50+
51+
y->yukseklik = max(yukseklik(y->sol), yukseklik(y->sag)) + 1;
52+
x->yukseklik = max(yukseklik(x->sol), yukseklik(x->sag)) + 1;
53+
return x;
54+
}
55+
56+
struct node *solaDondur(struct node *x){
57+
struct node *y = x->sag;
58+
struct node *T2 = y->sol;
59+
60+
y->sol = x;
61+
x->sag = T2;
62+
63+
x->yukseklik = max(yukseklik(x->sol), yukseklik(x->sag)) + 1;
64+
y->yukseklik = max(yukseklik(y->sol), yukseklik(y->sag)) + 1;
65+
return y;
66+
}
67+
68+
// Denge saglanir.
69+
int getBalance(struct node *root) {
70+
if (root == NULL){
71+
return 0;
72+
}
73+
return yukseklik(root->sol) - yukseklik(root->sag);
74+
}
75+
76+
77+
struct node *elemanEkle(struct node *root, int veri) {
78+
// Dugum bos mu kontrol edilir.
79+
if (root == NULL){
80+
return (kokDugumOlustur(veri));
81+
}
82+
83+
if (veri < root->data){
84+
root->sol = elemanEkle(root->sol, veri);
85+
}
86+
else if (veri > root->data){
87+
root->sag = elemanEkle(root->sag, veri);
88+
}
89+
else{
90+
return root;
91+
}
92+
93+
// Her dugumun denge faktoru guncellenir ve agaci dengeli hale getirilir.
94+
root->yukseklik = 1 + max(yukseklik(root->sol), yukseklik(root->sag));
95+
96+
int balance = getBalance(root);
97+
if (balance > 1 && veri < root->sol->data){
98+
return sagaDondur(root);
99+
}
100+
101+
if (balance < -1 && veri > root->sag->data){
102+
return solaDondur(root);
103+
}
104+
105+
if (balance > 1 && veri > root->sol->data){
106+
root->sol = solaDondur(root->sol);
107+
return sagaDondur(root);
108+
}
109+
110+
if (balance < -1 && veri < root->sag->data){
111+
root->sag = sagaDondur(root->sag);
112+
return solaDondur(root);
113+
}
114+
return root;
115+
}
116+
117+
struct node *minValueNode(struct node *root) {
118+
struct node *current = root;
119+
120+
while (current->sol != NULL){
121+
current = current->sol;
122+
}
123+
return current;
124+
}
125+
126+
// Dugum silme
127+
struct node *elemanSil(struct node *root, int silmekIstenilenEleman) {
128+
// Aranan dugum yoksa
129+
if (root == NULL){
130+
return root;
131+
}
132+
133+
if (silmekIstenilenEleman < root->data){
134+
root->sol = elemanSil(root->sol, silmekIstenilenEleman);
135+
}
136+
else if (silmekIstenilenEleman > root->data){
137+
root->sag = elemanSil(root->sag, silmekIstenilenEleman);
138+
}
139+
else{
140+
if ((root->sol == NULL) || (root->sag == NULL)){
141+
struct node *temp = root->sol ? root->sol : root->sag;
142+
if (temp == NULL){
143+
temp = root;
144+
root = NULL;
145+
}
146+
else{
147+
*root = *temp;
148+
}
149+
free(temp);
150+
}
151+
else{
152+
struct node *temp = minValueNode(root->sag);
153+
root->data = temp->data;
154+
root->sag = elemanSil(root->sag, temp->data);
155+
}
156+
}
157+
if(root == NULL){
158+
return root;
159+
}
160+
161+
// Her dugumun denge faktoru guncellenir ve agaci dengeli hale getirilir.
162+
root->yukseklik = 1 + max(yukseklik(root->sol), yukseklik(root->sag));
163+
164+
int balance = getBalance(root);
165+
if(balance > 1 && getBalance(root->sol) >= 0){
166+
return sagaDondur(root);
167+
}
168+
if(balance > 1 && getBalance(root->sol) < 0){
169+
root->sol = solaDondur(root->sol);
170+
return sagaDondur(root);
171+
}
172+
173+
if(balance < -1 && getBalance(root->sag) <= 0){
174+
return solaDondur(root);
175+
}
176+
177+
if(balance < -1 && getBalance(root->sag) > 0){
178+
root->sag = sagaDondur(root->sag);
179+
return solaDondur(root);
180+
}
181+
return root;
182+
}
183+
184+
185+
void ekranaYazdir(struct node *root) {
186+
if (root != NULL) {
187+
printf("%d ", root->data);
188+
ekranaYazdir(root->sol);
189+
ekranaYazdir(root->sag);
190+
}
191+
}
192+
193+
int main() {
194+
struct node *eleman = NULL;
195+
196+
while(1){
197+
int sayi, secim;
198+
printf("\n********************MENU********************");
199+
printf("\nEklemek Icin -----------------------------> 1\n");
200+
printf("Agactan Veri Silmek Icin -----------------> 2\n");
201+
printf("Ekrana Preorder Ile Yazdirmak Icin -------> 3\n");
202+
printf("Cikis Icin -------------------------------> 0\n");
203+
printf("Seciminizi Yapin: "); scanf("%d", &secim);
204+
switch(secim){
205+
case 1: printf("\n\nSayi Giriniz: "); scanf("%d", &sayi);
206+
eleman = elemanEkle(eleman, sayi);
207+
break;
208+
case 2: printf("\n\nSilmek Istediginiz Elemani Giriniz: "); scanf("%d", &sayi);
209+
eleman = elemanSil(eleman, sayi);
210+
break;
211+
case 3: system("cls");
212+
ekranaYazdir(eleman);
213+
break;
214+
case 0: printf("\nCikis Saglandi!");
215+
return 0;
216+
default: printf("Hatali Veri Girisi!\n");
217+
}
218+
}
219+
// 2 1 7 4 5 3 8 -> EKLE
220+
// 4 2 1 3 7 5 8 -> SIRALA
221+
// 3 -------------> SIL
222+
// 4 2 1 7 5 8 ---> SIRALA
223+
224+
225+
// 10 15 9 12 13 79 45 36 22 -> EKLE
226+
// 13 10 9 12 45 22 15 36 79 -> SIRALA
227+
// 45 ------------------------> SIL
228+
// 17 ------------------------> EKLE
229+
// 13 10 9 12 22 15 17 79 36 -> SIRALA
230+
return 0;
231+
}

AVL Tree/README.txt

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
The above code is used to perform operations such as adding elements, deleting elements, and printing the AVL tree in preorder traversal.
2+
3+
The main() function presents a menu to the user and allows them to choose the desired operation. The menu options include adding elements, deleting elements, and printing the tree in preorder traversal. The appropriate functions are used for these operations.
4+
5+
In addition, a node structure named struct node is defined. Each node contains a data value, left and right child nodes, and a height value used to determine the balance of the tree.
6+
7+
The rest of the code includes helper functions required for the AVL tree, such as node creation, height calculation, balance checking, rotation operations, element insertion, and element deletion.
8+
9+
The last part of the code contains a loop that runs the menu defined in the main() function and performs the selected operations.
10+
11+
In summary, this project performs operations of adding elements, deleting elements, and sorting the AVL tree for the given elements.
12+
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
13+
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
14+
Proje, AVL ağacında eleman ekleme, eleman silme ve ağacı preorder olarak ekrana yazdırma işlemlerini gerçekleştirmek için kullanılmaktadır.
15+
16+
Ana fonksiyon main(), kullanıcıya bir menü sunar ve kullanıcının istediği işlemi seçmesine olanak sağlar. Menüde kullanıcı, eleman eklemek, eleman silmek ve ağacı preorder olarak ekrana yazdırmak gibi işlemleri seçebilir. Bu işlemler için uygun fonksiyonlar kullanılır.
17+
18+
Ayrıca, struct node adında bir düğüm yapısı tanımlanır. Her bir düğüm, bir veri değeri, sol ve sağ alt düğümleri, ve ağacın denge durumunu belirlemek için kullanılan bir yükseklik değeri içerir.
19+
20+
Kodun geri kalan kısmında, AVL ağacı için gerekli olan düğüm oluşturma, yükseklik hesaplama, dengenin kontrolü, döndürme işlemleri, eleman ekleme ve silme işlemleri gibi yardımcı fonksiyonlar bulunur.
21+
22+
Kodun son kısmında ise, main() fonksiyonunda tanımlanan menüyü çalıştırmak ve işlemleri gerçekleştirmek için gerekli olan döngü yer alır.
23+
24+
Özetle bu proje, verilen elemanların AVL ağacına ekleme, silme ve sıralama işlemlerini gerçekleştirmektedir.

Infix-Postfix/Infix-Postfix.cpp

+103
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
// 20010011091 - Hasan CEYHAN
2+
#include<stdio.h>
3+
#include<string.h>
4+
char stack[100];
5+
int top;
6+
7+
int priority(char a)
8+
{
9+
if (a=='(')
10+
return -1;
11+
else if (a==')')
12+
return 0;
13+
else if (a=='+' || a=='-')
14+
return 1;
15+
else if (a=='*' || a=='/')
16+
return 2;
17+
else if (a=='^')
18+
return 3;
19+
else
20+
return 4;
21+
}
22+
23+
int main()
24+
{
25+
char a[100];
26+
printf("Enter the infix expression: \n");
27+
scanf("%s",a);
28+
char postfix[strlen(a)];
29+
top=-1;
30+
int r=0;
31+
int i,n=strlen(a);
32+
for (i=0;i<n;i++)
33+
{
34+
if (a[i]=='(')
35+
{
36+
push(a[i]);
37+
}
38+
else if (a[i]==')')
39+
{
40+
while (stack[top]!='(')
41+
{
42+
43+
postfix[r]=stack[top];
44+
r++;
45+
pop();
46+
}
47+
}
48+
else
49+
{
50+
if (priority(a[i])==4)
51+
{
52+
postfix[r]=a[i];
53+
r++;
54+
}
55+
else
56+
{
57+
if (priority(a[i])>priority(stack[top]) || (a[i]=='^' && stack[top]=='^'))
58+
{
59+
push(a[i]);
60+
}
61+
else
62+
{
63+
while(priority(a[i])<=priority(stack[top]))
64+
{
65+
postfix[r]=stack[top];
66+
r++;
67+
pop();
68+
}
69+
push(a[i]);
70+
71+
}
72+
}
73+
}
74+
75+
}
76+
while(top!=-1)
77+
{
78+
if (stack[top]!='(' && stack[top]!=')')
79+
{
80+
postfix[r]=stack[top];
81+
r++;
82+
}
83+
84+
pop();
85+
}
86+
printf("The postfix expression is: \n");
87+
for (i=0;i<r;i++)
88+
{
89+
printf("%c",postfix[i]);
90+
}
91+
}
92+
93+
94+
95+
void push(char c)
96+
{
97+
top++;
98+
stack[top]=c;
99+
}
100+
void pop()
101+
{
102+
top--;
103+
}

Infix-Postfix/README.txt

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
This project is a program that converts an infix expression entered by the user into a postfix expression. The code utilizes a stack data structure to perform the conversion from infix to postfix. Operator priorities are taken into consideration during the process and are temporarily stored on the stack. Parentheses are also used to determine the order of operations.
2+
3+
The general workflow of the code is as follows:
4+
5+
1- Prompt the user to enter an infix expression.
6+
2- Create a stack and a temporary array to construct the postfix expression.
7+
3- Iterate through each character of the infix expression.
8+
4- If the character is an opening parenthesis, push it onto the stack.
9+
5- If the character is a closing parenthesis, pop all operators from the stack and append them to the postfix expression until an opening parenthesis is encountered.
10+
6- If the character is an operand, directly append it to the postfix expression.
11+
7- If the character is an operator, compare its priority with the operator at the top of the stack. If the top operator has higher or equal priority, pop it from the stack and append it to the postfix expression. Repeat this process until a lower priority operator is encountered or the stack is empty. Then push the current operator onto the stack.
12+
8- Once all characters have been processed, append any remaining operators from the stack to the postfix expression.
13+
9- Finally, display the postfix expression.
14+
By converting the infix expression to postfix, this project provides convenience in evaluating mathematical expressions.
15+
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
16+
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
17+
Bu proje, kullanıcıdan alınan infix ifadesini postfix ifadeye dönüştüren bir programdır.
18+
Kod, bir stack (yığın) veri yapısı kullanarak infix ifadeyi postfix ifadeye dönüştürmektedir. İşleme sırasında operatörlerin öncelikleri dikkate alınır ve stack üzerinde geçici olarak tutulurlar. Parantezlerin işlem sırasını belirlemek için de kullanılırlar.
19+
20+
Kodun genel işleyişi şu şekildedir:
21+
22+
1- Kullanıcıdan infix ifade alınır.
23+
2- Stack ve geçici bir dizi kullanılarak postfix ifade oluşturulur.
24+
3- Infix ifadenin karakterleri tek tek kontrol edilir.
25+
4- Eğer karakter bir açma parantezi ise, stack'e eklenir.
26+
5- Eğer karakter bir kapama parantezi ise, stack'teki tüm operatörler açma parantezine kadar postfix ifadeye eklenir.
27+
6- Eğer karakter bir operand ise, doğrudan postfix ifadeye eklenir.
28+
7- Eğer karakter bir operatör ise, önceliği stack'teki en üstteki operatöre göre kontrol edilir. Daha düşük önceliğe sahip veya aynı önceliğe sahip operatörler postfix ifadeye eklenir.
29+
8- İşlem tamamlandığında, stack'teki tüm operatörler postfix ifadeye eklenir.
30+
9- Son olarak, postfix ifade ekrana yazdırılır.
31+
Bu şekilde, infix ifade postfix ifadeye dönüştürülerek matematiksel ifadelerin değerlendirilmesinde kolaylık sağlanır.

0 commit comments

Comments
 (0)