二叉树的基本操作,代码有问题

baidu_36692676 2016-11-13 12:35:42
#ifndef BITREE_H_H
#include <stdio.h>
#include <cstdlib>
#include <iostream>
#include <stack>
#include "btree.h"

#define OK 1
#define ERROR 0
typedef char ElemType;
typedef struct BiLNode
{
ElemType data;
struct BiLNode *lchild;
struct BiLNode *rchild;
}BiTree;
int init_BiTree(BiTree*);
int make_node(BiLNode**);
int create_BiTree(BiTree**);
int preOrderTraverse_BiTree(BiTree*);
int inOrderTraverse_BiTree(BiTree*);
int postOrderTraverse_BiTree(BiTree*);
int levelOrderTraverse_BiTree(BiTree*);
int destroy_BiTree(BiTree**);
int empty_BiTree(BiTree*);
int depth_BiTree(BiTree*);
int getValue_BiTree(BiTree*, ElemType*);
int assign_BiTree(BiTree*, ElemType);
int parent_BiTree(BiTree*, BiLNode**, BiLNode*);
int leftChild_BiTree(BiTree*, BiLNode**, BiLNode*);
int rightChild_BiTree(BiTree*, BiLNode**, BiLNode*);
int leftSibling_BiTree(BiTree*, BiLNode**, BiLNode*);
int rightSibling_BiTree(BiTree*, BiLNode**, BiLNode*);
int insertChild_BiTree(BiTree*, BiTree*, BiLNode*, int);
int deleteChild_BiTree(BiTree*, BiLNode*, int);
int init_BiTree(BiTree *btree)
{
btree = NULL;
return OK;
}

//后续遍历销毁二叉表
int destroy_BiTree(BiTree **btree)
{
if((*btree) == NULL)
return ERROR;
else
{
destroy_BiTree(&(*btree)->lchild);
destroy_BiTree(&(*btree)->rchild);
free(*btree);
(*btree) = NULL;
}
return OK;
}

int make_node(BiLNode **node)
{
if(!((*node) = (BiLNode*)malloc(sizeof(BiLNode))))
return ERROR;
return OK;
}

int create_BiTree(BiTree **btree)
{
char ch;
scanf("%c",&ch);
if(ch == '#')
{
(*btree) = NULL;
}
else
{
if(!make_node(btree))
return ERROR;
(*btree)->data = ch;
create_BiTree(&(*btree)->lchild);
create_BiTree(&(*btree)->rchild);
}
return OK;
}

int empty_BiTree(BiTree *btree)
{
if(btree == NULL)
return true;
return false;
}

//求二叉树深度
int depth_BiTree(BiTree *btree)
{
int dep = 0;
int dep1 = 0;
if(btree == NULL)
return 0;
else
{
dep = depth_BiTree(btree->lchild) ;
dep1 = depth_BiTree(btree->rchild);
return dep > dep1 ? dep + 1 : dep1 + 1;
}
return OK;
}

int getValue_BiTree(BiTree *btree, ElemType *e)
{
if(btree == NULL)
return ERROR;
*e = btree->data;
return OK;
}

int assign_BiTree(BiTree *btree, ElemType e)
{
if(btree == NULL)
return ERROR;
btree->data = e;
return OK;
}

//返回e结点的双亲
int parent_BiTree(BiTree *btree, BiLNode **parent, BiLNode *e)
{
BiLNode *queue[30], *tmp;
int front, rear;
front = rear = 0;
if(btree)
{
queue[rear++] = btree;
while(front != rear)
{
tmp = queue[front++];
if(tmp ->lchild == e || tmp->rchild == e)
{
(*parent) = tmp;
break;
}
if(tmp->lchild)
queue[rear++] = tmp->lchild;
if(tmp->rchild)
queue[rear++] = tmp->rchild;
}
}
return OK;
}

