爱问知识人 爱问教育 医院库

谁会二叉树C语言程序?

首页

谁会二叉树C语言程序?


        

提交回答
好评回答
  • 2018-04-05 10:29:49
      #include<stdio。h>#defineQUEUE_MAX_SIZE20#defineSTACK_MAX_SIZE10  typedefintelemType;#include"BT。c"  /************************************************************************/  /*          以下是关于二叉搜索树操作的4个简单算法       */  /************************************************************************/  /*1。
      查找*/  /*递归算法*/  elemType*findBSTree1(structBTreeNode*bst,elemTypex)  {  /*树为空则返回NULL*/  if(bst==NULL){  returnNULL;  }else{  if(x==bst->data){  return&(bst->data);  }else{  if(x<bst->data){  /*向左子树查找并直接返回*/  returnfindBSTree1(bst->left,x);  }else{    /*向右子树查找并直接返回*/  returnfindBSTree1(bst->right,x);  }  }  }  }  /*非递归算法*/  elemType*findBSTree2(structBTreeNode*bst,elemTypex)  {  while(bst!=NULL){  if(x==bst->data){  return&(bst->data);  }elseif(x<bst->data){  bst=bst->left;  }else{  bst=bst->right;  }  }  returnNULL;  }  /*2。
      插入*/  /*递归算法*/  voidinsertBSTree1(structBTreeNode**bst,elemTypex)  {  /*新建一个根结点*/  if(*bst==NULL){  structBTreeNode*p=(structBTreeNode*)malloc(sizeof(structBTreeNode));  p->data=x;  p->left=p->right=NULL;  *bst=p;  return;  }elseif(x<(*bst)->data){    /*向左子树完成插入运算*/  insertBSTree1(&((*bst)->left),x);  }else{    /*向右子树完成插入运算*/  insertBSTree1(&((*bst)->right),x);  }  }  /*非递归算法*/  voidinsertBSTree2(structBTreeNode**bst,elemTypex)  {  structBTreeNode*p;  structBTreeNode*t=*bst,*parent=NULL;  /*为待插入的元素查找插入位置*/  while(t!=NULL){  parent=t;  if(x<t->data){  t=t->left;  }else{  t=t->right;  }  }  /*建立值为x,左右指针域为空的新结点*/  p=(structBTreeNode*)malloc(sizeof(structBTreeNode));  p->data=x;  p->left=p->right=NULL;  /*将新结点链接到指针为空的位置*/  if(parent==NULL){  *bst=p;    /*作为根结点插入*/  }elseif(x<parent->data){    /*链接到左指针域*/  parent->left=p;  }else{  parent->right=p;  }  return;  }  /*3。
      建立*/  voidcreateBSTree(structBTreeNode**bst,elemTypea[],intn)  {  inti;  *bst=NULL;  for(i=0;i<n;i++){  insertBSTree1(bst,a[i]);  }  return;  }。
      

    e***

    2018-04-05 10:29:49

其他答案

    2018-04-05 11:29:49
  •   #include<stdio。h> #include<stdlib。
      h>typedef struct tree { char data; struct tree *l;/*左儿子*/ struct tree *r; }tr; int main() { tr *head=NULL; tr *create(tr *t);/*二叉树的建立*/ void preorder(tr *t); void inorder(tr *t); void postorder(tr *t); head=create(head); printf("\n\n"); printf("\nThe preorder is:"); preorder(head); printf("\nThe inorder is:"); inorder(head); printf("\nThe postorder is:"); postorder(head); } tr *create(tr *t)/*二叉树的建立*/{tr *k=NULL; char ch; scanf("%s",&ch); if(ch=='!')t=NULL; else { t=(tr *)malloc(sizeof(tr)); t->data=ch; t->l=t->r=NULL; t->l=create(k); t->r=create(k); } return(t); } void preorder(tr *head)/*前序遍历*/ { tr *t=NULL; t=head; if(t) { printf("%c\t",t->data); preorder(t->l); preorder(t->l); } } void inorder(tr *head)/*中序遍历*/ { tr *t=NULL; t=head; if(t) { inorder(t->l); printf("%c\t",t->data); inorder(t->l); } } void postorder(tr *head)/*后序遍历*/ { tr *t=NULL; t=head; if(t) { postorder(t->l); postorder(t->l); printf("%c\t",t->data); } }。

    吟***

    2018-04-05 11:29:49

类似问题

换一换

相关推荐

正在加载...
最新问答 推荐信息 热门专题 热点推荐
  • 1-20
  • 21-40
  • 41-60
  • 61-80
  • 81-100
  • 101-120
  • 121-140
  • 141-160
  • 161-180
  • 181-200
  • 1-20
  • 21-40
  • 41-60
  • 61-80
  • 81-100
  • 101-120
  • 121-140
  • 141-160
  • 161-180
  • 181-200
  • 1-20
  • 21-40
  • 41-60
  • 61-80
  • 81-100
  • 101-120
  • 121-140
  • 141-160
  • 161-180
  • 181-200
  • 1-20
  • 21-40
  • 41-60
  • 61-80
  • 81-100
  • 101-120
  • 121-140
  • 141-160
  • 161-180
  • 181-200

热点检索

  • 1-20
  • 21-40
  • 41-60
  • 61-80
  • 81-100
  • 101-120
  • 121-140
  • 141-160
  • 161-180
  • 181-200
返回
顶部
帮助 意见
反馈

确定举报此问题

举报原因(必选):