Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

AVL Tree: A Self Balancing Binary Tree #1367

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
284 changes: 284 additions & 0 deletions c/AVL Tree/avltree.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,284 @@

#include <stdio.h>
#include <stdlib.h>

typedef struct Node
{
int data;
struct Node *left;
struct Node *right;
int height;
} Node;

int height(Node *n)
{
if (n == NULL)
return 0;
return n->height;
}

int Max(int a, int b)
{
if (a > b)
return a;

else
return b;
}

Node *createNode(int key)
{
Node *node = (Node *)malloc(sizeof(Node));
node->data = key;
node->left = NULL;
node->right = NULL;
node->height = 1;
return (node);
}

Node *rightRotate(Node *y)
{
Node *x = y->left;
Node *z = x->right;

x->right = y;
y->left = z;

y->height = Max(height(y->left), height(y->right)) + 1;
x->height = Max(height(x->left), height(x->right)) + 1;

return x;
}

Node *leftRotate(Node *x)
{
Node *y = x->right;
Node *z = y->left;

y->left = x;
x->right = z;

x->height = Max(height(x->left), height(x->right)) + 1;
y->height = Max(height(y->left), height(y->right)) + 1;

return y;
}

int getBalance(Node *n)
{
if (n == NULL)
return 0;
return height(n->left) - height(n->right);
}

void Print(Node *root)
{
if (root != NULL)
{
printf("%d ", root->data);
Print(root->left);
Print(root->right);
}

else
return;
}

Node *minValueNode(Node *node)
{
Node *current = node;

while (current->left != NULL)
current = current->left;

return current;
}

void Find(Node *root, int key)
{
Node *current = root;

while (current != NULL)
{
if (current->data == key)
{
printf("The number is present in the AVL tree\n");
return;
}

else if (current->data > key)
current = current->left;
else
current = current->right;
}

printf("The number doesn't exist in the AVL tree\n");
return;
}

Node *Insert(Node *node, int key)
{

if (node == NULL)
return (createNode(key));

if (key < node->data)
node->left = Insert(node->left, key);

else if (key > node->data)
node->right = Insert(node->right, key);

else
return node;

node->height = 1 + Max(height(node->left), height(node->right));

int balance = getBalance(node);

// Left Left Rotation Case
if (balance > 1 && key < node->left->data)
return rightRotate(node);

// Right Right Rotation Case
if (balance < -1 && key > node->right->data)
return leftRotate(node);

// Left Right Rotation Case
if (balance > 1 && key > node->left->data)
{
node->left = leftRotate(node->left);
return rightRotate(node);
}

// Right Left Rotation Case
if (balance < -1 && key < node->right->data)
{
node->right = rightRotate(node->right);
return leftRotate(node);
}

return node;
}

Node *deleteNode(Node *root, int key)
{
if (root == NULL)
return root;

if (key < root->data)
root->left = deleteNode(root->left, key);

else if (key > root->data)
root->right = deleteNode(root->right, key);

else
{
if ((root->left == NULL) || (root->right == NULL))
{
Node *temp = root->left ? root->left : root->right;

if (temp == NULL)
{
temp = root;
root = NULL;
}
else
*root = *temp;
free(temp);
}
else
{
Node *temp = minValueNode(root->right);
root->data = temp->data;
root->right = deleteNode(root->right, temp->data);
}
}

if (root == NULL)
return root;

root->height = 1 + Max(height(root->left), height(root->right));

int balance = getBalance(root);

// Left Left Case
if (balance > 1 && getBalance(root->left) >= 0)
return rightRotate(root);

// Left Right Case
if (balance > 1 && getBalance(root->left) < 0)
{
root->left = leftRotate(root->left);
return rightRotate(root);
}

// Right Right Case
if (balance < -1 && getBalance(root->right) <= 0)
return leftRotate(root);

// Right Left Case
if (balance < -1 && getBalance(root->right) > 0)
{
root->right = rightRotate(root->right);
return leftRotate(root);
}

return root;
}

