【数据结构】BST:二叉排序树算法

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/hujingshuang/article/details/49679991

建立二叉排序树,实现树的插入、删除,前、中、后序遍历(递归方式)等操作。

/*****************************************   
Copyright (c) 2015 Jingshuang Hu   
   
@filename:demo.cpp   
@datetime:2015.11.03   
@author:HJS   
@e-mail:eleftheria@163.com   
@blog:http://blog.csdn.net/hujingshuang   
*****************************************/
#include <iostream>
#include "Binary_Tree.h"

using namespace std;

int main()
{
	int N = 0;
	int *data;
	cout << "N = ";
	cin >> N;
	data = (int *)malloc(N * sizeof(int));
	cout << "数据:";
	for (int i = 0; i < N; ++i)
	{
		cin >> data[i];
	}
	//30 12 54 8 24 35 70 19 28 48
	//30 12 54 24 35 70 19 28 48 37 36 38
	BT BTree;
	BTree.Create_Binary_Sort_Tree(data, N);
	while(1)
	{
		cout << "1.遍历\t2.插入\t3.删除\t4.退出"<<endl;
		cout <<"选择:";
		int choice;
		cin >> choice;
		switch(choice)
		{
			case 1:
				cout << "前序:";
				BTree.Pre_Oder_Traverse(BTree.root);
				cout << endl;
				cout << "中序:";
				BTree.In_Oder_Traverse(BTree.root);
				cout << endl;
				cout << "后序:";
				BTree.Post_Oder_Traverse(BTree.root);
				cout << endl;
				break;
			case 2:
				cout << "插入:";
				int value;
				cin >> value;
				BTree.Insert_Elem(value);
				break;
			case 3:
				cout << "删除:";
				cin >> value;
				BTree.Delete_Elem(value);
				break;
			case 4:
				return 0;
				break;
			default:
				break;
		}
	}
	return 0;
}

/*****************************************   
Copyright (c) 2015 Jingshuang Hu   
   
@filename:Binary_Tree.h   
@datetime:2015.11.03   
@author:HJS   
@e-mail:eleftheria@163.com   
@blog:http://blog.csdn.net/hujingshuang   
*****************************************/
#ifndef _BINARY_TREE_H_
#define _BINARY_TREE_H_

#include <iostream>

using namespace std;

struct Tree
{
	int elem;
	Tree *left;
	Tree *right;
};

class BT
{
public:
	BT();											//构造函数
	void Create_Binary_Sort_Tree(int *, int);		//建立二叉树
	void Pre_Oder_Traverse(Tree *);					//前序
	void In_Oder_Traverse(Tree *);					//中序
	void Post_Oder_Traverse(Tree *);				//后序
	void Insert_Elem(int);							//插入
	void Delete_Elem(int);							//删除
//	void Show_Tree(void);							//显示二叉树
//	virtual void Level_Oder_Traverse(Tree *) = 0;	//纯虚函数
//private:
	Tree *root;
};

#endif
/*****************************************   
Copyright (c) 2015 Jingshuang Hu   
   
@filename:Binary_Tree.cpp   
@datetime:2015.11.03   
@author:HJS   
@e-mail:eleftheria@163.com   
@blog:http://blog.csdn.net/hujingshuang   
*****************************************/
#include "Binary_Tree.h"

