14二叉树的源代码

#include "stdafx.h"

#include<stdio.h>

#include<conio.h>

#include<Windows.h>

#include<stdlib.h>

#include<assert.h>

//节点类型

typedef struct Node

{

         int data;         //数据域

         Node *left;         //左子树

         Node *right;      //右子树

}Node;

//树的根节点

Node *root=NULL;

 

bool UserSayYes()

{

         printf("是否继续(y/n)\n");

         char ch=getch();

         if(ch=='Y'||ch=='y')

                   return true;

         else

                   return false;

};

 

//创建节点

Node *CreateNode()

{

         int data;

         printf("请输入数据\n");

         scanf("%d",&data);

         Node* pn=(Node*)malloc(sizeof(Node));

         pn->data=data;

         pn->left=NULL;

         pn->right=NULL;

         return pn;

};

 

void Push(Node **proot,Node *pn)

{

         if((*proot)==NULL)

         {

                   (*proot)=pn;

         }

         else if((*proot)!=NULL&&(*proot)->data>pn->data) //插入值小于根节点的值

         {

                   Push(&(*proot)->left,pn);

         }

         else

         {

                   Push(&(*proot)->right,pn);

         };

};

 

//插入节点

void PushNode(Node **proot)

{

         Push(&root,CreateNode());

         if(UserSayYes())

                   PushNode(&root);

};

 

//显示

void ShowNode(Node* root)

{

         if(root!=NULL)

         {

                   ShowNode(root->left);

                   printf("%d ",root->data);

                   ShowNode(root->right);

         };

};

 

//遍历

Node *FindNode(Node* p,int num)

{

         if(p==NULL)

                   printf("error p is NULL\n");

         else if(p->data==num)

                   return p;

         else if(p->data>num)

                   FindNode(p->left,num);

         else

                   FindNode(p->right,num);

};

 

//修改

void SeleteNode(Node *root)

{

         int num1;           //要修改的值

         printf("\n请输入要修改的数值\n");

         scanf("%d",&num1);

         Node *ptr=FindNode(root,num1);

         printf("请输入修改后的数值\n");

         scanf("%d",&ptr->data);

 

};

 

//删除

void DeleteNode(Node *proot)

{

         int num;

         printf("\n请输入要删除的数值\n");

         scanf("%d",&num);

         Node *p=FindNode(proot,num);

         Node *temp=p;

         if(p->left==NULL&&p->right==NULL)          //节点后左右子树均为空

         {

                   free(p);

                   p=NULL;

         }

         else if(p->left!=NULL&&p->right==NULL)     //节点后左子树不为空,右子树为空

         {

                   p=p->left;

         }

         else if(p->left==NULL&&p->right!=NULL)          //节点后左子树为空,右子树不为空

         {

                   p=p->right;

 

         }

         else if(p->left!=NULL&&p->right!=NULL)    //节点后左右子树均不为空

         {

                   p->left=p->right;

                   p=p->left;

         };

         free(temp);

         temp=NULL;

};

 

//删除全部节点

void Clear(Node** root)

{

         if(*root!=NULL)

         {

                   Clear(&(*root)->left);

                   Clear(&(*root)->right);

                   free(*root);

                   *root=NULL;

         };

};

 

//void DeleteNode(Node *proot)

//{

//      int num;

//      printf("请输入要删除的数值\n");

//      scanf("%d",&num);

//      Node *p=FindNode(proot,num);

//      if(p->left==NULL)

//               Push(&p->left,p->right);

//      Node *temp=p;

//      p=p->right;

//      free(temp);

//      temp=NULL;

//

//}

int main()

{

         PushNode(&root);

         ShowNode(root);

//      SeleteNode(root);

//      ShowNode(root);

         DeleteNode(root);

//      Clear(&root);

//      PushNode(&root);

         ShowNode(root);

         system("pause");

         return 0;

};

posted @ 2018-01-09 18:38  gd_沐辰  阅读(408)  评论(0编辑  收藏  举报