Run Code
|
API
|
Code Wall
|
Misc
|
Feedback
|
Login
|
Theme
|
Privacy
|
Patreon
Find a Node Sibling Test
//Title of this code //gcc 4.9.2 #include<stdlib.h> #include <stdio.h> typedef struct node node ; struct node { int key; node*left; node*right; }; typedef struct tree tree; struct tree{ node*root; }; node*create_node(int anahtar){ node*n=(node*)malloc(sizeof(node)); if(n==NULL)exit(1); n->left=n->right=NULL; n->key=anahtar; return n; } void initialize(int anahtar, tree** T){ *T=(tree*)malloc(sizeof(tree)); if(*T==NULL)exit(1); (*T)->root=(node*)malloc(sizeof(node)); if((*T)->root==NULL)exit(1); (*T)->root=create_node(anahtar); } void add(int anahtar,tree*T){ if(T==NULL)initialize(anahtar,&T); else if(T->root==NULL) T->root=create_node(anahtar); else{ node*copy=T->root; node*parent; while(copy!=NULL){ parent=copy; if(copy->key<anahtar)copy=copy->right; else if(copy->key>anahtar)copy=copy->left; else return; } if(parent->key < anahtar)parent->right=create_node(anahtar); else parent->left=create_node(anahtar); } } void infix(node*root){ if(root==NULL)return; infix(root->left); printf("%4d",root->key); infix(root->right); } node* find_node(node*n,int anahtar){ if(n==NULL)return NULL ; if(n->key==anahtar)return n; else if(n->key<anahtar)return find_node(n->right,anahtar); else if(n->key>anahtar)return find_node(n->left,anahtar); } /* node* find_sibling(node*root,node*n){ if(root==NULL || n==NULL)return NULL; if(root==n) return NULL; node*copy=root; node*parent; while(copy!=n){ if(copy==NULL) return NULL; parent=copy; if(copy->key<n->key)copy=copy->right; else if(copy->key>n->key)copy=copy->left; } if(parent->left==n){ if(parent->right!=NULL)return parent->right;} else if(parent->right==n) if(parent->left!=NULL)return parent->left; } */ node* Kardes_bul(node*root,node*n){ if(root==NULL || n==NULL)return NULL; if(root==n) return NULL; if(root->right==n)return root->left; if(root->left==n)return root->right; node*Left=Kardes_bul(root->left,n); node*Right=Kardes_bul(root->right,n); if(Left==NULL)return Right; else return Left; } int main(void) { tree*T=NULL; initialize(100,&T); add(90,T); add(80,T); add(98,T); add(125,T); add(105,T); add(200,T); infix(T->root); node*n1=find_node(T->root,80); node*n2=Kardes_bul(T->root,n1); printf("\n %d is %d sibling !!!",n2->key,n1->key); return 0; }
run
|
edit
|
history
|
help
0
ptr_to_ptr
C_141105_PuntosTriangulo
150112_CribaErastotenes
Avance de guia 4- Pregunta 1
String manipulation
sum of arrays
time conversion1
ChangeStructVal
qsort
Logical Series