//初始化二叉树
BT::BT()
{
	root = NULL;
}
//建立二叉树
void BT::Create_Binary_Sort_Tree(int *data, int N)
{
	Tree *nowfreenode = new Tree;					//生成新的节点
	Tree *prefreenode = new Tree;
	root = new Tree;
	root->elem = data[0];							//父节点元素
	root->left = root->right = NULL;				//左右孩子为空
//30 12 54 8 24 35 70 19 28 48
	for (int i = 1; i < N; ++i)
	{
		nowfreenode = root;
		Tree *newnode = new Tree;					//生成新的节点
		newnode->elem = data[i];
		newnode->left = newnode->right = NULL;

		while(nowfreenode != NULL)					//空
		{
			prefreenode = nowfreenode;				//用于记录前一个节点
			if (newnode->elem < nowfreenode->elem)	//挂在左边
			{
				nowfreenode = nowfreenode->left;
			}
			else
			{
				nowfreenode = nowfreenode->right;	//挂在右边
			} 
		}
		if (newnode->elem < prefreenode->elem)
		{
			prefreenode->left = newnode;
		}
		else
		{
			prefreenode->right = newnode;
		}
	}
	delete nowfreenode;
//	delete prefreenode;
}
//前序
void BT::Pre_Oder_Traverse(Tree *T)
{
	if (T != NULL)
	{
		cout << T->elem << " ";
		Pre_Oder_Traverse(T->left);
		Pre_Oder_Traverse(T->right);
	}
}
//中序
void BT::In_Oder_Traverse(Tree *T)
{
	if (T != NULL)
	{
		In_Oder_Traverse(T->left);
		cout << T->elem << " "; 
		In_Oder_Traverse(T->right);
	}
}
//后序
void BT::Post_Oder_Traverse(Tree *T)
{
	if (T != NULL)
	{
		Post_Oder_Traverse(T->left);
		Post_Oder_Traverse(T->right);
		cout << T->elem << " ";
	}
}
//插入
void BT::Insert_Elem(int data)
{
	Tree *nowfreenode = new Tree;
	Tree *prefreenode = new Tree;
	nowfreenode = root;
	Tree *newnode = new Tree;					//生成新的节点
	newnode->elem = data;
	newnode->left = newnode->right = NULL;
	while(nowfreenode != NULL)					//空
	{
		prefreenode = nowfreenode;				//用于记录前一个节点
		if (newnode->elem < nowfreenode->elem)	//挂在左边
		{
			nowfreenode = nowfreenode->left;
		}
		else
		{
			nowfreenode = nowfreenode->right;	//挂在右边
		} 
	}
	if (newnode->elem < prefreenode->elem)
	{
		prefreenode->left = newnode;
	}
	else
	{
		prefreenode->right = newnode;
	}
}
//删除
void BT::Delete_Elem(int data)
{
	Tree *prefreenode = new Tree;
	Tree *nowfreenode = new Tree;
	nowfreenode = root;

	Tree *newnode = new Tree;
	newnode->elem = data;
	newnode->left = newnode->right = NULL;
	prefreenode = nowfreenode;
	//找到元素所在节点nowfreenode
	while((nowfreenode != NULL) && (newnode->elem != nowfreenode->elem))
	{
		prefreenode = nowfreenode;
		if (newnode->elem < nowfreenode->elem)		//往左走
		{
			nowfreenode = nowfreenode->left;
		}
		else if (newnode->elem > nowfreenode->elem)	//往右走
		{
			nowfreenode = nowfreenode->right;
		}
	}
//
	if ((nowfreenode->left != NULL) && (nowfreenode->right == NULL))//只有左子树,用左子树代替节点
	{	
		if ((prefreenode->left != NULL) && (prefreenode->left->elem == nowfreenode->elem))
		{
			prefreenode->left = nowfreenode->left;
		}
		else
		{
			if (prefreenode == nowfreenode)
			{
				root = nowfreenode->left;
			}
			else
			{
				prefreenode->right = nowfreenode->left;
			}
		}
	}
	else if ((nowfreenode->left == NULL) && (nowfreenode->right != NULL))//只有右子树,用右子树代替节点
	{
		if ((prefreenode->left != NULL) && (prefreenode->left->elem == nowfreenode->elem))
		{
			prefreenode->left = nowfreenode->right;
		}
		else
		{
			if (prefreenode == nowfreenode)
			{
				root = nowfreenode->right;
			}
			else
			{
				prefreenode->right = nowfreenode->right;
			}
		}
	}
	else if ((nowfreenode->left == NULL) && (nowfreenode->right == NULL))//是叶子节点,直接删除
	{
		if ((prefreenode->left != NULL) && (prefreenode->left->elem == nowfreenode->elem))
			prefreenode->left = NULL;
		else
			prefreenode->right = NULL;
	}
	else//左右子树都存在,用左子树最右节点代替节点
	{
		prefreenode = nowfreenode;
		nowfreenode = nowfreenode->left;
		Tree *pfreenode = prefreenode;
		while(nowfreenode->right != NULL)
		{
			pfreenode = nowfreenode;
			nowfreenode = nowfreenode->right;
		}
		prefreenode->elem = nowfreenode->elem;
		pfreenode->right = nowfreenode->left;
		delete prefreenode;
	}
//	delete nowfreenode;
}

阅读更多
换一批

没有更多推荐了,返回首页