//返回e结点的左孩子
int leftChild_BiTree(BiTree *btree, BiLNode **lchild, BiLNode *e)
{
BiLNode *queue[30], *tmp;
int front, rear;
front = rear = 0;
if(btree)
{
queue[rear++] = btree;
while(front != rear)
{
tmp = queue[front++];
if(tmp == e)
{
(*lchild) = tmp->lchild;
break;
}
if(tmp->lchild)
queue[rear++] = tmp->lchild;
if(tmp->rchild)
queue[rear++] = tmp->rchild;
}
}
return OK;
}

//返回e结点的右孩子
int rightChild_BiTree(BiTree *btree, BiLNode **rchild, BiLNode *e)
{
BiLNode *queue[30], *tmp;
int front, rear;
front = rear = 0;
if(btree)
{
queue[rear++] = btree;
while(front != rear)
{
tmp = queue[front++];
if(tmp == e)
{
(*rchild) = tmp->rchild;
break;
}
if(tmp->lchild)
queue[rear++] = tmp->lchild;
if(tmp->rchild)
queue[rear++] = tmp->rchild;
}
}
return OK;
}

//返回结点左兄弟
int leftSibling_BiTree(BiTree *btree, BiLNode **lsib, BiLNode *e)
{
BiLNode *queue[30], *tmp;
int front, rear;
front = rear = 0;
if(btree)
{
queue[rear++] = btree;
while(front != rear)
{
tmp = queue[front++];
if(tmp ->lchild == e || tmp->rchild == e)
{
if(tmp->lchild == e)
(*lsib) = NULL;
else
(*lsib) = tmp->lchild;
break;
}
if(tmp->lchild)
queue[rear++] = tmp->lchild;
if(tmp->rchild)
queue[rear++] = tmp->rchild;
}
}
return OK;
}

//返回结点右兄弟
int rightSibling_BiTree(BiTree *btree, BiLNode **rsib, BiLNode *e)
{
BiLNode *queue[30], *tmp;
int front, rear;
front = rear = 0;
if(btree)
{
queue[rear++] = btree;
while(front != rear)
{
tmp = queue[front++];
if(tmp ->lchild == e || tmp->rchild == e)
{
if(tmp->rchild == e)
(*rsib) = NULL;
else
(*rsib) = tmp->rchild;
break;
}
if(tmp->lchild)
queue[rear++] = tmp->lchild;
if(tmp->rchild)
queue[rear++] = tmp->rchild;
}
}
return OK;
}

//在e的位置插入c到二叉树
//条件: c的右子树必为空,flag是在左还是右插入,0 左 1 右
int insertChild_BiTree(BiTree *btree, BiTree *c, BiLNode *e, int flag)
{
if(c->rchild != NULL)
return ERROR;
BiLNode *queue[30], *tmp, *tmp1;
int front, rear;
front = rear = 0;
if(btree)
{
queue[rear++] = btree;
while(front != rear)
{
tmp = queue[front++];
if(tmp == e)
{
//在左边插入树
if(flag == 0)
{
tmp1 = tmp->lchild;
tmp->lchild = c;
c->rchild = tmp1;
}
else if(flag == 1)
{
//在右边插入树
tmp1 = tmp->rchild;
tmp->rchild = c;
c->rchild = tmp1;
}
else
{
return ERROR;
}
break;
}
if(tmp->lchild)
queue[rear++] = tmp->lchild;
if(tmp->rchild)
queue[rear++] = tmp->rchild;
}
}
return OK;
}

int deleteChild_BiTree(BiTree *btree, BiLNode *e, int flag)
{
BiLNode *queue[30], *tmp;
int front, rear;
front = rear = 0;
if(btree)
{
queue[rear++] = btree;
while(front != rear)
{
tmp = queue[front++];
if(tmp == e)
{
if(flag == 0)
{
destroy_BiTree(&tmp->lchild);
}
else if(flag == 1)
{
destroy_BiTree(&tmp->rchild);
}
else
{
return ERROR;
}
break;
}
if(tmp->lchild)
queue[rear++] = tmp->lchild;
if(tmp->rchild)
queue[rear++] = tmp->rchild;
}
}
return OK;
}

//先序递归遍历二叉树
int preOrderTraverse_BiTree(BiTree *btree)
{
if(btree == NULL)
return ERROR;
else
{
printf("%c ", btree->data);
preOrderTraverse_BiTree(btree->lchild);
preOrderTraverse_BiTree(btree->rchild);
}
return OK;
}

