1. 程式人生 > >二叉搜尋樹的操作題集

二叉搜尋樹的操作題集

BinTree Insert( BinTree BST, ElementType X )
{
	if(BST==NULL)
	{
		BinTree gg=(BinTree)malloc(sizeof(struct TNode));
		gg->Data=X;
		gg->Left=NULL;
		gg->Right=NULL;
		BST=gg;
		return BST;
	}
	else
	{
		BinTree hh=BST,parent;
		int flag;
		while(hh!=NULL)
		{
			parent=hh;
			if(X>hh->Data)
			hh=hh->Right,flag=1;
			else
			hh=hh->Left,flag=0;
		}
		BinTree tmp=(BinTree)malloc(sizeof(struct TNode));
		tmp->Data=X;
		tmp->Left=NULL;
		tmp->Right=NULL;
		hh=tmp;
		if(flag)parent->Right=hh;
		else parent->Left=hh;
		return BST;
	}
}
Position Find( BinTree BST, ElementType X )
{
	if(BST)
	{
		while(BST)
		{
			if(X==BST->Data)return BST;
			else if(X>BST->Data)
			BST=BST->Right;
			else if(X<BST->Data)
			BST=BST->Left;	
		}
	} 
	return BST;
}
Position FindMin( BinTree BST )//找到搜尋二叉樹中最小值 
{
	BinTree gg=NULL;
	if(BST)
	{
		while(BST)
		gg=BST,	BST=BST->Left;
	}
	return gg;
}
Position FindMax( BinTree BST )//找到搜尋二叉樹最大值 
{
	BinTree gg=NULL;
	if(BST)
	{
		while(BST)
		gg=BST,BST=BST->Right;
	}
	return gg;
}
BinTree Delete( BinTree BST, ElementType X )
{
	//刪除處理的關鍵在於 刪除這個節點
	//之後還要就是將後面是節點給補上去 
	Position tmp;  
    if(!BST)
	{
		printf("Not Found\n");
		return BST; 
	}
	else if(X<BST->Data)
	BST->Left=Delete(BST->Left,X);
	else if(X>BST->Data)
	BST->Right=Delete(BST->Right,X);
	else
	{
		 if(BST->Left && BST->Right) {               /* 被刪除的結點有左右子結點 */
            tmp=FindMin(BST->Right);                /* 在右子樹中找到最小結點填充刪除結點 */
            BST->Data = tmp ->Data;
            BST->Right=Delete(BST->Right,BST->Data);/* 遞迴刪除要刪除結點的右子樹中最小元素 */
			//這一步無法理解  
		}else 
		{
			if(!BST->Left)
			BST=BST->Right;
			else if(!BST->Right)
			BST=BST->Left;
		}
	} 
	return BST; 
} 

相關推薦

【LeetCode】99. 恢復搜尋報告 (C++)

原題地址:https://leetcode-cn.com/problems/recover-binary-search-tree/description/ 題目描述: 二叉搜尋樹中的兩個節點被錯誤地交換。 請在不改變其結構的情況下,恢復這棵樹。 示例 1: 輸入: [1,3,nu

搜尋操作

BinTree Insert( BinTree BST, ElementType X ) { if(BST==NULL) { BinTree gg=(BinTree)malloc(sizeof(struct TNode)); gg->Data=X; gg->Left=NULL;

[PTA] 資料結構與演算法題目 6-12 搜尋操作

唯一比較需要思考的刪除操作: 被刪除節點有三種情況: 1、葉節點,直接刪除 2、只有一個子節點,將子節點替換為該節點,刪除該節點。 3、有兩個子節點,從右分支中找到最小節點,將其值賦給被刪除節點的位置,接著刪除這個最小節點    // 函式Insert將X插入二叉搜尋樹BST並返

04-7 搜尋操作 (30 分)

本題要求實現給定二叉搜尋樹的5種常用操作。 函式介面定義: BinTree Insert( BinTree BST, ElementType X ); BinTree Delete( BinTree BST, ElementType X ); Position Find( BinTree

6-12 搜尋操作-PTA

函式介面定義: BinTree Insert( BinTree BST, ElementType X ); BinTree Delete( BinTree BST, ElementType X ); Position Find( BinTree BST, ElementType X ); P

7 搜尋操作

函式介面定義: BinTree Insert( BinTree BST, ElementType X ); BinTree Delete( BinTree BST, ElementType X ); Position Find( BinTree BST, ElementTy

《資料結構》04-7 搜尋操作

題目 本題要求實現給定二叉搜尋樹的5種常用操作。 函式介面定義: BinTree Insert( BinTree BST, ElementType X ); BinTree Delete( BinTree BST, ElementType X ); Positi

PTA資料結構與演算法題目(中文)4-12 搜尋操作 (30分)

本題要求實現給定二叉搜尋樹的5種常用操作。 函式介面定義: BinTree Insert( BinTree BST, ElementType X ); BinTree Delete( BinTree BST, ElementType X ); Position Fin

資料結構---04-7 搜尋操作(30 分)

#include<cstdio> #include<cstdlib> #include<iostream> using namespace std; typedef int ElementType; typedef struct TNode* Position; typed

04-7 搜尋操作 (30分)

#include <stdio.h> #include <stdlib.h> typedef int ElementType; typedef struct TNode *Position; typedef Position Bin

04-7 搜尋操作

二叉搜尋樹的各種操作! 程式碼: #include <stdio.h> #include <stdlib.h> typedef int ElementType; typedef struct TNode *Position; typedef P

4-12 搜尋操作 (30分)

本題要求實現給定二叉搜尋樹的5種常用操作。 函式介面定義: BinTree Insert( BinTree BST, ElementType X ); BinTree Delete( BinTree BST, ElementType X ); Posit

關於劍指offer上“搜尋與雙向連結串列”的理解

題目描述: 輸入一棵二叉搜尋樹,將該二叉搜尋樹轉換成一個排序的雙向連結串列。要求不能建立任何新的結點,只能調整樹中結點指標的指向。 一、遞迴的思路 對於函式TreeNode* Convert(TreeNode* root),傳入的是需要轉換的二叉樹的頭結點,返回的是已經轉換好的

【LeetCode 簡單】64-搜尋的最近公共祖先

宣告: 今天是第64道題。給定一個二叉搜尋樹, 找到該樹中兩個指定節點的最近公共祖先。以下所有程式碼經過樓主驗證都能在LeetCode上執行成功,程式碼也是借鑑別人的,在文末會附上參考的部落格連結,如果侵犯了博主的相關權益,請聯絡我刪除 (手動比心ღ( ´・ᴗ・` )) 正文 題目:

【Java】 劍指offer(33) 搜尋的後序遍歷序列 《劍指Offer》Java實現合 《劍指Offer》Java實現合

本文參考自《劍指offer》一書,程式碼採用Java語言。 更多:《劍指Offer》Java實現合集   題目    輸入一個整數陣列,判斷該陣列是不是某二叉搜尋樹的後序遍歷的結果。如果是則返回true,否則返回false。假設輸入的陣列的任意兩個數字都互不相同。 思路   二叉

【LeetCode】95. 不同的搜尋 II 結報告 (C++)

原題地址:https://leetcode-cn.com/problems/unique-binary-search-trees-ii/description/ 題目描述: 給定一個整數 n,生成所有由 1 ... n 為節點所組成的二叉搜尋樹。 示例: 輸入: 3 輸出: [ &n

【LeetCode】173. 搜尋迭代器 結報告 (C++)

原題地址:https://leetcode-cn.com/problems/binary-search-tree-iterator/description/ 題目描述: 實現一個二叉搜尋樹迭代器。你將使用二叉搜尋樹的根節點初始化迭代器。 呼叫 next() 將返回二叉搜尋樹中的下一個最小

演算法(十九):搜尋轉雙鏈表

題目描述 輸入一棵二叉搜尋樹,將該二叉搜尋樹轉換成一個排序的雙向連結串列。要求不能建立任何新的結點,只能調整樹中結點指標的指向。 輸入輸出示例: {10,6,4,8,14,12,16} from left to right:4,6,8,10,12,14,16 from right

C語言-基本操作以及搜尋基本操作

功能 二叉樹操作: 建立二叉樹 遍歷二叉樹(前序,中序,後續) 計算高度 計算結點數目 清空二叉樹 空樹判斷 二叉搜尋樹操作: 插入 最值(最大值,最小值) 刪除 程式碼 #include &l

【LeetCode 簡單】117-搜尋中的眾數

宣告: 今天是第117道題。給定兩個沒有重複元素的陣列 nums1 和 nums2 ,其中nums1 是 nums2 的子集。找到 nums1 中每個元素在 nums2 中的下一個比其大