yetang307

  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

二叉树的结构体:左右子树指针(Tree *)  值(int)

typedef struct Tree {
    char data;
    struct Tree* lchild, * rchild;
} * BiTree;

二叉树的先序创建

BiTree Create() {
    char ch;scanf_s("%c", &ch);
    BiTree T = NULL;
    if (ch == '#')T = NULL;
    else {
        T = (BiTree)malloc(sizeof(Tree));
        T->data = ch;
        T->lchild = Create();
        T->rchild = Create();
    }
    return T;
}

二叉树的中序输出

void  TLROut(BiTree T) {
    if (T) {
        TLROut(T->lchild);
        cout << T->data;
        TLROut(T->rchild);
    }
}

交换左右子树

void ChangeTree(BiTree T) {
    BiTree temp = NULL;
    if (T){
        temp = T->rchild;
        T->rchild = T->lchild;
        T->lchild = temp;
        ChangeTree(T->lchild);
        ChangeTree(T->rchild);
    }
    else return;
}

 

posted on 2022-10-24 16:11  椰糖  阅读(41)  评论(0)    收藏  举报