//中序递归遍历二叉树
int inOrderTraverse_BiTree(BiTree *btree)
{
if(btree == NULL)
return ERROR;
else
{
inOrderTraverse_BiTree(btree->lchild);
printf("%c ", btree->data);
inOrderTraverse_BiTree(btree->rchild);
}
return OK;
}

//后序递归遍历二叉树
int postOrderTraverse_BiTree(BiTree *btree)
{
if(btree == NULL)
return ERROR;
else
{
postOrderTraverse_BiTree(btree->lchild);
postOrderTraverse_BiTree(btree->rchild);
printf("%c ", btree->data);
}
return OK;
}

//层次递归遍历二叉树(利用队列实现)
int levelOrderTraverse_BiTree(BiTree *btree)
{
BiLNode *queue[30], *tmp;
int front, rear;
front = rear = 0;
if(btree)
{
queue[rear++] = btree;
while(front != rear)
{
tmp = queue[front++];
printf("%c ", tmp->data);
if(tmp->lchild)
queue[rear++] = tmp->lchild;
if(tmp->rchild)
queue[rear++] = tmp->rchild;
}
}
return OK;
}
...全文
734 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
shiter 2016-11-14
  • 打赏
  • 举报
回复
不是算法有问题了,你工程构建的问题
赵4老师 2016-11-14
  • 打赏
  • 举报
回复
仅供参考:
#include <iostream>
#include <stack>
#include <queue>
#include <locale.h>
using namespace std;
typedef struct BiTNode {//二叉树结点
    char data;                      //数据
    struct BiTNode *lchild,*rchild; //左右孩子指针
} BiTNode,*BiTree;
int CreateBiTree(BiTree &T) {//按先序序列创建二叉树
    char data;
    scanf("%c",&data);//按先序次序输入二叉树中结点的值(一个字符),‘#’表示空树
    if (data == '#') {
        T = NULL;
    } else {
        T = (BiTree)malloc(sizeof(BiTNode));
        T->data = data;         //生成根结点
        CreateBiTree(T->lchild);//构造左子树
        CreateBiTree(T->rchild);//构造右子树
    }
    return 0;
}
void Visit(BiTree T) {//输出
    if (T->data != '#') {
        printf("%c ",T->data);
    }
}
void PreOrder(BiTree T) {//先序遍历
    if (T != NULL) {
        Visit(T);               //访问根节点
        PreOrder(T->lchild);    //访问左子结点
        PreOrder(T->rchild);    //访问右子结点
    }
}
void InOrder(BiTree T) {//中序遍历
    if (T != NULL) {
        InOrder(T->lchild);     //访问左子结点
        Visit(T);               //访问根节点
        InOrder(T->rchild);     //访问右子结点
    }
}
void PostOrder(BiTree T) {//后序遍历
    if (T != NULL) {
        PostOrder(T->lchild);   //访问左子结点
        PostOrder(T->rchild);   //访问右子结点
        Visit(T);               //访问根节点
    }
}
void PreOrder2(BiTree T) {//先序遍历(非递归)
//访问T->data后,将T入栈,遍历左子树;遍历完左子树返回时,栈顶元素应为T,出栈,再先序遍历T的右子树。
    stack<BiTree> stack;
    BiTree p = T;//p是遍历指针
    while (p || !stack.empty()) {   //栈不空或者p不空时循环
        if (p != NULL) {
            stack.push(p);          //存入栈中
            printf("%c ",p->data);  //访问根节点
            p = p->lchild;          //遍历左子树
        } else {
            p = stack.top();        //退栈
            stack.pop();
            p = p->rchild;          //访问右子树
        }
    }
}
void InOrder2(BiTree T) {//中序遍历(非递归)
//T是要遍历树的根指针,中序遍历要求在遍历完左子树后,访问根,再遍历右子树。
//先将T入栈,遍历左子树;遍历完左子树返回时,栈顶元素应为T,出栈,访问T->data,再中序遍历T的右子树。
    stack<BiTree> stack;
    BiTree p = T;//p是遍历指针
    while (p || !stack.empty()) {   //栈不空或者p不空时循环
        if (p != NULL) {
            stack.push(p);          //存入栈中
            p = p->lchild;          //遍历左子树
        } else {
            p = stack.top();        //退栈,访问根节点
            printf("%c ",p->data);
            stack.pop();
            p = p->rchild;          //访问右子树
        }
    }
}

