二叉查找树

#include <stdio.h>
#include <malloc.h>

typedef struct node *Position;
typedef int ElementT;

Position InsertValue(Position T,ElementT value);
Position Find(Position,ElementT);
Position FindMax(Position);
Position FindMin(Position);
ElementT DeleteTree(Position);    //此处要用ElementT
void PrintSortedTree(Position);
ElementT delete_leaf(Position T);  //<span style="font-family: Arial, Helvetica, sans-serif;">此处要用ElementT</span>
int Is_leaf(Position T);
int Is_root(Position T);

void Insert_node(Position,Position);

struct node
{
	ElementT element;
	Position parent;
	Position left;
    Position right;
}; 

int main()
{
	Position T=NULL;
	int c;
	printf("输入数字以0为结束:");
	while (~scanf("%d",&c),c)
	{
		T = InsertValue(T,c);
	}
	//---------------------------排序输出(升序)
	printf("排序后的所有值为:\n");
    PrintSortedTree(T);
	//---------------------------查找
	int value;
	Position Find_value;
	printf("输入要查找的数字为:");
	scanf("%d",&value);
	Find_value=Find(T,value);
	if(Find_value != NULL)
	    printf("%d\n",Find_value->element);
	else
		printf("未找到此元素\n");
	//---------------------------查找最小值
    printf("最小值为:");
    printf("%d\n",FindMin(T)->element);
	//---------------------------查找最大值
    printf("最大值为:");
    printf("%d\n",FindMax(T)->element);
	//---------------------------删除
    printf("要删除的数字为:"); 
	scanf("%d",&value);
	Position del;
	del = Find(T,value);
	if(del == NULL) printf("未找到此元素\n");
	else
	{ 
		DeleteTree(del);
	    PrintSortedTree(T);
	}
	return 0;
}

Position InsertValue(Position T,ElementT value)
{
    Position np;
	np=(Position)malloc(sizeof(node));
	np->element = value;
	np->left = NULL;
	np->right = NULL;
	np->parent = NULL;
	if (T == NULL)
    {
		T = np;
	}
	else
	{
        Insert_node(np,T);
	}
	return T;
}

Position Find(Position T,ElementT value)
{
	if(T == NULL) return NULL;
    if(value == T->element) return T;
	else
	{
		if (value < T->element)
		{
			Find(T->left,value);
		}
		else
			Find(T->right,value)                                                                                                                                             ;
	}
}

Position FindMin(Position T)
{
    if(T->left == NULL) return T;
	else
		FindMin(T->left);
}


Position FindMax(Position T)
{
    if(T->right == NULL) return T;
	else
		FindMax(T->right);
}

ElementT DeleteTree(Position del)    //删除树是最复杂的,考虑叶子的删除和不是叶子的删除
{
	Position temp;
	ElementT element;
	int flag=1;
	if (Is_leaf(del))
	{
		return delete_leaf(del);
	}
	else
	{
        temp = (del->left != NULL)?FindMax(del->left):FindMin(del->right);
        element = del->element;
		del->element = DeleteTree(temp);
		return element;
	}
}

ElementT delete_leaf(Position np)
{
     ElementT element;
	 Position parent;
	 element = np->element;
	 parent = np->parent;
	 if (!Is_root(np))
	 {
        if (parent->left == np)
        {
			parent->left=NULL;
        }
		else
		{
			parent->right = NULL;
		}
	 }
	 free(np);
	 return element;
}

void Insert_node(Position np,Position T) //并未修改T,一直指向根
{
    if (np->element <= T->element)
    {
		if (T->left == NULL)
		{
            T->left = np;
			np->parent = T;
			return ;//停止作用
		}
		else
		{
            Insert_node(np,T->left);
		}
    }
	else
	{
		if (T->right == NULL)
		{
			T->right = np;
			np->parent = T;
			return ;
		}
		else
		{
            Insert_node(np,T->right); 
		}
	}
}

int Is_leaf(Position T)
{
   return (T->left == NULL && T->right == NULL);
}

int Is_root(Position T)
{
	return (T->parent == NULL);
}


void PrintSortedTree(Position T)
{
    if(T == NULL ) return ;
	PrintSortedTree(T->left);
	printf("%d\n",T->element);
	PrintSortedTree(T->right);
}

方法2:

