前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >搜索二叉树

搜索二叉树

作者头像
废江_小江
发布2022-09-05 11:16:18
2380
发布2022-09-05 11:16:18
举报
文章被收录于专栏:总栏目

搜索二叉树的定义很简单:

7ebb2688f0ec88d901ab162252765e60.png
7ebb2688f0ec88d901ab162252765e60.png

搜索二叉树可以用中序遍历来实现排序输出。。。 下面是自己写的搜索二叉树的代码

代码语言:javascript
复制
#include<bits/stdc++.h>
using namespace std;
typedef int ElementType ;
typedef struct tnode *BinTree;
typedef BinTree Position ;
struct tnode {
	ElementType Data;
	BinTree Left;
	BinTree Right;
};
Position FindMin( BinTree BST){
	if (!BST) return NULL;
	else if (!BST->Left)
		return BST;
	else
		return FindMin(BST->Left);
}
Position FindMax( BinTree BST){
	if(BST)
	 	while ( BST->Right) BST =BST->Right;
	return BST;
}
BinTree Insert( BinTree BST, ElementType X ) {
	if( !BST ) { 
		BST = (BinTree)malloc(sizeof(struct tnode));
		BST->Data = X;
		BST->Left = BST->Right = NULL;
	} else {
		if( X < BST->Data )
			BST->Left = Insert( BST->Left, X );   
		else  if( X > BST->Data )
			BST->Right = Insert( BST->Right, X ); 
		
	}
	return BST;
}
BinTree Delete( BinTree BST, ElementType X ) 
{ 
    Position Tmp; 
 
    if( !BST ) 
        printf("要删除的元素未找到"); 
    else {
        if( X < BST->Data ) 
            BST->Left = Delete( BST->Left, X );   /* 从左子树递归删除 */
        else if( X > BST->Data ) 
            BST->Right = Delete( BST->Right, X ); /* 从右子树递归删除 */
        else { /* BST就是要删除的结点 */
            /* 如果被删除结点有左右两个子结点 */ 
            if( BST->Left && BST->Right ) {
                /* 从右子树中找最小的元素填充删除结点 */
                Tmp = FindMin( BST->Right );
                BST->Data = Tmp->Data;
                /* 从右子树中删除最小元素 */
                BST->Right = Delete( BST->Right, BST->Data );
            }
            else { /* 被删除结点有一个或无子结点 */
                Tmp = BST; 
                if( !BST->Left )       /* 只有右孩子或无子结点 */
                    BST = BST->Right; 
                else                   /* 只有左孩子 */
                    BST = BST->Left;
                free( Tmp );
            }
        }
    }
    return BST;
}
BinTree Find( BinTree BST, ElementType X ) {
	if(!BST ) return NULL;
	if( X >BST->Data )
		return Find(BST->Right,X);
	else if(X<BST->Data)
		return Find(BST->Left,X);
	else
		return BST;
}
void preorder(BinTree BST){  //先序遍历 
	if(BST){
	printf("%d ",BST->Data);
	preorder(BST->Left);
	preorder(BST->Right);
}
}
void inorder(BinTree BST){  //中序遍历 
	if(BST){
	inorder(BST->Left);
	printf("%d ",BST->Data);
	inorder(BST->Right);
}
}
int main() {
    BinTree BST;
    BST=NULL;
//    int a[10];
//    for(int i=0;i<9;i++)
//    cin>>a[i];
//    for(int i=0;i<9;i++){
//    	BST=Insert(BST,a[i]);
//	}
	for(int i=0;i<9;i++){
    	BST=Insert(BST,i);
	}
    preorder(BST);
    cout<<endl;
    inorder(BST);
}

废江博客 , 版权所有丨如未注明 , 均为原创丨本网站采用BY-NC-SA协议进行授权 转载请注明原文链接:搜索二叉树

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2019-11-04),如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档