| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 311 人关注过本帖
标题:求版主,求高手进,- -,这好难!
取消只看楼主 加入收藏
xiaoshalong
Rank: 2
等 级:论坛游民
帖 子:20
专家分:16
注 册:2013-1-10
结帖率:0
收藏
 问题点数:0 回复次数:0 
求版主,求高手进,- -,这好难!
这是一个实现二叉树的程序,有简单的,结点插入,删除,报告结点数,还有遍历输出等功能,我都写两天了,终于可以运行,所预想的功能不没有全部实现,现在发现的是无法遍历输出和删除。求大神帮忙发现错误!!!
程序代码:
/* 实现函数*/
#include <stdio.h>
#include <string.h>
#include <stdbool.h>
#include "tree.h"

enum allchoices {addone,deleteone,itemcount,search,showall,cleanall};
const char * choices[]={"addone","deleteone","itemcount","search","showall","cleanall"};

#define LEN 15
void menu(void);
void AddOne (Tree *ptree);
void DeleteOne(Tree *ptree);
void ItemCount(const Tree *ptree);
void FindOne(const Tree *ptree);
void ShowAll(const Tree *ptree);
void CleanAll(Tree *ptree);
void PrintItem(Item item);

int main(void)
{
    Tree classmates;
    char yourchoice[LEN];
    enum allchoices  choice;
    bool the_order_iseffective=false;
    InitializeTree(&classmates);

     menu();
    while(strcmp(gets(yourchoice),"quit")!=0&&yourchoice[0]!='\0')
    {
        for(choice=addone;choice<=cleanall;choice++)
        {
            if(strcmp(yourchoice,choices[choice])==0)
            {
                the_order_iseffective=true;
                break;
            }
        }
        if(the_order_iseffective)
            switch (choice)
        {
            case (addone): AddOne(&classmates);
            break;
            case (deleteone):DeleteOne(&classmates);
            break;
            case(itemcount):ItemCount(&classmates);
            break;
            case(search):FindOne(&classmates);
            break;
            case(showall):ShowAll(&classmates);
            break;
            case(cleanall):CleanAll(&classmates);
            break;

        }
        else
            printf("i don\'t know what the order mean!\n");
        the_order_iseffective=false;
        while(getchar()!='\n')
            continue;
        puts("next order\n");
         menu();

    }
    puts("good job\n");
    return 0;
}

void menu(void)
{
     puts("please chioce what do you want to to?\n");
     puts("enter \"addone\" to add a classmate.\n");
     puts("enter \"deleteone\" to delete a classmate\n");
     puts("enter \"itemcount\" to konw the number of your classmatas\n");
     puts("enter \"search\" to find your classmate\n");
     puts("enter\"showall\"to printf all of your classmates.\n");
     puts("enter \"cleanall\"to chean all items. \n");
     puts("enter \"quit\" to quit\n");
     return 0;
}

void AddOne (Tree *ptree)
{
    Item temp;
    if(TreeIsFull(ptree))
        puts("no room to accept a new one!\n");
    else
        {
        puts("please enter your classmate\'s name:\n");
        gets(temp.name);
        puts("please enter sex of your classmate:\n");
        gets(temp.sex);
        puts("please enter age of your classmate:\n");
        scanf("%d",&temp.age);
        AddItem(&temp,ptree);
        }


}

void DeleteOne(Tree *ptree)
{
   Item temp;

   if(TreeIsEmpty(ptree))
   {
       puts("no members to put out!\n");
       return;
    }

    puts("please enter name of classmate you want to delete:\n");
    gets(temp.name);
    if(DeleteItem(&temp,ptree))
        printf("sccessfully\n");
    else
        puts("is not a members\n");

}

void ItemCount(const Tree *ptree)
{
    unsigned int number;
    number=TreeItemCount(ptree);
    printf("there are %d  classmates\n",number);


}
void FindOne(const Tree *ptree)
{
    Item temp;

    if(TreeIsEmpty(ptree))
    {
        puts("no information in the tree!");
        return;
    }
    puts("please enter name of classmate you want to find:\n");
    gets(temp.name);
    if(InTree(&temp,ptree))
        puts("is a member in the class!");
        else
        puts("is not a member");

}


void ShowAll(const Tree *ptree)
{
    if(TreeIsEmpty(ptree))
    {
        puts("no information in the tree!");
        return;
    }
    else
        Traverse(ptree,PrintItem);

}

void CleanAll(Tree *ptree)
{
    DeleteAll(ptree);
    printf("just clean all\n");
}

void PrintItem(Item item)
{
   printf("name:%s,\tsex:%s,\tage:%d",item.name,item.sex,item.age);
}


程序代码:
/*实现头文件功能*/
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include "tree.h"

static Node * MakeNode(const Item *pitem);
static bool ToLeft(const Item *pitem1,const Item *pitem2);
static bool ToRight(const Item *pitem1,const Item *pitem2);
static void AddNode(Node * new_node,Node *root);
static void TraverseTree(const Node *root,void (*pfun)(Item item));
static Pair SeekItem(const Item *pitem,const Tree *ptree);
static void DeleteNode(Node **pt);
static void DeleteAllNode(Node *root);


void InitializeTree(Tree *ptree)
{
       ptree->root=NULL;
       ptree->size=0;
}

bool TreeIsEmpty(const Tree *ptree)
{
    if(ptree->root==NULL)
        return true;
    else
        return false;
}