int main()
{
Node *g_root = NULL;
int choice, key_var_insert, key_var_delete, key_var_find;

while (1)
{
printf("\n");
printf(" _____________________________________________________________\n");
printf("|************AVL:SELF BALANCING TREE DATA STRUCTURE***********|\n");
printf("|---------------------------MENU------------------------------|\n");
printf("|1.Insert A Key in the AVL Tree |\n");
printf("|2.Find a Key in the AVL Tree |\n");
printf("|3.Delete a Key from the AVL Tree |\n");
printf("|4.Print the AVL Tree |\n");
printf("|_____________________________________________________________|\n");
printf("Enter your choice: \n");
scanf("%d", &choice);

switch (choice)
{
case 1:
{
printf("Enter the number you wish to insert:\n");
scanf("%d", &key_var_insert);
g_root = Insert(g_root, key_var_insert);
break;
}

case 2:
{
printf("Enter the number you wish to search for: \n");
scanf("%d", &key_var_find);
Find(g_root, key_var_find);
break;
}

case 3:
{
printf("Enter the number you wish to delete:\n");
scanf("%d", &key_var_delete);
g_root = deleteNode(g_root, key_var_delete);
break;
}

case 4:
{
Print(g_root);
printf("\n");
break;
}
}
}

return 0;
}
63 changes: 26 additions & 37 deletions cpp/algorithms/TernarySearch.cpp
Original file line number Diff line number Diff line change
@@ -1,51 +1,40 @@
#include<bits/stdc++.h>
/*
terniary search finds position of an element in a UNIMODAL function or array filled
* unimodally
* https://en.wikipedia.org/wiki/Unimodality
*/
#include<iostream>
using namespace std;

int ternary_search(vector<int> ar,int l,int r, int x)
const int SIZE = 10000005;
int arr[SIZE];
int ternary_search(int l,int r, int x)
{
if(r>=l)
{
int mid1 = l + (r-l)/3;
int mid2 = r - (r-l)/3;
if(ar[mid1] == x)
int mid2 = r - (r-l)/3;
if(arr[mid1] == x)
return mid1;
if(ar[mid2] == x)
if(arr[mid2] == x)
return mid2;
if(x<ar[mid1])
return ternary_search(ar,l,mid1-1,x);
else if(x>ar[mid2])
return ternary_search(ar,mid2+1,r,x);
if(x<arr[mid1])
return ternary_search(l,mid1-1,x);
else if(x>arr[mid2])
return ternary_search(mid2+1,r,x);
else
return ternary_search(ar,mid1+1,mid2-1,x);
return ternary_search(mid1+1,mid2-1,x);

}
return -1;
}


int main()
{
vector <int>v;
int n;
cout<<"Enter size of list\n";
cin>>n;
int no,k;
cout<<"Enter nos\n";
while(n--)
{
cin>>no;
v.push_back(no);
}
cout<<"Enter number you want to search"<<endl;

cin>>k;
int l=0;
int r=v.size()-1;
int found = ternary_search(v,l,r,k);
if(found == -1)
std::cout<<"Couldn't find number"<<endl;
else
{cout<<"Number is found at position "<<++found;
}
return 0;
}
int sz = 100;
for(int i=0;i<sz;i++)
arr[i] = i*2;
int pos = ternary_search(0,sz,10);
if (pos == -1)
cout <<"NOT FOUND\n";
else cout <<"FOUND AT "<<pos+1<<endl;
return 0;
}
34 changes: 18 additions & 16 deletions python/algorithms/Kadane.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,18 @@
def kadane(lista):

max_atual = 0
max_total = -1

for i in range(len(lista)):

max_atual = max(max_atual + lista[i], lista[i])
max_total = max(max_total, max_atual)

if max_total < 0:

max_total = 0

return max_total

# Kadane's Algorithm to find Largest Sum to Contiguous Subarray
from sys import maxint
def maxSubArraySum(a,size):

max_so_far = -maxint - 1
max_ending_here = 0

for i in range(0, size):
max_ending_here = max_ending_here + a[i]
if (max_so_far < max_ending_here):
max_so_far = max_ending_here

if max_ending_here < 0:
max_ending_here = 0
return max_so_far

a = [-13, -3, -25, -20, -3, -16, -23, -12, -5, -22, -15, -4, -7]
print "Maximum contiguous sum is", maxSubArraySum(a,len(a))