遍历-非递归利用队列创建二叉树

问题描述

非递归利用队列创建二叉树

BinTree create_BinTree( TElemType endTag, TElemType arr[], int n ) {

BinTNode* T = new BinTNode;

if( T == NULL ) {
    cerr<<"存储分配失败!"<<endl;
    exit( 1 );
}
InitBTree( T );
LinkQueue Q;
InitQueue( Q );
BinTNode *p = NULL;
EnQueue( Q, T );
int i = 0;
while( i < n ) {
    DeQueue( Q, p );
    if( arr[i] != endTag ) {
        p->data = arr[i];
        p->left_child  = new BinTNode;
        p->right_child = new BinTNode;
        if( p->left_child == NULL || p->right_child == NULL ) {
            cerr<<"存储分配失败!"<<endl;
            exit( 1 );
        }
        //cout << T->data <<endl;
        EnQueue( Q, p->left_child );
        EnQueue( Q, p->right_child );
    }
    else {
        p = NULL;
    }
    i++;
}
//cout << T->data << endl;
return T;

};
非递归利用队列层序创建二叉树为什么返回的结点是最后一个结点的地址?

全部代码在下边:
#include
#include
typedef char TElemType;
typedef struct node {
TElemType data;
struct node *left_child;
struct node *right_child;
}BinTNode, *BinTree;

typedef BinTNode *QElemType;

typedef struct Node {
QElemType data;
struct Node *link;
} LinkNode;

typedef struct {
LinkNode *front, *rear;
} LinkQueue;

void InitQueue( LinkQueue &Q );
bool EnQueue( LinkQueue &Q, QElemType x );
bool DeQueue( LinkQueue &Q, QElemType &x );
//QElemType DeQueue( LinkQueue &Q );
void InitBTree( BinTree T ) {
T = NULL;
};

BinTree create_BinTree( TElemType endTag, TElemType arr[], int n ) {

BinTNode* T = new BinTNode;

if( T == NULL ) {
    cerr<<"存储分配失败!"<<endl;
    exit( 1 );
}
InitBTree( T );
LinkQueue Q;
InitQueue( Q );
BinTNode *p = NULL;
EnQueue( Q, T );
int i = 0;
while( i < n ) {
    DeQueue( Q, p );
    if( arr[i] != endTag ) {
        p->data = arr[i];
        p->left_child  = new BinTNode;
        p->right_child = new BinTNode;
        if( p->left_child == NULL || p->right_child == NULL ) {
            cerr<<"存储分配失败!"<<endl;
            exit( 1 );
        }
        //cout << T->data <<endl;
        EnQueue( Q, p->left_child );
        EnQueue( Q, p->right_child );
    }
    else {
        p = NULL;
    }
    i++;
}
//cout << T->data << endl;
return T;

};

/*
*前序遍历
*/
void PreOrder( BinTNode *T ) {
if( T != NULL ) {
cout<data<
PreOrder( T->left_child );
PreOrder( T->right_child );
}
};

/*
void PreOrder_S( BinTNode *T ) {
SeqStack S;
InitStack( S );
BinTNode *p = T;
Push( S, NULL );
while( p != NULL ) {
cout<< p->data <
if( p->right_child != NULL )
Push( S, p->right_child );
if( p->left_child != NULL )
p = p->left_child;
else
Pop( S, p );
}
};
*/

/*
*中序遍历
*/
void InOrder( BinTNode *T ) {
if( T!= NULL ) {
InOrder( T->left_child );
cout<< T->data << endl;
InOrder( T->right_child );
}
};

void InitQueue( LinkQueue &Q ) {

//书上的
/*
Q = new LinkNode;
if( Q == NULL ) {
    cerr<<"存储分配失败"<<endl;
    exit(1);
}
Q.front = Q.rear = Q;
*/

//我的
Q.front = Q.rear = new LinkNode;
if( !Q.front ) {
    cerr<<"存储分配失败"<<endl;
    exit(1);
}
Q.front->link = NULL;

}

bool EnQueue( LinkQueue &Q, QElemType x ) {
Q.rear->link = new LinkNode;
if( Q.rear->link == NULL ) {
cerr<<"存储分配失败"<
exit(1);
}
Q.rear = Q.rear->link;
Q.rear->data = x;
Q.rear->link = NULL;

return true;

}

bool DeQueue( LinkQueue &Q, QElemType &x ) {
if( Q.front->link == NULL )
return false;
LinkNode p = Q.front->link;
x = p->data;
Q.front->link = p->link;
delete p;
return true;
}
/

QElemType DeQueue( LinkQueue &Q ) {
QElemType x;
if( Q.front->link == NULL )
return NULL;
LinkNode *p = Q.front->link;
x = p->data;
Q.front->link = p->link;
delete p;
return x;
}
*/
bool QueueEmpty( LinkQueue &Q ) {
return Q.front->link == NULL;
}

