Notes![what is notes.io? What is notes.io?](/theme/images/whatisnotesio.png)
![]() ![]() Notes - notes.io |
#include<stdlib.h>
#include<stdbool.h>
struct node
{
int data;
struct node *left;
struct node *right;
};
//struct node *root = NULL;
int n =100;
struct node *que[100];
int front = -1;
int rear = -1;
int findMaxOfNumber(int a,int b)
{
if (a>b)
{
return a;
}
else
return b;
}
int findHeight(struct node *root)
{
int leftHeight;
int rightHeight;
if (root == NULL)
{
return 0;
}
leftHeight = findHeight(root->left);
rightHeight = findHeight(root->right);
return (findMaxOfNumber(leftHeight,rightHeight)+1);
}
struct node *findMin(struct node * root)
{
while(root->left != NULL)
root = root->left;
return root;
}
void nq(struct node *item)
{
if ( rear == n-1)
{
printf("dont fuck with my coden");
return;
}
else if (front == -1 )
{
front = 0 ;
rear = 0;
}
else{
rear +=1;
}
que[rear] = item;
}
void dq()
{
if (front == -1)
{
printf("empty hai n");
return;
}
//printf("dequed item is %dn",que[front] );
if (front == rear)
{
front = -1;
rear = -1;
return;
/* code */
}
front +=1;
}
struct node* peak()
{
if (front == -1)
{
printf("queue is emptyn");
return;
}
return que[front];
}
void preorder(struct node *root)
{
if (root == NULL)
{
return;
}
printf("%dn",root->data);
preorder(root->left);
preorder(root->right);
}
void inorder(struct node *root)
{
if (root == NULL)
{
return;
}
inorder(root->left);
printf("%dn",root->data);
inorder(root->right);
}
struct node *createNode(int item)
{
struct node *new = (struct node *)malloc(sizeof(struct node));
new -> data = item;
new -> left = NULL;
new -> right = NULL;
return new;
}
struct node *insertNode(struct node *root,int item)
{
if (root == NULL)
{
struct node *new = createNode(item);
root = new;
return new;
}
else if (item <= root->data)
{
root->left = insertNode(root->left,item);
}
else
{
root->right = insertNode(root->right,item);
}
return root;
}
bool search(struct node *root,int data)
{
if (root == NULL)
{
return false;
}
else if (data == root->data )
{
return true;
}
else if (data <= root->data)
{
return search(root->left,data);
}
else
return search(root->right,data);
}
struct node* delete(struct node *root,int data)
{
if (root == NULL)
{
return;
}
else if (root->data < data)
{
delete(root->right,data);
}
else if (root->data > data)
{
delete(root->left,data);
}
else
{
if (root->left == NULL && root->right == NULL)
{
free(root);
root = NULL;
}
else if (root->left == NULL)
{
root = root->right;
}
else if (root->right == NULL)
{
root = root->left;
}
else
{
struct node *temp = findMin(root->right);
root->data = temp->data;
root->right = delete(root->right,temp->data);
}
}
return root;
}
bool que_notEmpty()
{
if (front == -1 && rear == -1)
{
return true;
}
return false;
}
void levelOrder(struct node *root)
{
if(root == NULL) return;
nq(root);
while(!que_notEmpty())
{
struct node *current = peak();
struct node *current_left = current->left;
struct node *current_right = current->right;
dq();
if(current_left != NULL)
nq(current_left);
if(current_right != NULL) nq(current_right);
printf("%dn",current->data);
}
}
struct node *find_node(struct node *root,int data)
{
if (root == NULL)
{
return NULL;
}
if (root->data == data)
{
return root;
}
if (data > root->data )
{
return find_node(root->right,data);
}
if (data < root->data)
{
return find_node(root->left,data);
}
}
struct node *getsuccessor(struct node *root,int data)
{
struct node *current_node = find_node(root,data);
if(current_node == NULL) return NULL;
if(current_node->right!=NULL)
return findMin(root->right);
else
{
struct node *prev = root;
struct node *successor = NULL;
while(successor!=prev)
{
if(current_node->data < prev->data)
{
successor = prev;
prev = prev->left;
}
else
prev = prev ->right;
}
return successor;
}
}
int main()
{
struct node *root = NULL;
root = insertNode(root,10);
root = insertNode(root,20);
root = insertNode(root,3);
root = insertNode(root,0);
root = insertNode(root,14);
root = insertNode(root,24);
printf("enter value to be searchn");
int i;
scanf("%d",&i);
if (search(root,i))
{
printf("foundn");
}
else
printf("not foundn");
root = delete(root,10);
//levelOrder(root);
printf("inorder isn");
//inorder(root);
int k = findHeight(root);
printf("%dn",k);
printf("successor isn");
if (getsuccessor(root,20)!=NULL)
{
/* code */
printf("%dn",getsuccessor(root,20)->data );
}
return 0;
}
![]() |
Notes is a web-based application for online taking notes. You can take your notes and share with others people. If you like taking long notes, notes.io is designed for you. To date, over 8,000,000,000+ notes created and continuing...
With notes.io;
- * You can take a note from anywhere and any device with internet connection.
- * You can share the notes in social platforms (YouTube, Facebook, Twitter, instagram etc.).
- * You can quickly share your contents without website, blog and e-mail.
- * You don't need to create any Account to share a note. As you wish you can use quick, easy and best shortened notes with sms, websites, e-mail, or messaging services (WhatsApp, iMessage, Telegram, Signal).
- * Notes.io has fabulous infrastructure design for a short link and allows you to share the note as an easy and understandable link.
Fast: Notes.io is built for speed and performance. You can take a notes quickly and browse your archive.
Easy: Notes.io doesn’t require installation. Just write and share note!
Short: Notes.io’s url just 8 character. You’ll get shorten link of your note when you want to share. (Ex: notes.io/q )
Free: Notes.io works for 14 years and has been free since the day it was started.
You immediately create your first note and start sharing with the ones you wish. If you want to contact us, you can use the following communication channels;
Email: [email protected]
Twitter: http://twitter.com/notesio
Instagram: http://instagram.com/notes.io
Facebook: http://facebook.com/notesio
Regards;
Notes.io Team