bool TreeIsFull(const Tree *ptree)
{
    if(ptree->size==MAXSIZE)
        return true;
    else
        return false;
}

unsigned int TreeItemCount(const Tree *ptree)
{
    return ptree->size;
}

bool AddItem(const Item *pitem,Tree *ptree)

 {
     Node *new_node;
     if(SeekItem(pitem,ptree).child!=NULL)
     {
         printf("the item is already exist.");
         return false;
     }
     new_node=MakeNode(pitem);
     if(new_node==NULL)
     {
         printf("FAIED TO CREAT A NODE!");
         return false;
     }
     ptree->size++;
     if(ptree->root==NULL)
        ptree->root=new_node;
     else
        AddNode(new_node,ptree->root);

 }


 bool InTree(const Item *pitem,const Tree *ptree)

 {
     return(SeekItem(pitem,ptree).child==NULL?false:true);

 }


 bool DeleteItem(const Item *pitem,Tree *ptree)

 {
     Pair temp;

     temp=SeekItem(pitem,ptree);
     if(temp.child==NULL)
        return false;
     else if(temp.parent==NULL)
        DeleteNode(&ptree->root);
     else
        DeleteNode(&temp.child);                //is there a difference between left node and right node;
     ptree->size--;
     return true;


 }

 void Traverse(const Tree *ptree,void(*pfun)(Item item))

 {
     if(ptree!=NULL)
        TraverseTree(ptree->root,pfun);

 }


 void DeleteAll(Tree *ptree)


 {
     if(ptree->root!=NULL)
        DeleteAllNode(ptree->root);
     ptree->root=NULL;
     ptree->size=0;
}

static Node *MakeNode(const Item *pitem)
{
    Node * new_node;
    new_node=(Node *)malloc(sizeof(Node));
    if(new_node!=NULL)
    {
        new_node->item=*pitem;
        new_node->left=NULL;
        new_node->right=NULL;
    }
    return new_node;
}

static bool ToLeft(const Item *pitem1,const Item *pitem2)
{
    int flag;
    if(flag=strcmp(pitem1->name,pitem2->name)<0)
        return true;
    else
        return false;
}

static bool ToRight (const Item *pitem1,const Item*pitem2)
{
    int flag;
    if(flag=strcmp(pitem1->name,pitem2->name)<0)
        return true;
    else
        return false;
}

static void AddNode(Node *new_node,Node *root)
{
    if(ToLeft(&new_node->item,&(root->item)))
    {
        if(root->left==NULL)
                    root->left=&new_node;
        else
              AddNode(new_node,root->left);

    }
    else if(ToRight(&new_node->item,&(root->item)))
          if(root->right==NULL)
             root->right=&new_node;
            else
            AddNode(new_node,root->right);

}

static void TraverseTree(const Node *root,void (*pfun)(Item item))
{
    if(root!=NULL)
    {
        TraverseTree(root->left,pfun);
        (*pfun)(root->item);
        TraverseTree(root->right,pfun);
    }
    else
        printf(("the tree is empty!"));
}

static Pair SeekItem(const Item *pitem,const Tree *ptree)
{
    Pair temp;
    temp.parent=NULL;
    temp.child=ptree->root;

  while(temp.child!=NULL)
  {
      if(ToLeft(pitem,&temp.child->item))
      {
       temp.parent=temp.child;
       temp.child=temp.child->left;

      }
      else if (ToRight(pitem,&temp.child->item))
      {
          temp.parent=temp.child;
          temp.child=temp.child->right;
      }
      else
        break;
    }
    return temp;
}

static void DeleteNode(Node **pt)
{
    Node * temp;
    if((*pt)->left=NULL)
    {
        temp=*pt;
        *pt=(*pt)->right;
        free(temp);
    }
    else if ((*pt)->right=NULL)
    {
        temp=*pt;
        *pt=(*pt)->left;
        free(temp);
    }
    else
    {
     for(temp=(*pt)->left;temp->right==NULL;temp=temp->right)
        break;
     temp->right=(*pt)->right;
     temp=*pt;
     (*pt)=(*pt)->left;
          free(temp);
    }
}

static void DeleteAllNode(Node * root)
{
   Node *pright;
   if (root!=NULL)
   {
       pright=root->right;
       DeleteAllNode(root->left);
       free(root);
       DeleteAllNode(pright);
   }
}


程序代码:
/*头文件*/
#ifndef _TREE_H_
#define _TREE_H_
#include <stdbool.h>

#define SIZE 15
#define MAXSIZE 15

typedef struct item
{
    char name[SIZE];
    char sex[SIZE];
    int age;
} Item;

typedef struct node
{
    Item item;
    struct node * left;
    struct node * right;
} Node;

typedef struct tree
{
    Node *root;
    unsigned int size;
} Tree;

typedef struct pair
{
    Node * parent;
    Node * child;
}Pair;

void InitializeTree(Tree *ptree);
bool TreeIsEmpty(const Tree *ptree);
bool TreeIsFull(const Tree *ptree);
unsigned int TreeItemCount(const Tree*ptree);
bool AddItem(const Item *pitem,Tree *ptree);
bool InTree(const Item *pitem,const Tree *ptree);
bool DeleteItem(const Item *pitem,Tree *ptree);
void Traverse(const Tree *ptree,void(*pun)(Item item));
void DeleteAll(Tree *ptree);


#endif // _TREE_H_

搜索更多相关主题的帖子: 二叉树 color 
2013-06-11 20:34
快速回复:求版主,求高手进,- -,这好难!
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.050549 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved