二叉树深度优先遍历算法

按你说的部分改了但一直显示ninOrder函数too many arguments to function
,可以帮忙看看哪里错了吗

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
typedef char DataType;
struct SeqBinTree{
 int maxnum;
 int n;
 DataType *nodelist;
};
typedef struct SeqBinTree *PSeqBinTree;

char leftChild(PSeqBinTree t){
    return (t->nodelist[1]);

char rightChild(PSeqBinTree t){
    return (t->nodelist[2]);
}

void visit(PSeqBinTree p){
 printf("%c",p->nodelist[0]);
 return 0;
}

struct SeqStack{
  int maxnum;
  int b;
  DataType *s;
  };
typedef struct SeqStack *PSeqStack;

PSeqBinTree t;
PSeqBinTree creatEmptyBinTree(){
  int m;
  t=(PSeqBinTree*)malloc(sizeof(struct SeqBinTree));
  if(t==NULL){
  return 0;
  }
  t->nodelist=(DataType*)malloc(m*sizeof(DataType));
  t->maxnum=m;
  t->n=0;
  return t;
}

void PreOrder28(PSeqBinTree t){
 if(t==NULL)
 return 0;
 visit(root(t));
 PreOrder28(leftChild(t));
 PreOrder28(rightChild(t));
}

void inOrder28(PSeqBinTree t){
 if(t==NULL)
 return 0;
 inOrder28(leftChild(t));
 visit(root(t));
 inOrder28(rightChile(t));
}

void postOrder28(PSeqBinTree t){
 if(t==NULL)
 return 0;
 postOrder28(leftChild(t));
 postOrder28(rightChild(t));
 visit(root(t));
}

PSeqStack pastack;
PSeqStack creatEmptyStack_seq(){
 int m;
 pastack=(PSeqStack*)malloc(sizeof(struct SeqStack));
 pastack->s=(DataType*)malloc(m*sizeof(DataType));
 pastack->maxnum=m;
 pastack->s[pastack->b]=-1;
}

int isEmptyStack(PSeqStack s){
  if(pastack->s[pastack->b]==-1)
  return 1;
  return 0;
}

DataType top(PSeqStack s){
return pastack->b;
}

void push(PSeqStack s,PSeqBinTree c){
 if(pastack->b>pastack->maxnum-1)
 printf("Overflow!\n ");
 pastack->b++;
 pastack->s[pastack->b]=c;
}

void pop(PSeqStack s){
  if(s->b==-1)
    printf("underflow\n");
  else
    s->b--;
}

void nPreOrder28(PSeqBinTree t){
  PSeqStack s;
  PSeqBinTree c;
  if(t==NULL)
  return 0;
  s=creatEmptyStack();
  push(s,t);
    while(!isEmptyStack(s)){
    c=top(s);pop(s);
    if(c!=NULL){
    visit(root(c));
    push(s,rightChild(c));
    push(s,leftChild(c));
    }
  }
}

void nInOrder28(PSeqBinTree t){
 PSeqStack s=creatEmptyStack();
 PSeqBinTree c=t;
 if(c==NULL)return 0;
  do{
     while(c!=NULL){
     push(s,c);
     c=leftChild(c)?leftChild(c):rightChild(c);
     }
    c=top(s);pop(s);visit(root(c));c=rightChild(c);
  }while(c!=NULL||!isEmptyStack(s));
}

void npostOrder28(PSeqBinTree t){
  PSeqStack s=creatEmptyStack();
  PSeqBinTree p=t;
  while(p!=NULL||!isEmptyStack(s)){
     while(p!=NULL){
     push(s,p);
     p=leftChild(p)?leftChild(p):rightChild(p);
     }
     p=top(s);pop(s);visit(root(p));
     if(!isEmptyStack&&leftChild(s)==p)
       p=rightChild(s);
     else p=NULL;
   }
}
int main(){
char nodelist[];
PSeqBinTree t;
nodelist[]={'A','B','C','^','D','E'};
for (int i = 0; i < sizeof(nodelist); i++) {
    t->nodelist[i] = nodelist[i];
    t->n++;
}
printf("递归遍历\n");
printf("\n先根");
PreOrder28(t);
printf("\n中根");
inOrder28(t);
printf("\n后根");
postOrder28(t);
printf("\n非递归遍历");
printf("\n先根");
nPreOrder28(t,t->maxnum=7);
printf("\n中根");
nInOrder28(t,t->maxnum=7);
printf("\n后根");
npostOrder28(t,t->maxnum=7);
return 0;
}



若有用,请采用原答案,代码确实有一点问题。在inOrder28函数的定义中,右子树的访问函数名错误,应该是rightChild而不是rightChile,修改该错误后即可解决too many arguments to function的问题。还有一些地方有语法错误。修改后代码如下:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

typedef char DataType;

struct SeqBinTree {
    int maxnum;
    int n;
    DataType *nodelist;
};
typedef struct SeqBinTree *PSeqBinTree;

char leftChild(PSeqBinTree t) {
    return (t->nodelist[1]);
}

char rightChild(PSeqBinTree t) {
    return (t->nodelist[2]);
}

void visit(PSeqBinTree p) {
    printf("%c", p->nodelist[0]);
}

struct SeqStack {
    int maxnum;
    int b;
    DataType *s;
};
typedef struct SeqStack *PSeqStack;

PSeqBinTree t;
PSeqStack pastack;

PSeqBinTree creatEmptyBinTree() {
    int m = 10;
    t = (PSeqBinTree)malloc(sizeof(struct SeqBinTree));
    if(t == NULL) {
        return NULL;
    }
    t->nodelist = (DataType*)malloc(m * sizeof(DataType));
    t->maxnum = m;
    t->n = 0;
    return t;
}

void PreOrder28(PSeqBinTree t) {
    if(t == NULL) {
        return;
    }
    visit(t);
    PreOrder28(leftChild(t));
    PreOrder28(rightChild(t));
}

void inOrder28(PSeqBinTree t) {
    if(t == NULL) {
        return;
    }
    inOrder28(leftChild(t));
    visit(t);
    inOrder28(rightChild(t));
}

void postOrder28(PSeqBinTree t) {
    if(t == NULL) {
        return;
    }
    postOrder28(leftChild(t));
    postOrder28(rightChild(t));
    visit(t);
}

PSeqStack creatEmptyStack() {
    int m = 10;
    pastack = (PSeqStack)malloc(sizeof(struct SeqStack));
    pastack->s = (DataType*)malloc(m * sizeof(DataType));
    pastack->maxnum = m;
    pastack->s[pastack->b] = -1;
}

int isEmptyStack(PSeqStack s) {
    if(pastack->s[pastack->b] == -1) {
        return 1;
    }
    return 0;
}

DataType top(PSeqStack s) {
    return pastack->b;
}

void push(PSeqStack s, PSeqBinTree c) {
    if(pastack->b > pastack->maxnum - 1) {
        printf("Overflow!\n");
    }
    pastack->b++;
    pastack->s[pastack->b] = *c;
}

void pop(PSeqStack s) {
    if(s->b == -1) {
        printf("underflow\n");
    } else {
        s->b--;
    }
}

void nPreOrder28(PSeqBinTree t) {
    PSeqStack s;
    PSeqBinTree c;
    if(t == NULL) {
        return;
    }
    s = creatEmptyStack();
    push(s, t);
    while(!isEmptyStack(s)) {
        c = &pastack->s[pastack->b];
        pop(s);
        if(c != NULL) {
            visit(c);
            if(leftChild(c) != '^') {
                push(s, leftChild(c));
            }
            if(rightChild(c) != '^') {
                push(s, rightChild(c));
            }
        }
    }
}

void nInOrder28(PSeqBinTree t) {
    PSeqStack s = creatEmptyStack();
    PSeqBinTree c = t;
    if(c == NULL) {
        return;
    }
    do {
        while(c != NULL) {
            push(s, c);
            c = leftChild(c) != '^' ? leftChild(c) : rightChild(c);
        }
        c = &pastack->s[pastack->b];
        pop(s);
        visit(c);
        c = rightChild(c) != '^' ? rightChild(c) : NULL;
    } while(c != NULL || !isEmptyStack(s));
}

void nPostOrder28(PSeqBinTree t) {
    PSeqStack s = creatEmptyStack();
    PSeqBinTree p = t;
    PSeqBinTree q = NULL;
    if(p == NULL) {
        return;
    }
    do {
        while(p != NULL) {
            push(s, p);
            p = leftChild(p) != '^' ? leftChild(p) : rightChild(p);
        }
        q = NULL;
        while(!isEmptyStack(s)) {
            p = &pastack->s[pastack->b];
            if(rightChild(p) == q || rightChild(p) == '^') {
                visit(p);
                pop(s);
                q = p;
            } else {
                p = rightChild(p) != '^' ? rightChild(p) : NULL;
                break;
            }
        }
        if(isEmptyStack(s)) {
            p = NULL;
        }
    } while(p != NULL || !isEmptyStack(s));
}

int main() {
    char nodelist[] = {'A', 'B', 'C', '^', 'D', 'E'};
    PSeqBinTree t = creatEmptyBinTree();
    for(int i = 0; i < sizeof(nodelist); i++) {
        t->nodelist[i] = nodelist[i];
        t->n++;
    }
    printf("递归遍历\n");
    printf("\n先根");
    PreOrder28(t);
    printf("\n中根");
    inOrder28(t);
    printf("\n后根");
    postOrder28(t);
    printf("\n非递归遍历");
    printf("\n先根");
    nPreOrder28(t);
    printf("\n中根");
    nInOrder28(t);
    printf("\n后根");
    nPostOrder28(t);
    return 0;
}


修改的内容和原因如下:

1、在creatEmptyBinTree函数中,添加了m的值赋值,避免可能出现的未初始化错误;

2、在creatEmptyStack函数中,将定义中的参数去掉,避免与push函数中的重名造成的错误;

3、在push函数中,对pastack进行解引用操作,并且将入栈的元素转换为指针类型;

4、修改了nPreOrder28、nInOrder28和nPostOrder28函数中循环中的一些细节问题,确保算法可以正确执行。

可能还有一点小BUG。请再修改。

img


直接复制你发的代码能运行但没结果,哭泣~@希望代码都能跑