void LevelOrder( BinTree BT ) {
LinkQueue Q;
InitQueue( Q );
BinTNode *p = BT;
EnQueue( Q , p );
while( !QueueEmpty ( Q ) ) {
DeQueue( Q, p );
cout << p->data << endl;
if( p->left_child != NULL ) EnQueue( Q, p->left_child );
if( p->right_child != NULL ) EnQueue( Q, p->right_child);
}
};
int main() {
TElemType endTag = '#';
//BinTNode T;
//BinTree root = &T;
//InitBTree( root );

TElemType arr[15] = { '1', '2', '3', '4', '5', '6', '7', '#', '#', '#', '#', '#', '#', '#', '#' };
//TElemType arr[7] = { '1', '2', '4', '5', '3', '6', '7' };
BinTree root = create_BinTree( endTag, arr, 15 );
cout << "层序遍历二叉树:"<<endl;
LevelOrder( root );

}

时间: 2024-09-20 00:54:40

遍历-非递归利用队列创建二叉树的相关文章

求大神告诉我我的二叉树后序遍历非递归哪里错了

问题描述 求大神告诉我我的二叉树后序遍历非递归哪里错了 #include using namespace std; struct binode { int data; binode *lchild,*rchild; }; binode *Q[100],*S[100]; struct element { binode *ptr; int flag; }; class bitree { public: void create_bitree(){root=creat(root);} void dele

二叉搜索树与树的遍历非递归练习

复习了二叉搜索树的实现,包括插入.查找和删除操作,顺便做了下二叉树的三种遍历操作.全部操作采用非递归方式.   #include<iostream>   #include<stack>   using namespace std;     typedef int T;   // 值类型      // 节点定义    struct node {       T val;       node *left,*right;       node(const T& val):va

中序遍历二叉树-二叉树的非递归操作。。

问题描述 二叉树的非递归操作.. 如何用栈实现二叉树的非递归操作,越详细越好,谢谢各位啦.一定要详细哦 解决方案 void inOrder2(BinTree *root) //非递归中序遍历 { stack<BinTree*> s; BinTree *p=root; while(p!=NULL||!s.empty()) { while(p!=NULL) { s.push(p); p=p->lchild; } if(!s.empty()) { p=s.top(); cout<<

深入遍历二叉树的各种操作详解(非递归遍历)_C 语言

先使用先序的方法建立一棵二叉树,然后分别使用递归与非递归的方法实现前序.中序.后序遍历二叉树,并使用了两种方法来进行层次遍历二叉树,一种方法就是使用STL中的queue,另外一种方法就是定义了一个数组队列,分别使用了front和rear两个数组的下标来表示入队与出队,还有两个操作就是求二叉树的深度.结点数... 复制代码 代码如下: #include<iostream>#include<queue>#include<stack>using namespace std;/

先序遍历二叉树的递归实现与非递归实现深入解析

以下是对先序遍历二叉树的递归实现与非递归实现进行了详细的分析介绍,需要的朋友可以过来参考下   1.先序遍历二叉树  递归实现思想:若二叉树为空,返回.否则 1)遍历根节点: 2)先序遍历左子树: 3)先序遍历右子树: 代码: 复制代码 代码如下: template<typename elemType> void PreOrder(nodeType<elemType> *root)  {      if(root==NULL)          return ;      visi

先序遍历二叉树的递归实现与非递归实现深入解析_C 语言

1.先序遍历二叉树  递归实现思想:若二叉树为空,返回.否则 1)遍历根节点:2)先序遍历左子树:3)先序遍历右子树: 代码: 复制代码 代码如下: template<typename elemType> void PreOrder(nodeType<elemType> *root)  {      if(root==NULL)          return ;      visit(root->data); // visit the data    PreOrder(ro

迷宫求解非递归 DFS BFS(应用栈和队列)

栈和队列的应用对迷宫问题求解 没有递归 自己手动建的栈和队 并且输出路径 DFS的路径就是 栈中的坐标 BFS的路径在队又开了一个域存上一层的base值 语言还是用的C++ 感觉比C的封装性好很多 充分体会了一下DFS一边比BFS快 但是BFS是最优解而DFS可能不是最优解   #include <iostream> #include<cstdio> #include<cstring> #include<algorithm> using namespace

创建二叉树 二叉树如何删除节点操作教程_C 语言

复制代码 代码如下: // 二叉树.cpp : 定义控制台应用程序的入口点. // /* *二叉树作业 *2012.12.1 13:55 *Made By Karld Vorn Doenitz */ #include "stdafx.h" #include<iostream> #include<string> using namespace std; class TreeNode{//建立节点类 public: char num; TreeNode *leftc

C++非递归队列实现二叉树的广度优先遍历_C 语言

本文实例讲述了C++非递归队列实现二叉树的广度优先遍历.分享给大家供大家参考.具体如下: 广度优先非递归二叉树遍历(或者说层次遍历): void widthFirstTraverse(TNode* root) { queue<TNode*> q; // 队列 q.enqueue(root); TNode* p; while(q.hasElement()) { p = q.dequeue(); // 队首元素出队列 visit(p); // 访问p结点 if(p->left) q.enqu