设为首页 加入收藏

TOP

遍历二叉树的各种操作(非递归遍历)(一)
2015-02-02 14:15:40 来源: 作者: 【 】 浏览:33
Tags:各种 操作

先使用先序的方法建立一棵二叉树,然后分别使用递归与非递归的方法实现前序、中序、后序遍历二叉树,并使用了两种方法来进行层次遍历二叉树,一种方法就是使用STL中的queue,另外一种方法就是定义了一个数组队列,分别使用了front和rear两个数组的下标来表示入队与出队,还有两个操作就是求二叉树的深度、结点数。


#include?
#include?
#include?
using namespace std;?
?
//二叉树结点的描述?
typedef struct BiTNode
{?
? ? char data;?
? ? struct BiTNode *lchild, *rchild;? ? ? //左右孩子?
}BiTNode,*BiTree;?
?
//按先序遍历创建二叉树?
//BiTree *CreateBiTree()? ? //返回结点指针类型?
//void CreateBiTree(BiTree &root)? ? ? //引用类型的参数?
void CreateBiTree(BiTNode **root)? ? //二级指针作为函数参数?
{?
? ? char ch; //要插入的数据?
? ? scanf("\n%c", &ch);
? ? //cin>>ch;?
? ? if(ch=='#')
? ? ? ? *root = NULL;
? ? else
? ? {
? ? ? ? *root = (BiTNode *)malloc(sizeof(BiTNode));
? ? ? ? (*root)->data = ch;
? ? ? ? printf("请输入%c的左孩子:",ch);
? ? ? ? CreateBiTree(&((*root)->lchild));
? ? ? ? printf("请输入%c的右孩子:",ch);
? ? ? ? CreateBiTree(&((*root)->rchild));
? ? }
}
?
//前序遍历的算法程序?
void PreOrder(BiTNode *root)
{?
? ? if(root==NULL)?
? ? ? ? return ;?
? ? printf("%c ", root->data); //输出数据?
? ? PreOrder(root->lchild); //递归调用,前序遍历左子树?
? ? PreOrder(root->rchild); //递归调用,前序遍历右子树?
}?
?
//中序遍历的算法程序?
void InOrder(BiTNode *root)?
{?
? ? if(root==NULL)
? ? ? ? return ;
? ? InOrder(root->lchild); //递归调用,前序遍历左子树?
? ? printf("%c ", root->data); //输出数据?
? ? InOrder(root->rchild); //递归调用,前序遍历右子树?
}?
?
//后序遍历的算法程序?
void PostOrder(BiTNode *root)
{
? ? if(root==NULL)
? ? ? ? return ;
? ? PostOrder(root->lchild);? ? ? //递归调用,前序遍历左子树?
? ? PostOrder(root->rchild);? ? ? //递归调用,前序遍历右子树?
? ? printf("%c ", root->data);? ? //输出数据? ?
}?
?
/*
二叉树的非递归前序遍历,前序遍历思想:先让根进栈,只要栈不为空,就可以做弹出操作,
每次弹出一个结点,记得把它的左右结点都进栈,记得右子树先进栈,这样可以保证右子树在栈中总处于左子树的下面。
*/?
void PreOrder_Nonrecursive(BiTree T)? ? //先序遍历的非递归? ?
{?
? ? if(!T)? ?
? ? ? ? return ;? ?
? ?
? ? stack s;?
? ? s.push(T);?
?
? ? while(!s.empty())?
? ? {?
? ? ? ? BiTree temp = s.top();?
? ? ? ? cout<data<<" ";?
? ? ? ? s.pop();?
? ? ? ? if(temp->rchild)?
? ? ? ? ? ? s.push(temp->rchild);?
? ? ? ? if(temp->lchild)?
? ? ? ? ? ? s.push(temp->lchild);?
? ? }?
}?


void PreOrder_Nonrecursive1(BiTree T)? ? //先序遍历的非递归
{
?if(!T)?
? ? ? ? return ;
?stack s;
?BiTree curr = T;
?while(curr != NULL || !s.empty())
?{
? while(curr != NULL)
? {
? ?cout<data<<"? ";
? ?s.push(curr);
? ?curr = curr->lchild;
? }
? if(!s.empty())
? {
? ?curr = s.top();
? ?s.pop();
? ?curr = curr->rchild;
? }
?}
}


void PreOrder_Nonrecursive2(BiTree T)? ? //先序遍历的非递归?
{?
? ? if(!T)
? ? ? ? return ;?
?
? ? stack s;?
? ? while(T)? ? ? ? ? // 左子树上的节点全部压入到栈中?
? ? {?
? ? ? ? s.push(T);?
? ? ? ? cout<data<<"? ";?
? ? ? ? T = T->lchild;?
? ? }?
? ? ?
? ? while(!s.empty())?
? ? {? ? ? ? ?
? ? ? ? BiTree temp = s.top()->rchild;? // 栈顶元素的右子树?
? ? ? ? s.pop();? ? ? ? ? ? ? ? ? ? ? ? // 弹出栈顶元素?
? ? ? ? while(temp)? ? ? ? ? // 栈顶元素存在右子树,则对右子树同样遍历到最下方?
? ? ? ? {?
? ? ? ? ? ? cout<data<<"? ";?
? ? ? ? ? ? s.push(temp);?
? ? ? ? ? ? temp = temp->lchild;?
? ? ? ? }?
? ? }?
}?


void InOrderTraverse1(BiTree T)? // 中序遍历的非递归?
{?
? ? if(!T)?
? ? ? ? return ;?
? ? BiTree curr = T;? ? // 指向当前要检查的节点?
? ? stack s;
?while(curr != NULL || !s.empty())
?{
? while(curr != NULL)
? {
? ?s.push(curr);
? ?curr = curr->lchild;
? }//while
? if(!s.empty())
? {
? ?curr = s.top();
? ?s.pop();
? ?cout<data<<"? ";
? ?curr = curr->rchild;
? }
?}
}


void InOrderTraverse(BiTree T)? // 中序遍历的非递归?
{?
? ? if(!T)?
? ? ? ? return ;?
? ? stack s;?
? ? BiTree curr = T->lchild;? ? // 指向当前要检查的节点?
? ? s.push(T);?
? ? while(curr != NULL || !s.empty())?
? ? {?
? ? ? ? while(curr != NULL)? ? // 一直向左走?
? ? ? ? {?
? ? ? ? ? ? s.push(curr);?
? ? ? ? ? ? curr = curr->lchild;?
? ? ? ? }?
? ? ? ? curr = s.top();?
? ? ? ? s.pop();?
? ? ? ? cout<data<<"? ";?
? ? ? ? curr = curr->rchild;?
? ? }?
}?


void PostOrder_Nonrecursive1(BiTree T)? // 后序遍历的非递归? ?
{? ?
? ? stack S;? ?
? ? BiTree curr = T ;? ? ? ? ? /

首页 上一页 1 2 3 4 下一页 尾页 1/4/4
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇二叉树:后序遍历,递归和非递归.. 下一篇求二叉树深度,递归和非递归

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容: