二叉排序树建立及前中后序遍历

题目描述

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

输入描述:

输入第一行包括一个整数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 <iostream>
using namespace std;
struct NODE{
	int data;
	NODE *l;
	NODE *r;
};
bool creat(NODE * &root,int a){
	if(root==NULL){
		root=new NODE;
	    root->data=a;
	    root->l=NULL;
	    root->r=NULL;
	    return true;
	}
	if(a>root->data){
		return creat(root->r,a);
	}
	else{
		return creat(root->l,a);
	}
}
void preorder(NODE *root){
	cout<<root->data<<" ";
	if(root->l!=NULL){
		preorder(root->l);
	}
    if(root->r!=NULL){
    	preorder(root->r); 
	}
}
void midorder(NODE *root){
    if(root->l!=NULL){
		midorder(root->l);
	}
	cout<<root->data<<" ";
	if(root->r!=NULL){
		midorder(root->r);
	}
}
void postorder(NODE *root){
	if(root->l!=NULL){
		postorder(root->l);
	}
	if(root->r!=NULL){
		postorder(root->r);
	}
	cout<<root->data<<" ";
}
int main(){
	int n;
	while(cin>>n){
		int num[102];
		NODE *root=NULL;
		for(int i=0;i<n;i++){
			cin>>num[i];
		}
		bool temp[102];
		for(int i=0;i<n;i++){
			temp[i]=false;
		}
		for(int i=0;i<n;i++){
			for(int j=i+1;j<n;j++){
				if(num[j]==num[i]){
					temp[j]=true;
				}
			}
		}
		int k=0;
		int num1[102];
		for(int i=0;i<n;i++){
			if(!temp[i]){
				num1[k]=num[i];
				k++;
			}
		}
		NODE *roo=new NODE;
		roo->data=num1[0];
		roo->l=NULL;
		roo->r=NULL;
		for(int i=1;i<k;i++){
			creat(roo,num1[i]);
		}
		preorder(roo);
		cout<<endl;
		midorder(roo);
		cout<<endl;
		postorder(roo);
		cout<<endl;
	}
	return 0;
}

 

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是实现二叉排序树的代码: #include <stdio.h> #include <stdlib.h> struct Node { int val; struct Node *left; struct Node *right; }; struct Node *insert(struct Node *T, int x) { if (T == NULL) { T = (struct Node *)malloc(sizeof(struct Node)); T->val = x; T->left = NULL; T->right = NULL; } else if (x < T->val) { T->left = insert(T->left, x); } else if (x > T->val) { T->right = insert(T->right, x); } return T; } void postorder(struct Node *T) { if (T == NULL) { return; } postorder(T->left); postorder(T->right); printf("%d ", T->val); } int main() { struct Node *T = NULL; char cmd[10]; int x; while (scanf("%s", cmd) != EOF) { if (cmd[0] == 'i') { scanf("%d", &x); T = insert(T, x); } else if (cmd[0] == 'p') { postorder(T); printf("\n"); } } return 0; } 在这个程序,我们定义了一个结构体 Node,表示二叉排序树的节点。每个节点包含一个整数值 val,以及指向左右子树的指针 left 和 right。 我们还定义了两个函数 insert 和 postorder,分别用于插入节点和后序遍历二叉排序树。 在主函数,我们不断读入命令,如果是 insert 命令,则读入一个整数 x 并插入到二叉排序树;如果是 postorder 命令,则进行后序遍历并输出结果。 注意,我们使用了递归的方式实现插入和后序遍历。在插入节点时,如果当前节点为空,则创建一个新节点并返回;否则,根据节点值的大小递归地插入到左子树或右子树。在后序遍历时,先递归遍历左子树,再递归遍历右子树,最后输出当前节点的值。 这个程序可以通过以下命令编译运行: gcc -o bst bst.c ./bst 然后输入一系列命令,比如: insert 5 insert 3 insert 7 postorder 输出结果应该为: 3 7 5 这表示后序遍历的结果为 3、7、5。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值