二叉排序树

#include<iostream>
#include<malloc.h> 
#include<stdlib.h>
#include<stack>
using namespace std;

class BST
{
protected:
	 struct BstNode
	{
		int data;
		struct BstNode *leftchild,*rightchild,*pa;
	};
	
private:
	BstNode *root;
	static BstNode *BuyNode( )
	{
		BstNode *s = (BstNode*)malloc(sizeof(BstNode));
		if(s == NULL) 
		{
			printf("weiwei\n");
			exit(1);
		}
		s->leftchild=NULL;
		s->rightchild=NULL;
		return s;
	}
	static BstNode *BuyNode(int key )
	{
		BstNode *s = (BstNode*)malloc(sizeof(BstNode));
		if(s == NULL) 
		{
			printf("weiwei\n");
			exit(1);
		}
        s->data=key;
		s->leftchild=NULL;
		s->rightchild=NULL;
		return s;
	}
	static void FreeNode(BstNode *ptr)
	{
		free(ptr);
		ptr=NULL;
	}
    static InOrder(BstNode *ptr)
	{
		std::stack<BstNode *> st;
		while(ptr!=NULL||!st.empty())
		{
			if(ptr!=NULL)
			{
				st.push(ptr);
				ptr=ptr->leftchild;
			}
			else
			{
				ptr=st.top();
				cout<<ptr->data<<"  ";
				st.pop();
				ptr=ptr->rightchild;
			}
		}
		cout<<endl;
	}
    static bool SearchBST(BstNode *T,int key,BstNode *pa,BstNode **ptr)
	{
		if(T==NULL)
		{
			*ptr=pa;
			return false;
		}
		else if(key==T->data)
		{
			*ptr=T;
			return true;
		}
		else if(key<T->data)
		{
			return SearchBST(T->leftchild,key,T,ptr);
		}
		else 
		{
			return SearchBST(T->rightchild,key,T,ptr);
		}

	}
	static bool InsertBST(BstNode **T,int key)
	{
		BstNode *p,*s;
		if(!SearchBST(*T,key,NULL,&p))
		{
			s=BuyNode(key);
			if(p==NULL)	 
				*T=s;
			else if(key<p->data)
				p->leftchild=s;
			else
				p->rightchild=s;
			return true;
		}
		else
			return false;
	}
	static bool Delete(BstNode **T)
	{
		BstNode *tmp,*s;
		if((*T)->rightchild==NULL)
		{
             tmp=*T;
			 (*T)=(*T)->leftchild;
			 free(tmp);
		}
		else if((*T)->leftchild==NULL)
		{
			tmp=*T;
			(*T)=(*T)->rightchild;
			free(tmp);
		}
		else
		{
			tmp=(*T);
			s=(*T)->leftchild;
			while(s->rightchild)
			{
				tmp=s;
				s=s->rightchild;
			}
			(*T)->data=s->data;
			if(tmp!=*T)
			{
				tmp->rightchild=s->leftchild;
			}
			else
			{
				tmp->leftchild=s->leftchild;
			}
			free(s);
		}
		return true;
	}
	static bool DeleteBST(BstNode * *T,int key)
	{
		if(!*T)
		{
            return false;
		}
		else
		{
			if(key==((*T)->data))
				return Delete(T);
			else if(key<(*T)->data)
			     return DeleteBST(&(*T)->leftchild,key);
			else 
				return DeleteBST(&(*T)->rightchild,key);
		}

	}
public:
	BST():root(NULL){	}
	BST(int *arr,int len):root(NULL)
	{
		int i=0;
		while(i<len)
		{
			InsertBST(&root,arr[i++]);
		}
	}
	void DeleteBST(int key)
	{
		DeleteBST(&root,key);
	}
	void InOrder()
	{
		InOrder(root);
	}

};
void main()
{
	int arr[10]={1,245,49,3954,83,345,94,2,6,0};
	int len=sizeof(arr)/sizeof(arr[0]);
	BST mytree1(arr,len);
	mytree1.InOrder();
	mytree1.DeleteBST(94);
	mytree1.InOrder();
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值