typedef struct BiTNodePost{
    BiTree biTree;
    char tag;
} BiTNodePost,*BiTreePost;
void PostOrder2(BiTree T) {//后序遍历(非递归)
    stack<BiTreePost> stack;
    BiTree p = T;//p是遍历指针
    BiTreePost BT;
    while (p != NULL || !stack.empty()) {//栈不空或者p不空时循环
        while (p != NULL) {//遍历左子树
            BT = (BiTreePost)malloc(sizeof(BiTNodePost));
            BT->biTree = p;
            BT->tag = 'L';//访问过左子树
            stack.push(BT);
            p = p->lchild;
        }
        while (!stack.empty() && (stack.top())->tag == 'R') {//左右子树访问完毕访问根节点
            BT = stack.top();
            stack.pop();//退栈
            printf("%c ",BT->biTree->data);
        }
        if (!stack.empty()) {//遍历右子树
            BT = stack.top();
            BT->tag = 'R';//访问过右子树
            p = BT->biTree;
            p = p->rchild;
        }
    }
}

void LevelOrder(BiTree T) {//层次遍历
    if (T == NULL) return;
    BiTree p = T;
    queue<BiTree> queue;//队列
    queue.push(p);//根节点入队
    while (!queue.empty()) {    //队列不空循环
        p = queue.front();      //对头元素出队
        printf("%c ",p->data);  //访问p指向的结点
        queue.pop();            //退出队列
        if (p->lchild != NULL) {//左子树不空,将左子树入队
            queue.push(p->lchild);
        }
        if (p->rchild != NULL) {//右子树不空,将右子树入队
            queue.push(p->rchild);
        }
    }
}
int main() {
    BiTree T;

    setlocale(LC_ALL,"chs");
    CreateBiTree(T);

    printf("先序遍历        :");PreOrder  (T);printf("\n");
    printf("先序遍历(非递归):");PreOrder2 (T);printf("\n");
                                               printf("\n");
    printf("中序遍历        :");InOrder   (T);printf("\n");
    printf("中序遍历(非递归):");InOrder2  (T);printf("\n");
                                               printf("\n");
    printf("后序遍历        :");PostOrder (T);printf("\n");
    printf("后序遍历(非递归):");PostOrder2(T);printf("\n");
                                               printf("\n");
    printf("层次遍历        :");LevelOrder(T);printf("\n");

    return 0;
}
//ABC##DE#G##F###
//先序遍历        :A B C D E G F
//先序遍历(非递归):A B C D E G F
//
//中序遍历        :C B E G D F A
//中序遍历(非递归):C B E G D F A
//
//后序遍历        :C G E F D B A
//后序遍历(非递归):C G E F D B A
//
//层次遍历        :A B C D E F G
//

///       A
///      /
///     B
///    / \
///   C   D
///      / \
///     E   F
///      \
///       G
小灸舞 2016-11-14
  • 打赏
  • 举报
回复
1.注意当前路径可能是工程所在的目录
2.使用绝对路径试试
3.右键项目--属性--配置属性--C/C++--附加包含目录里添加上btree.h所在的路径
PS:这里以VS为例
baidu_36692676 2016-11-13
  • 打赏
  • 举报
回复
一直显示fatal error: btree.h: No such file or directory|
paschen 2016-11-13
  • 打赏
  • 举报
回复
引用 1 楼 baidu_36692676 的回复:
一直显示fatal error: btree.h: No such file or directory|
提示说的很明白了,找不到这个文件,这个文件是否在工程目录,再不行包含时使用绝对路径

5,530

社区成员

发帖
与我相关
我的任务
社区描述
C/C++ 模式及实现
社区管理员
  • 模式及实现社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