#include<iostream>
using namespace std;
typedef struct tree
{
    tree *l,*r;
    int num;
}tree;
tree *creat(int x)                         //建树
{
    tree *t=(tree *)malloc(sizeof(tree));
    t->l=0;
    t->r=0;
    t->num=x;
    return t;
}
tree *inster(tree *s,int x)               //插入结点
{
    if(s==NULL)                    
    {
        tree *t=creat(x);
        s=t;
    }
    else
    {
        if(x<=s->num)
            s->l=inster(s->l,x);
        else
            s->r=inster(s->r,x);
    }
    return s;
}
tree *findmin(tree *s)               //找tree的最小值(左子树最左边的为最小值)
{
    if(s==NULL)
        return NULL;
    else if(s->l==NULL)
        return s;
    else
        return findmin(s->l);
}
tree *del(tree *s,int x)        //在删除一个结点后,要有一个结点可以承接此结点位置的,要么取被删除结点右子树中最小的那个结点,
{                                  //要么取其左子树中最大的那个结点
    tree *t;
    if(s==NULL)
        return NULL;
    else if(x<s->num)
        s->l=del(s->l,x);
    else if(x>s->num)
        s->r=del(s->r,x);
    else
        if(s->l&&s->r)            //如果两个孩子都存在
        {
            t=findmin(s->r);       //找右子树中最小的那个结点
            s->num=t->num;
            s->r=del(s->r,s->num);
            return s;
        }
        else                       //只有一个孩子在,那么直接操作就好
        {
            t=s;
            if(s->l==NULL)
                s=s->r;
            else if(s->r==NULL)
                s=s->l;
            free(t);
        }
        return s;
}



//2015 - 10 -14

#include <iostream>
#include <cstring>
using namespace std;

//  二叉树   创建 插入  查找最小最大值   删除*

typedef struct tree {

	 tree *l;
	 tree *r;
	 int data;

}*ptree;

ptree creat(int data)
{
    ptree t = (ptree)malloc(sizeof(tree));
	t->l = NULL;
	t->r = NULL;
	t->data = data;
	return t;
}

ptree insert(ptree t,int data)
{
    if(t == NULL)
	{
		t = creat(data);
	}
	else
	{
		if(data <= t->data)
		{
            t->l = insert(t->l,data);
		}
		else
		{
			t->r = insert(t->r,data);
		}
	}
	return t;
}

ptree find_min(ptree t)
{
    if(t->l->l == NULL)
	{
		return t;
	}
	else
	{
		find_min(t->l);
	}
}

ptree find_max(ptree t)
{
	if(t->r->r == NULL)
	{
		return t;
	}
	else
	{
		find_min(t->r);
	}
}

//二叉树的删除是个难点
//原先不知道指针传参,在函数里修改是不能修改的,但是指针所取得值是可以修改的
//所以就需要一个父指针
ptree find(ptree p,int data,ptree root,ptree parent)
{
	//ptree root = p;
	ptree f;

    if(p->data == data)
	{
		if(p == root)   //删除头部
		{
			f = find_min(p->r);
            root->data = f->l->data;
			free(f->l);
			f->l = NULL;
		}
		else
		{
			if(p->l && p->r)  //中间的
			{
				if(parent->l->data == data)
				{
                    parent->l = p->l;
					parent->l->r = p->r;
				}
				else
				{
					parent->r = p->l;
					parent->r->r = p->r;
				}
			}
			else                  //尾部  
			{
				if(p->l == NULL  && p->r == NULL)
                {
					if(parent->l->data == data)
					{
						free(parent->l);
						parent->l = NULL;
					}
					else
					{
						free(parent->r);
						parent->r = NULL;
					}
				}
				else
				{
					if(parent->l->data == data)
					{
						if(p->l == NULL)
							parent->l = p->r;
						else
					        parent->l = p->l; 
					}
					else
					{
						if(p->l == NULL)
							parent->r = p->r;
						else
					        parent->r = p->l; 
					}

				}
			}
		}
	}
	else
	{
		if(data < p->data)
		{
            find(p->l,data,root,p);
		}
		else
		{
			find(p->r,data,root,p);
		}
	}
	return NULL;
}

int main()
{
	int data;
	cin>>data;

    ptree root = creat(data);//指针的初始化不能忘,要传参
    ptree t = root;

	while(cin>>data)
	{
	    t = insert(t,data);
	}
     
	t = find_min(root); //传出来的是父节点
	if(t)
		cout<<"最小值为: "<<t->l->data<<endl;

	cin.clear();
	cin.sync();
    cout<<"输入要删除的数字: ";
	cin>>data;
    find(root,data,root,root);

	t = find_min(root);
	if(t)
		cout<<"最小值为: "<<t->l->data<<endl;

	t = find_max(root);
	if(t)
		cout<<"最大值为: "<<t->r->data<<endl;

    //cout<<root->l->data<<"  "<<root->r->data<<endl;测试
	return 0;
}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值