// Tree.cpp : Defines the entry point for the console application.
//
/**********************************************************************/
/*
* Author : Leaf_KA [LiWen.Zhang]
* Function : 二叉数操作
*
/***********************************************************************/
#include "stdafx.h"
#include <string.h>
#include <malloc.h>
#include <stdlib.h>
//
/**********************************************************************/
/*
* Author : Leaf_KA [LiWen.Zhang]
* Function : 二叉数操作
*
/***********************************************************************/
#include "stdafx.h"
#include <string.h>
#include <malloc.h>
#include <stdlib.h>
#define LINE "-------------------------------------------------------\n"
//#include <conio.h>
typedef struct NODE_PTR
{
int _id;
int age;
char name[80];
NODE_PTR *right_tree;
NODE_PTR *left_tree;
}NODE,*NODE_P;
////////////////////////////////////////////
NODE_P root_t = NULL;
NODE_P ControlTree(NODE_P root, int _id, int age, char* name);
int DisplayTree(NODE_P root,char* name);
void DisplayAllTree(NODE_P root);
int DeleteNode(NODE_P node,int id);
///////////////////////////////////////////////////
NODE_P ControlTree(NODE_P root, int _id, int age, char* name)
{
if (!root)
{
root = (NODE_P)malloc(sizeof(NODE));
root->_id = _id;
root->age = age;
strcpy(root->name,name);
root->left_tree = NULL;
root->right_tree = NULL;
}
else
{
if ( age >= root->age) //right
{
if (root->right_tree)
{
ControlTree(root->right_tree,_id,age,name);
}
else
{
NODE_P new_node = (NODE_P)malloc(sizeof(NODE));
new_node->_id = _id;
new_node->age = age;
strcpy(new_node->name,name);
new_node->left_tree = NULL;
new_node->right_tree = NULL;
root->right_tree = new_node;
}
}
else //left
{
if (root->left_tree)
{
ControlTree(root->left_tree,_id,age,name);
}
else
{
NODE_P new_node = (NODE_P)malloc(sizeof(NODE));
new_node->_id = _id;
new_node->age = age;
strcpy(new_node->name,name);
new_node->left_tree = NULL;
new_node->right_tree = NULL;
root->left_tree = new_node;
}
}
}
return root;
}
///////////////////////////////////////////////////////////////////
int DisplayTree(NODE_P root,char* name)
{
if (!root)
return 0;
if (!strcmp(root->name,name))
{
printf("\nID = % 5d, age = % 5d, Name = % 8s\n",root->_id,root->age,root->name);
DisplayTree(root->left_tree,name);
DisplayTree(root->right_tree,name);
return 1;
}
DisplayTree(root->left_tree,name);
DisplayTree(root->right_tree,name);
return 0;
}
/////////////////////////////////////////////////////////////////////
void DisplayAllTree(NODE_P root)
{
if (!root)
return;
printf("\nID = % 5d, age = % 5d, Name = % 8s\n",root->_id,root->age,root->name);
DisplayAllTree(root->left_tree);
DisplayAllTree(root->right_tree);
}
//////////////////////////////////////////////////////////////////
int DeleteNode(NODE_P node, int id)
{
NODE_P curr_node = NULL, sele_node = NULL;
if (!node)
return 0;
sele_node = node;
if (sele_node->_id != id)
{
//#include <conio.h>
typedef struct NODE_PTR
{
int _id;
int age;
char name[80];
NODE_PTR *right_tree;
NODE_PTR *left_tree;
}NODE,*NODE_P;
////////////////////////////////////////////
NODE_P root_t = NULL;
NODE_P ControlTree(NODE_P root, int _id, int age, char* name);
int DisplayTree(NODE_P root,char* name);
void DisplayAllTree(NODE_P root);
int DeleteNode(NODE_P node,int id);
///////////////////////////////////////////////////
NODE_P ControlTree(NODE_P root, int _id, int age, char* name)
{
if (!root)
{
root = (NODE_P)malloc(sizeof(NODE));
root->_id = _id;
root->age = age;
strcpy(root->name,name);
root->left_tree = NULL;
root->right_tree = NULL;
}
else
{
if ( age >= root->age) //right
{
if (root->right_tree)
{
ControlTree(root->right_tree,_id,age,name);
}
else
{
NODE_P new_node = (NODE_P)malloc(sizeof(NODE));
new_node->_id = _id;
new_node->age = age;
strcpy(new_node->name,name);
new_node->left_tree = NULL;
new_node->right_tree = NULL;
root->right_tree = new_node;
}
}
else //left
{
if (root->left_tree)
{
ControlTree(root->left_tree,_id,age,name);
}
else
{
NODE_P new_node = (NODE_P)malloc(sizeof(NODE));
new_node->_id = _id;
new_node->age = age;
strcpy(new_node->name,name);
new_node->left_tree = NULL;
new_node->right_tree = NULL;
root->left_tree = new_node;
}
}
}
return root;
}
///////////////////////////////////////////////////////////////////
int DisplayTree(NODE_P root,char* name)
{
if (!root)
return 0;
if (!strcmp(root->name,name))
{
printf("\nID = % 5d, age = % 5d, Name = % 8s\n",root->_id,root->age,root->name);
DisplayTree(root->left_tree,name);
DisplayTree(root->right_tree,name);
return 1;
}
DisplayTree(root->left_tree,name);
DisplayTree(root->right_tree,name);
return 0;
}
/////////////////////////////////////////////////////////////////////
void DisplayAllTree(NODE_P root)
{
if (!root)
return;
printf("\nID = % 5d, age = % 5d, Name = % 8s\n",root->_id,root->age,root->name);
DisplayAllTree(root->left_tree);
DisplayAllTree(root->right_tree);
}
//////////////////////////////////////////////////////////////////
int DeleteNode(NODE_P node, int id)
{
NODE_P curr_node = NULL, sele_node = NULL;
if (!node)
return 0;
sele_node = node;
if (sele_node->_id != id)
{
DeleteNode(node->left_tree,id);
DeleteNode(node->right_tree,id);
}
else
{
curr_node = node;
if (curr_node->left_tree)
sele_node->left_tree = curr_node->left_tree;
if (curr_node->right_tree)
sele_node->right_tree = curr_node->right_tree;
curr_node = node;
free(curr_node);
curr_node = NULL;
}
// DeleteNode(node->left_tree,id);
// DeleteNode(node->right_tree,id);
return 0;
}
//////////////////////////////////////////////////////////////////
int main(int argc, char* argv[])
{
int exit_flag = 0;
int flag = 0;
int num = 0;
while(!exit_flag)
{
printf("\n1 Display Tree");
printf("\n2 Insert Node");
printf("\n3 Delete Node");
printf("\n4 Select Name");
printf("\n5 Clear screen");
printf("\n6 Exit\n");
printf(LINE);
printf("\nPlease input number\n");
DeleteNode(node->right_tree,id);
}
else
{
curr_node = node;
if (curr_node->left_tree)
sele_node->left_tree = curr_node->left_tree;
if (curr_node->right_tree)
sele_node->right_tree = curr_node->right_tree;
curr_node = node;
free(curr_node);
curr_node = NULL;
}
// DeleteNode(node->left_tree,id);
// DeleteNode(node->right_tree,id);
return 0;
}
//////////////////////////////////////////////////////////////////
int main(int argc, char* argv[])
{
int exit_flag = 0;
int flag = 0;
int num = 0;
while(!exit_flag)
{
printf("\n1 Display Tree");
printf("\n2 Insert Node");
printf("\n3 Delete Node");
printf("\n4 Select Name");
printf("\n5 Clear screen");
printf("\n6 Exit\n");
printf(LINE);
printf("\nPlease input number\n");
scanf("%d",&flag);
switch(flag)
{
case 1:
if (!root_t)
printf("It is empty tree");
else
DisplayAllTree(root_t);
break;
case 2:
{
printf(LINE);
//int age;
char name[80];
//printf("\nInput name = ");
//scanf("%s",name);
//printf("\nInput age = ");
//scanf("%d",&age);
if (!root_t)
root_t = ControlTree(root_t,num,5,"KA");
for (int i = 0; i < 10; i++)
{
if (i == 5)
continue;
sprintf(name,"KA%d",i);
root_t = ControlTree(root_t,++num,i,name);
}
}
break;
case 3:
{
int id;
printf("\nPlease input id");
scanf("%d",&id);
DeleteNode(root_t,id);
}
break;
case 4:
{
char name[80];
if (root_t)
{
printf("\nPlease input name for selected\n");
scanf("%s",name);
if (!DisplayTree(root_t,name))
{
printf(LINE);
printf("Can not find %s",name);
}
}
}
break;
case 5:
system("cls");
break;
case 6:
exit_flag = 1;
break;
default:
break;
}
}
return 0;
}
switch(flag)
{
case 1:
if (!root_t)
printf("It is empty tree");
else
DisplayAllTree(root_t);
break;
case 2:
{
printf(LINE);
//int age;
char name[80];
//printf("\nInput name = ");
//scanf("%s",name);
//printf("\nInput age = ");
//scanf("%d",&age);
if (!root_t)
root_t = ControlTree(root_t,num,5,"KA");
for (int i = 0; i < 10; i++)
{
if (i == 5)
continue;
sprintf(name,"KA%d",i);
root_t = ControlTree(root_t,++num,i,name);
}
}
break;
case 3:
{
int id;
printf("\nPlease input id");
scanf("%d",&id);
DeleteNode(root_t,id);
}
break;
case 4:
{
char name[80];
if (root_t)
{
printf("\nPlease input name for selected\n");
scanf("%s",name);
if (!DisplayTree(root_t,name))
{
printf(LINE);
printf("Can not find %s",name);
}
}
}
break;
case 5:
system("cls");
break;
case 6:
exit_flag = 1;
break;
default:
break;
}
}
return 0;
}
操作提示