二叉排序树的建立及递归与非递归遍历

题目描述

输入一系列整数,建立二叉排序树,并进行前序,中序,后序遍历。

输入描述:

输入第一行包括一个整数n(1<=n<=100)。
接下来的一行包括n个整数。

输出描述:

可能有多组测试数据,对于每组数据,将题目所给数据建立一个二叉排序树,并对二叉排序树进行前序、中序和后序遍历。
每种遍历结果输出一行。每行最后一个数据之后有一个空格。

输入中可能有重复元素,但是输出的二叉树遍历序列中重复元素不用输出。

示例1

输入

复制

5
1 6 5 9 8

输出

复制

1 6 5 9 8 
1 5 6 8 9 
5 8 9 6 1 

 

 

#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<queue>
#include<stack>
#include<iostream>
using namespace std;
typedef struct node{
	int data;
	node *lchild;
	node *rchild;
	node(){
		lchild = NULL;
		rchild = NULL;
	}
}*tree,node;
typedef struct node1
{
     node *btnode;
     bool isFirst;
}BTNode;
void insert(node* &T, int x){
	if(T==NULL){
		T = new node();
		T->data = x;
		//printf("%d\n",pre);
		return ;
		//return pre;
	}
	else if(x < T->data){
		insert(T->lchild,x);
	}
	else if(x > T->data){
		insert(T->rchild,x);
	}
	
}
//前序遍历 
void preorder(node* T){
	printf("%d ",T->data);
	if(T->lchild!=NULL){
		preorder(T->lchild);
	}
	if(T->rchild!=NULL){
		preorder(T->rchild);
	}
}
//中序遍历 
void inorder(node* T){
	if(T->lchild!=NULL){
		inorder(T->lchild);
	}
	printf("%d ",T->data);
	if(T->rchild!=NULL){
		inorder(T->rchild);
	}
}
//后序遍历 
void postorder(node* T){
	if(T->lchild!=NULL){
		postorder(T->lchild);
	}

	if(T->rchild!=NULL){
		postorder(T->rchild);
	}
	printf("%d ",T->data);
}
//非递归前序遍历
void preorder1(node *root)      
{
    stack<node*> s;
    node *p=root;
    while(p!=NULL||!s.empty())
    {
        while(p!=NULL)
        {
            cout<<p->data<<" ";
            s.push(p);
            p=p->lchild;
        }
        if(!s.empty())
        {
            p=s.top();
            s.pop();
            p=p->rchild;
        }
    }
}
//非递归中序遍历
void inorder1(node *root)      
{
    stack<node*> s;
    node *p=root;
    while(p!=NULL||!s.empty())
    {
        while(p!=NULL)
        {
            s.push(p);
            p=p->lchild;
        }
        if(!s.empty())
        {
            p=s.top();
            cout<<p->data<<" ";
            s.pop();
            p=p->rchild;
        }
    }    
}
 //非递归后序遍历
void postorder1(node *root)   
{
    stack<BTNode*> s;
    node *p=root;
    BTNode *temp;
    while(p!=NULL||!s.empty())
    {
        while(p!=NULL)              //沿左子树一直往下搜索,直至出现没有左子树的结点 
        {
            BTNode *btn=(BTNode *)malloc(sizeof(BTNode));
            btn->btnode=p;
            btn->isFirst=true;
            s.push(btn);
            p=p->lchild;
        }
        if(!s.empty())
        {
            temp=s.top();
            s.pop();
            if(temp->isFirst==true)     //表示是第一次出现在栈顶 
             {
                temp->isFirst=false;
                s.push(temp);
                p=temp->btnode->rchild;    
            }
            else                        //第二次出现在栈顶 
             {
                cout<<temp->btnode->data<<" ";
                p=NULL;
            }
        }
    }    
}
int main(){
	int n, m, i, j;
	while(scanf("%d", &n)!=EOF){
		tree T;
		T = NULL;
		for(i=0;i<n;i++){
			scanf("%d",&m);
			insert(T,m);			
		}
		preorder(T);
		printf("\n");
		preorder1(T);
		printf("\n");
		inorder(T);
		printf("\n");
		inorder1(T);
		printf("\n");
		postorder(T);
		printf("\n");
		postorder1(T);
		printf("\n");
	}
	return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值