考研复习(7)树的基本操作

这次是树的基本操作,包括生成树,求树深度,求叶子节点个数。。。
附上二叉树的几个重要性质及证明(考研必考)。
1.在二叉树的第i层至多有2^(i-1)个结点;
2.深度为k的二叉树至多有2^(k)-1 个结点;
3.对任何一棵二叉树T,如果其终端结点数为n0,度为2的结点数为n2,则n0=n2+1;(n=n0+n1+n2=n1+2n2+1);
4.具有n哥节点的完全二叉树深度为【log2n】+1;
#include <stdio.h>
#include<stdlib.h>
#define maxSize 20
#define maxWidth 20
typedef struct node
{
char data;
struct node *left,*right;
}Btree;
Btree* createNode(char ch,Btree *l,Btree *r);
//先序遍历
void preOrder(Btree *BT)
{
if(BT!= NULL)
{
printf("%c",BT->data);
preOrder(BT->left);
preOrder(BT->right);
}
}
//中序遍历
void inOrder(Btree *BT)
{
if(BT!= NULL)
{
   inOrder(BT->left);
printf("%c",BT->data);
inOrder(BT->right);
}
}
//后序遍历
void postOrder(Btree *BT)
{
if(BT!= NULL)
{
   postOrder(BT->left);
postOrder(BT->right);
printf("%c",BT->data);
}
}
//从括号表达式生成树
void createTree(Btree **BT,char *str)//create a tree BT  according by hollow function str
{




     Btree *stack[maxSize],*p;
     int top=-1,k,j=0;//top is the point of stack,k is the tab of son,j is the point of str;
     char ch;
     *BT=NULL;
     ch=str[j];


     while(ch!=NULL)
     {
          switch(ch)
          {
          case'(':top++;stack[top]=p;k=1;//left son,push to stack
          break;
          case')':top--;//pop stack
          break;
          case ',':k=2;//right son
          break;
          default:
          /*p=(Btree *)malloc(sizeof(Btree));//set a node
          if(!(p))
          {
               exit(0);
          }
          p->data=ch;
          p->left=p->right=NULL;*/
          p=createNode(ch,NULL,NULL);
          if(*BT==NULL)//the root node
          {
               *BT=p;
          }
          else
          {
               switch(k)
               {
                 case 1:stack[top]->left=p;
                 break;
                 case 2:stack[top]->right=p;
                 break;
                 default:
                 ;
               }


          }
          }
          j++;
          ch=str[j];//get next char
     }
}
/*
void createTree(Btree **BT,char *str)
{
     int j=0;//top is the point of stack,k is the tab of son,j is the point of str;
     Btree *T;
     char ch;
     ch=str[j];
     if(ch=='')  *BT=NULL;
     else
     {
          if(!(T=))


     }
}
*/
//生成节点
Btree* createNode(char ch,Btree *l,Btree *r)
{
     printf("Create now!\n");
     Btree *p;
     p=(Btree *)malloc(sizeof(Btree));//set a node
     if(!(p))
     {
          exit(0);
     }
     p->data=ch;
     p->left=l;
     p->right=r;
     return p;
}
//复制树
//copy a tree and return the new tree's point
Btree* copyTree(Btree *T)
{


     Btree *newptl,*newptr,*newT;
     if(!T) return NULL;
     if(T->left!=NULL)
     {
          newptl=copyTree(T->left);


     }
     else newptl=NULL;
     if(T->right!=NULL) newptr=copyTree(T->right);
     else newptr=NULL;
     newT=createNode(T->data,newptl,newptr);
     //newT=createNode(T->data,NULL,NULL);
     //newT=NULL;
     return newT;
}
//层次法打印树
void disptree(Btree *BT)
{
     Btree *stack[maxSize],*p;
     int level[maxSize][2],top,n,i,width=4;
     if(BT!=NULL)
     {
          printf("Display a tree by hollow means.\n");
          top=1;
          stack[top]=BT;//push root point to stack.
          level[top][0]=width;
          while(top>0)
          {
               p=stack[top];
               n=level[top][0];
               for(i=1;i<=n;i++)
               printf(" ");
               printf("%c",p->data);
               for(i=n+1;i<maxWidth;i+=2)
               printf("--");
               printf("\n");
               top--;
               if(p->right!=NULL)
               {
                    top++;
                    stack[top]=p->right;
                    level[top][0]=n+width;
                    level[top][1]=2;
               }
               if(p->left!=NULL)
               {
                    top++;
                    stack[top]=p->left;
                    level[top][0]=n+width;
                    level[top][1]=1;
               }
          }




     }
}
int TreeDepth(Btree *BT)//calculate the depth of tree
{
     int leftDep,rightDep;
     if(BT==NULL) return 0;
     else
     {
          leftDep=TreeDepth(BT->left);
          rightDep=TreeDepth(BT->right);
          if(leftDep>rightDep) return(leftDep+1);
          else return (rightDep+1);
     }
}
int NodeCount(Btree *BT)//cout the nodes
{
     if(BT==NULL) return 0;
     else return (NodeCount(BT->left)+NodeCount(BT->right)+1);
}
int LeafCount(Btree *BT)//count the leafnodes
{
     if(BT==NULL) return 0;
     else if(BT->left==NULL&&BT->right==NULL) return 1;
     else return (LeafCount(BT->left)+LeafCount(BT->right));
}
void PrintTree(Btree *BT)//print tree
{
     if(BT!=NULL)
     {
          printf("%c",BT->data);
          if(BT->left!=NULL||BT->right!=NULL)
          {
               printf("(");
               PrintTree(BT->left);
               if(BT->right!=NULL) printf(",");
               PrintTree(BT->right);
               printf(")");
          }
     }
}
main()
{
     Btree *B,*C;
     char *s="A(B(D,E(H,I)),C(G))";
     createTree(&B,s);
     printf("preOder:");
     preOrder(B);
     printf("\n");
     printf("inOder:");
     inOrder(B);
     printf("\n");
     printf("postOder:");
     postOrder(B);
     printf("\n");
     printf("Display tree\n");
     disptree(B);
     printf("copy tree\n");
     //disptree(B);
     C=copyTree(B);
     preOrder(C);
     disptree(C);


     printf("Deep:  %d\n",TreeDepth(B));
     printf("Sum of nodes:  %d\n",NodeCount(B));
     printf("Sum of leafnodes:  %d\n",LeafCount(B));
     PrintTree(B);
printf("\nHello,world!");
}


  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
对于数据结构的考研复习规划,可以按照以下步骤进行: 1. 熟悉考纲和教材:首先,详细了解考试大纲,并根据大纲选择合适的教材。阅读教材,了解数据结构的基本概念、常用算法和数据结构的实现原理。 2. 制定学习计划:根据复习时间和个人情况,制定合理的学习计划。将整个知识点分成小块,每天安排一到两个主题进行深入学习。 3. 系统学习基础知识:首先,从基本数据结构开始,如数组、链表、栈、队列等。学习它们的定义、操作和常见应用。然后,深入了解、图等高级数据结构。 4. 理解算法设计和分析:学习各种常用算法的设计思路和实现方法,如排序算法、查找算法、图算法等。重点掌握它们的时间复杂度和空间复杂度分析。 5. 多做练习题:通过大量练习题来巩固所学知识。可以选择一些经典题目和历年真题进行训练,提高解题能力。 6. 复习与总结:在复习过程中,及时总结所学知识点,整理笔记,强化记忆。复习过程中发现的问题和难点,可以积极寻求解答和帮助。 7. 模拟考试:复习结束前,进行一些模拟考试,检验自己的学习成果,同时熟悉考试形式和时间管理。 8. 增强实践能力:除了理论知识,还要注重实践能力的培养。尝试实现一些常用数据结构和算法,通过编程实践加深对数据结构的理解。 总之,数据结构考研复习需要有系统性和规划性。合理分配时间和精力,多做练习,注重理论与实践相结合,相信你能够取得好的成绩!

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值