您好,登錄后才能下訂單哦!
#include <stdio.h> #include <stdlib.h> #include <string.h> #define __MAX_LENGTH__ 20 typedef struct SqStack{//調(diào)用棧 int data[__MAX_LENGTH__]; int top; }SqStack; typedef struct SqBiTree{//順序存儲(chǔ)二叉樹 elemType data[__MAX_LENGTH__]; int len; }SqBiTree; //按先序遍歷一維數(shù)組作為存儲(chǔ)結(jié)構(gòu)的二叉樹 int traTree(SqBiTree *t){ SqStack *stack = calloc(sizeof(SqStack),1); stack->top = 0;//將棧制空; int root; root = 0;//指向根節(jié)點(diǎn); while((stack->top)>-1){//當(dāng)棧不為空時(shí),進(jìn)入循環(huán) while(root<__MAX_LENGTH__&&t->data[root]>='a'){// printf("%c,",t->data[root]);//訪問節(jié)點(diǎn) stack->data[stack->top] = root;//節(jié)點(diǎn)入棧 stack->top ++; root = (root+1) * 2-1;//檢查下一個(gè)左子樹; } if(stack->top >-1){//棧非空訪問,遍歷右子樹; stack->top --; root = (stack->data[stack->top]+1)*2; } } } int main(int argc, char *argv[]) { SqBiTree *p = calloc(sizeof(SqBiTree),1); strcpy(p->data,"abcdefghij"); traTree(p); return 0; }
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。