查找:二叉排序树(习题-2、3、4)

第1关:二叉排序树判别(算法设计题2)

#include<iostream>
#include <string.h>
using namespace std;

typedef struct BiTNode
{
   char data; 
   struct BiTNode *lchild,*rchild;
}BiTNode,*BiTree;
void CreateBiTree(BiTree &T,char a[],int &i)
{//先序建立二叉树
	/***********************Begin**********************/
    if(a[i] == '\0') return ;

	if(a[i] == '#')  T = NULL;			
	else
	{							
		T = new BiTNode;
		T->data = a[i];					
		CreateBiTree(T->lchild,a,++ i);	
		CreateBiTree(T->rchild,a,++ i);
	}				
	/*********************** End **********************/
}
BiTree pre=NULL;									//前驱指针
void JudgeBST(BiTree T,int &flag)
{//判断二叉树T是否是二叉排序树,flag初值为1
    if(!T) return ;
	if(T!=NULL&&flag)
	{ 
		/***********************Begin**********************/
		JudgeBST(T -> lchild,flag); // 中序遍历左子树
		if (pre == NULL) pre = T; // 中序遍历的第一个结点不必判断
		else if (pre -> data < T -> data) pre = T; // 前驱指针指向当前结点
		else flag = false;
		/*********************** End **********************/
	}
 
    JudgeBST(T->rchild,flag);
}

int main()
{
	char a[99];
	//输入先序序列
	cin>>a;
	if(strcmp(a,"#")!=0){
		int i=-1;
		int flag=1;
		BiTree T;
		CreateBiTree(T,a,++i);
		JudgeBST(T,flag);
		if(flag)
			cout<<"YES"<<endl;
		else
			cout<<"NO"<<endl;
	}
	return 0;
}

第2关:不小于x的所有数据(算法设计题3)

#include<iostream>
using namespace std;
typedef struct BSTNode
{//二叉排序树的二叉链表存储表示
	int data;
	struct BSTNode *lchild,*rchild;
}BSTNode,*BSTree;
void InsertBST(BSTree &T,int e){
	/***********************Begin**********************/
	if(!T)
    {
        BSTNode *S = new BSTNode;
        S -> data = e;
        S -> lchild = S -> rchild = NULL;
        T = S; 
    }
    else if(e < T -> data) InsertBST(T -> lchild,e);
    else if(e > T -> data) InsertBST(T -> rchild,e);
	/*********************** End **********************/
}
void Print(BSTree T)
{//中序输出以T为根的二叉排序树的所有结点
	/***********************Begin**********************/
	if(T)
    {
        Print(T -> lchild);
        cout << T -> data << ' ';
        Print(T -> rchild);
    }
	/*********************** End **********************/	
}
void PrintAllx(BSTree T,int x)
{//在二叉排序树T中,查找值≥x的结点并输出
	/***********************Begin**********************/
	BSTree p = T;
    if(p)
    {
        while(p && p -> data < x) p = p -> rchild;
        T = p;
 
        while(p && p -> data >= x)
        {
            BSTree f = p;
            p = p -> lchild;
            if(p) f -> lchild = NULL;
            Print(T);
        } 
    }
	/*********************** End **********************/
}
int main()
{
	BSTree T=NULL;
	int n;
	cin>>n;
	for(int i=0;i<n;i++)
	{
		int e;
		cin>>e;
		InsertBST(T,e);
	}
	Print(T);
	cout<<endl;
	int x;
	cin>>x;
	PrintAllx(T,x);
}

第3关:二叉排序树和查找(算法设计题4)

#include<iostream>
using namespace std;
typedef struct BiTNode
{
	int data;
	int count;
	struct BiTNode *lchild,*rchild;
}BiTNode,*BiTree;
void SearchBST(BiTree &T,int X)
{
	/***********************Begin**********************/
	BiTree s,q,f;// 以数据值 target, 新建结点 s

    s = new BiTNode; 
	s->data = X; 
	s->count = 1; 
	s->lchild = s->rchild = NULL; 
	if(!T){ 
		T=s; 
		return ; 
	} //如果该树为空则跳出该函数
	f=NULL; 
	q=T; 
	while (q){ 
		if (q -> data == X){ 
			q -> count ++; 
            //cout << q -> data << ' ' << q -> count << '*' << endl;
			return ; 
		} // 如果找到该值则计数加一
		f=q; 
		if (q ->data > X) // 如果查找值比目标值大,则为该树左孩子
			q=q->lchild; 
		else // 否则为右孩子
			q=q->rchild; 
		}// 将新结点插入树中
	if(f->data > X) 
		f->lchild=s; 
	else 
		f->rchild=s; 
	/*********************** End **********************/
}
void PrintData(BiTree T)
{//中序遍历输出二叉树T
    /***********************Begin**********************/
	if(T)
    {
        PrintData(T -> lchild);
        cout << T -> data << ' ';
        PrintData(T -> rchild);
    }
    /*********************** End **********************/
}
void PrintCount(BiTree T)
{//中序遍历输出二叉树T计数
    /***********************Begin**********************/
	if(T)
    {
        PrintCount(T -> lchild);
        cout << T -> count << ' ';
        PrintCount(T -> rchild);
    }
    /*********************** End **********************/
}
int main()
{
	int n;
	cin>>n;
	int e;        			//变量e用于接收输入数据
  	BiTree T=NULL;
	for(int i=0;i<n;i++)
	{//基于非递归的二叉排序树的结点的查找和插入
		cin>>e;
		SearchBST(T,e);
	}
	PrintData(T);			//中序遍历输出二叉树T结点
	cout<<endl;
	PrintCount(T);   		//中序遍历输出二叉树T计数
	cout<<endl;
	return 0;
}

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值