数据结构与算法源程序下载

时间:2012-04-28 07:11:29
【文件属性】:

文件名称:数据结构与算法源程序下载

文件大小:39KB

文件格式:RAR

更新时间:2012-04-28 07:11:29

建立二叉树,从序、先序遍历(用递归或非递归的方法都可以)

#include "stdio.h" #include "malloc.h" struct Bitree { char nodes; struct Bitree *lchild; struct Bitree *rchild; }; struct node { char this; struct node *next; }; build( ) { struct Bitree *b,*bn; /*b指向二叉树,bn为操作数 */ struct node *c,*cn; /*c放置节点,作为一个小的队列.操作cn*/ int i,j,k; char no; i=1; /*每层有i个节点*/ printf("now we'll build the binary tree,,in the process,if the node is not exist ,enter '#'.first enter a boot\n"); /*先输入根节点*/ c=(struct node *)malloc(sizeof(struct node)); b=(struct Bitree *)malloc(sizeof(struct Bitree)); scanf("%c",&no); c->this=no; b->nodes=no; bn=b; do { j=2*i; /*i的下一层有2i个节点,*/ for(k=1;k<=i;k++) { bn=c->this; printf("the left child of %c:",c->this); scanf("%c",&no); bn->lchild=(struct Bitree *)malloc(sizeof(struct Bitree)); bn->lchild->nodes=no; if (no=="#") { j=j-1; /*如果节点的孩子为空,节点数-1*/ } else { cn->next=(struct Bitree *)malloc(sizeof(struct Bitree)); cn->next->this=no; cn=cn->next; }; printf("the right child of %c:",c->this); scanf("%c",&no); bn->rchild=(struct Bitree *)malloc(sizeof(struct Bitree)); bn->rchild->nodes=no; if(no=="#") { j=j-1; } else { cn->next=(struct Bitree *)malloc(sizeof(struct Bitree)); cn->next->this=no; cn=cn->next; }; c=c->next; }; i=j; }while(i!=0); } firstorder(struct Bitree *n) { printf("%c",n->nodes); if(n->nodes=="#") ; else firstorder(n->lchild); firstorder(n->rchild); } main() { struct Bitree *o; build(o); firstorder(o); } 回答:2006-06-07 16:51 提问者对答案的评价: 共0条评论...其他回答 共2条回答评论 ┆ 举报 飞鱼 [新手] 二叉树实现源代码如下: #include #include #include #define OK 1 #define ERROR 0 #define TRUE 1 #define FALSE 0 #define OVERFLOW -2 typedef int status; typedef struct BiNode { char Data; struct BiNode* lChild; struct BiNode* rChild; }BiNode,*pBiNode; status CreateTree(BiNode** pTree); status PreOrderTraval(BiNode* pTree); status Visit(char Data); status Display(BiNode* pTree,int Level); status Clear(BiNode* pTree); BiNode *pRoot=NULL; main() { clrscr(); CreateTree(&pRoot); printf("\nPreOrder:"); PreOrderTraval(pRoot); printf("\n"); printf("\nInOrder:"); InOrderTraval(pRoot); printf("\n"); printf("\nPostOrder:"); PostOrderTraval(pRoot); printf("\n"); printf("\nShowLeaves:"); ShowLeaves(pRoot); printf("\n-----------------------\n"); printf("\n"); Display(pRoot,0); printf("\n"); printf("\nDeleting Tree:\n"); DelTree(pRoot); printf("BiTree Deleted."); getch(); } status CreateTree(BiNode** pTree) /*Input Example: abd##e##cf##g##*/ { char ch; scanf("%c",&ch); if(ch==‘#‘) { (*pTree)=NULL; } else { if(!((*pTree)=(BiNode*)malloc(sizeof(BiNode)))) { exit(OVERFLOW); } (*pTree)->Data=ch; CreateTree(&((*pTree)->lChild)); CreateTree(&((*pTree)->rChild)); } return OK; } status PreOrderTraval(BiNode* pTree) { if(pTree) { if(Visit(pTree->Data)) { if(PreOrderTraval(pTree->lChild)) { if(PreOrderTraval(pTree->rChild)) { return OK; } } } return ERROR; } else { return OK; } } status InOrderTraval(BiNode* pTree) { if(pTree) { if(InOrderTraval(pTree->lChild)) { if(Visit(pTree->Data)) { if(InOrderTraval(pTree->rChild)) { return OK; } } return ERROR; } return ERROR; } else { return OK; } } status PostOrderTraval(BiNode* pTree) { if(pTree) { if(PostOrderTraval(pTree->lChild)) { if(PostOrderTraval(pTree->rChild)) { if(Visit(pTree->Data)) { return OK; } return ERROR; } } return ERROR; } else { return OK; } } status Visit(char Data) { printf("%c",Data); return OK; } status Display(BiNode* pTree,int Level) { int i; if(pTree==NULL) return; Display(pTree->lChild,Level+1); for(i=0;i=1) { printf("--"); } printf("%c\n",pTree->Data); Display(pTree->rChild,Level+1); } status ShowLeaves(BiNode* pTree) { if(pTree) { if(ShowLeaves(pTree->lChild)) { if(ShowLeaves(pTree->rChild)) { if((pTree->lChild==NULL)&&(pTree->rChild==NULL)) { if(!Visit(pTree->Data)) { return ERROR; } } return OK; } } return ERROR; } else { return OK; } } status DelTree(BiNode* pTree) { if(pTree) { if(DelTree(pTree->lChild)) { if(DelTree(pTree->rChild)) { printf("Deleting %c\n",pTree->Data); free((void*)pTree); return OK; } } return ERROR; } else { return OK; } }


【文件预览】:
数据结构源代码
----topsort.cpp(3KB)
----kruskal.cpp(1KB)
----btree.cpp(4KB)
----sqqueue.cpp(1KB)
----SeqSearch.cpp(621B)
----dijkstra.cpp(2KB)
----poly.cpp(4KB)
----selectsort.cpp(851B)
----sqlist.cpp(2KB)
----cdlink.cpp(3KB)
----insertsort.cpp(849B)
----jose.cpp(709B)
----BinSearch.cpp(871B)
----dfs.cpp(4KB)
----sqstack.cpp(1KB)
----cslink.cpp(2KB)
----quicksort.cpp(1KB)
----shellsort.cpp(1015B)
----linkstring.cpp(5KB)
----Order.cpp(3KB)
----hash2.cpp(1KB)
----bstree.cpp(4KB)
----dlink.cpp(3KB)
----bfs.cpp(3KB)
----bubblesort.cpp(972B)
----_desktop.ini(9B)
----heapsort.cpp(1KB)
----linkstack.cpp(1KB)
----mergesort.cpp(2KB)
----floyed.cpp(1KB)
----seedoctor.cpp(1KB)
----slink.cpp(3KB)
----createadjlist.cpp(2KB)
----prim.cpp(1KB)
----BlkSearch.cpp(1KB)
----sqstring.cpp(3KB)
----huffman.cpp(2KB)
----hash1.cpp(1KB)
----linkqueue.cpp(2KB)
----radixsort.cpp(2KB)
----creatematix.cpp(1KB)

网友评论