谁会二叉树C语言程序?
#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; }。
#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); } }。
问:C语言(不是C++)能写窗口程序(像软件那样)的程序吗?
答:能,累死你,简单的一个比方,你把成熟语言库里的一个经常用的函数重新编写一遍,你就体验到味道了详情>>
答:详情>>