#include <stdio.h>
#include <malloc.h>
typedef struct node {
struct node *left;
int data;
struct node *right;
}BST , *PBST;
PBST GetNode(){
PBST node = (PBST) malloc (sizeof (BST));
node->left = node->right = NULL;
return node;
}
void InsertNode(PBST *root , int data){
if ((*root) == NULL){
(*root) = GetNode();
(*root)->data = data;
}
else if ((*root)->data > data )
InsertNode(&(*root)->left, data);
else
InsertNode(&(*root)->right, data);
}
void Pop (PBST *root)
{
if(*root==NULL)
{
return;
}
if((*root)->data != 0)
{
printf("%d\n",(*root)->data);
}
if((*root)->left!=0)
{
Pop(&(*root)->left);
}
if((*root)->right!=0)
{
Pop(&(*root)->right);
}
}
void Pop1 (PBST *root)
{
if(*root==NULL)
{
return;
}
if((*root)->left!=0)
{
Pop1(&(*root)->left);
}
if((*root)->data != 0)
{
printf("%d\n",(*root)->data);
}
if((*root)->right!=0)
{
Pop1(&(*root)->right);
}
}
void Pop2 (PBST *root)
{
if(*root==NULL)
{
return;
}
if((*root)->left!=0)
{
Pop2(&(*root)->left);
}
if((*root)->right!=0)
{
Pop2(&(*root)->right);
}
if((*root)->data != 0)
{
printf("%d\n",(*root)->data);
}
}
int Del1(PBST *root)
{
int keroro;
if((*root)->left !=NULL)
return Del1(&(*root)->left);
if((*root)->left ==NULL)
keroro = (*root)->data;
free(*root);
*root=NULL;
return keroro;
}
void Del(PBST *root , int data)
{
if(data!=(*root)->data)
{
if((*root)->left->data == data)
printf("그딴거 없다.\n");
return;
}
if(data == (*root)->data)
{
(*root)->data = Del1(&(*root)->right);
}
}
int Del2(PBST *root) *root 의 오른쪽주소를 따라왔을때
{
int keroro;
if((*root)->left !=NULL)
return Del2(&(*root)->left);
if((*root)->left ==NULL)
keroro = (*root)->data;
if((*root)->right==NULL)
{
free(*root);
*root=NULL;
return keroro;
}
else
{
*root=(*root)->right;
free(*root);
*root=NULL;
return keroro;
}
}
int Del3(PBST *root) *root 의 왼쪽주소를 따라왔을때
{
int keroro;
if((*root)->right !=NULL)
return Del3(&(*root)->left);
if((*root)->right ==NULL)
keroro = (*root)->data;
if((*root)->left==NULL)
{
free(*root);
*root=NULL;
return keroro;
}
else
{
*root=(*root)->left;
free(*root);
*root=NULL;
return keroro;
}
}
void Del(PBST *root , int data)
{
data크기 비교해서 이동
if(*root!=NULL)
{
if((*root)->data<data)
Del(&(*root)->right,data);
else if((*root)->data>data)
Del(&(*root)->left,data);
else if((*root)->data == data)
{
if((*root)->right && (*root)->left !=NULL)
(*root)->data = Del2(&(*root)->right);
else if((*root)->right==NULL && (*root)->left == NULL)
{
free(*root);
*root=NULL;
}
else if((*root)->left==NULL)
{
(*root)->data = Del2(&(*root)->right);
}
else if((*root)->right==NULL)
{
(*root)->data = Del3(&(*root)->left);
}
}
else
printf("그딴거 없다.\n");
return;
}
else
return;
}
void main (){
PBST root = NULL;
InsertNode(&root, 10);
InsertNode(&root, 20);
InsertNode(&root, 5);
InsertNode(&root, 22);
InsertNode(&root, 24);
InsertNode(&root, 19);
InsertNode(&root, 21);
InsertNode(&root, 2);
InsertNode(&root, 9);
InsertNode(&root, 6);
InsertNode(&root, 13);
InsertNode(&root, 23);
printf("전위순회\n");
Pop(&root);
printf("중위순회\n");
Pop1(&root);
printf("후위 순회\n");
Pop2(&root);
printf("삭제후 전위순회\n");
Del(&root,19);
Pop(&root);
}