zoukankan      html  css  js  c++  java
  • 二叉搜索树的实现

    // bogo.cpp : 定义控制台应用程序的入口点。
    //
    
    #include "stdafx.h"
    #include <stdio.h>
    #include <iostream>
    #include <time.h>
    using namespace std;
    
    struct Tree
    {
    	int key;
    	Tree *left;
    	Tree *right;
    	Tree *p;
    };
    
    Tree *gRoot = NULL;
    
    //插入节点
    void fnInsert(Tree *date)
    {
    	if (!gRoot)
    	{
    		gRoot = new Tree;
    		gRoot->key = date->key;
    		gRoot->left = NULL;
    		gRoot->right = NULL;
    		gRoot->p = NULL;
    	}
    	else
    	{
    		Tree *x = gRoot;
    		Tree *y = NULL;
    		while (x != NULL)
    		{
    			y = x;
    			if (date->key < x->key)
    				x = x->left;
    			else x = x->right;
    		}
    
    		date->p = y;
    
    		if(date->key < y->key)
    			y->left = date;
    		else y->right = date;
    
    		date->left = NULL;
    		date->right = NULL;
    
    	}
    }
    
    void fnSwapNode(Tree *src,Tree *des)
    {
    	if (src->p == NULL)
    		gRoot = des;
    	else if (src == src->p->left)
    		src->p->left = des;
    	else src->p->right = des;
    
    	if(des != NULL)
    		des->p = src->p;
    
    }
    
    //中序遍历树
    void fnShowTree(Tree *date)
    {
    	if (!date) return ;
    	fnShowTree(date->left);
    	cout<<date->key<<" ";
    	fnShowTree(date->right);
    }
    
    Tree *fnFindMin(Tree *src)
    {
    	while (src->left != NULL)
    		src = src->left;
    	return src;
    }
    
    void fnDeleteNode(Tree *des)//删除节点
    //删除节点分四种情况
    {
    	if (des->left == NULL)
    		fnSwapNode(des,des->right);
    	else if(des->right == NULL)
    		fnSwapNode(des,des->left);
    	else
    	{
    		Tree *x = fnFindMin(des->right);
    		if (x->p != des)
    		{
    			fnSwapNode(x,x->right);
    			x->right = des->right;
    			x->right->p = x;
    		}
    		fnSwapNode(des,x);
    		x->left = des->left;
    		x->left->p = x;
    	}
    	delete(des);
    }
    
    Tree *fnSearchTree(int key)//寻找树
    {
    	if (gRoot->key == key)
    		return gRoot;
    	else
    	{
    		Tree *temp = gRoot;
    		while (gRoot != NULL)
    		{
    			if (key < temp->key)
    				temp = temp->left;
    			else if (key > temp->key)
    				temp = temp->right;
    			else return temp;
    		}
    		return NULL;
    	}
    }
    
    void fnDestoryTree(Tree *x)//销毁树
    {
    	if ( x == NULL) return;
    	else
    	{
    		fnDestoryTree(x->left);
    		fnDestoryTree(x->right);
    		delete(x);
    	}
    }
    
    void fnRandBuildTree(int num)//随机生出num个节点并插入二叉树
    {
    	srand((int)time(0));
    	Tree *temp = NULL;
    	while (num--)
    	{
    		temp = new Tree;
    		temp->key = rand()%100;
    		fnInsert(temp);
    	}
    }
    
    int _tmain(int argc, _TCHAR* argv[])
    {
    	int x;
    	Tree *temp;
    	while (true)
    	{
    		cout<<"1.随机生成二叉树"<<endl;
    		cout<<"2.插入节点"<<endl;
    		cout<<"3.删除节点"<<endl;
    		cout<<"4.浏览节点"<<endl;
    		cout<<"5.退出程序"<<endl;
    
    		cin>>x;
    		switch (x)
    		{
    		case 1:
    			{
    			cout<<"输入节点数:";
    			cin>>x;
    			fnRandBuildTree(x);
    			break;
    			}
    		case 2:
    			{
    				cout<<"输入节点数:";
    				temp = new Tree;
    				cin>>temp->key;
    				fnInsert(temp);
    				break;
    			}
    		case 3:
    			{
    				cout<<"输入要删除节点数据:";
    				cin>>x;
    				fnDeleteNode(fnSearchTree(x));
    				break;
    			}
    		case 4:
    			{
    				cout<<"遍历输出"<<endl;
    				fnShowTree(gRoot);
    				break;
    			}
    		case 5:
    			{
    				fnDestoryTree(gRoot);
    				exit(-1);
    			}
    		default:
    			break;
    		}
    		system("pause");
    		system("cls");
    	}
    
    	return 0;
    }

  • 相关阅读:
    产生WM_PAINT 消息
    模板作为模板的参数
    转:网络游戏同步原理荟萃
    一个隐晦的c++语法问题
    Winbind authentication against active directory
    In Place Upgrade of CentOS 6 to CentOS 7
    How To mount/Browse Windows Shares【在linux{centos}上挂载、浏览window共享】
    File System Shell
    Administration Commands
    User Commands
  • 原文地址:https://www.cnblogs.com/zkkkkkky/p/4435788.html
Copyright © 2011-2022 走看看