二叉树的相关操作总结

二叉树三种递归和非递归遍历实现,二叉树的构造,插入,删除操作,二叉树的前序,中序来构造二叉树,二叉树的中序,后序来确定二叉树

#include <iostream>
#include <cstring>
using namespace std;
#define NUM 11

class BinStree;
class BinSTreeNode
{
public:
	int key;
	BinSTreeNode* lchild;
	BinSTreeNode* rchild;
	BinSTreeNode()
	{
		lchild = rchild = NULL;
	}
};
class BinStree
{
public:
	BinSTreeNode* root;
	BinStree()
	{
		root = NULL;
	}
	~BinStree()
	{
		//delete the root;
	}
	BinSTreeNode *BinStreeSearch(BinSTreeNode *bt, int key, BinSTreeNode*&p);
	void BSTreeInsert(BinSTreeNode *&bt, int k);
	int BSTreeDelete(BinSTreeNode*&bt, int k);
	void BSTreePreOrder(BinSTreeNode *bt); //前序遍历
	void BSTreePreOrderRecursion(BinSTreeNode*bt); //前序递归实现
	void BSTreeInOrderRecursion(BinSTreeNode*bt); //中序遍历递归实现
	void BSTreeInOrder(BinSTreeNode *bt); //中序遍历
	void BSTreePostOrder(BinSTreeNode *bt); //后序遍历
	void BSTreePostOrderRecursion(BinSTreeNode*bt); //后序遍历递归实现
	BinSTreeNode* RebuildBinTreePreIn(int* preOrder, int* midOrder, int len); //前序中序遍历重构二叉树
	BinSTreeNode* RebuildBinTreeInPost(int* midOrder, int* postOrder, int len); //中序后序遍历重构二叉树
	bool IsEmpty()
	{
		return root == NULL;
	}
};
BinSTreeNode* BinStree::BinStreeSearch(BinSTreeNode*bt, int k, BinSTreeNode*&p)
{
	BinSTreeNode *q = NULL;
	q = bt; 
	while(q)
	{
		p=q;
		if(q->key == k)
			return p;
		if(q->key > k)
			q = q->lchild;
		else
			q = q->rchild;
	}
	return NULL;
}
void BinStree::BSTreeInsert(BinSTreeNode*&bt, int k)
{
	BinSTreeNode *p = NULL, *q;
	q = bt;
	if(BinStreeSearch(q, k, p) == NULL)
	{
		BinSTreeNode* r = new BinSTreeNode;
		r->key = k;
		r->lchild = NULL;
		r->rchild = NULL;
		if(q == NULL)
			bt = r;
		if(p && k < p->key)
			p->lchild = r;
		else if(p)
			p->rchild = r;
	}
	
}
int BinStree::BSTreeDelete(BinSTreeNode* &bt, int k)
{
	BinSTreeNode* f, *p, *q, *s;
	p = bt; 
	f = NULL;
	while(p && p->key != k)
	{
		f = p; 
		if(p->key > k)
			p = p->lchild;
		else
			p = p->rchild;
	}
	if(p == NULL)
		return 0;
	if(p->lchild == NULL)
	{
		if(f == NULL)
			bt = p->rchild;
		else if(f->lchild == p)
			f->lchild = p->rchild;
		else
			f->rchild = p->rchild;
		delete p;
	}
	else
	{
		q = p;
		s = p->lchild;
		while(s->rchild)
		{
			q = s;
			s = s->rchild;
		}
		if(q == p)
			q->lchild = s->lchild;
		else
		{
			q->rchild = s->lchild;
		}
		p->key = s->key;
		delete s;
	}
	return 1;
}
void BinStree::BSTreePreOrder(BinSTreeNode*bt)
{
	BinSTreeNode* stack[NUM];
	BinSTreeNode* p = bt;
	int top = 0;
	while(top != 0 || p != NULL)
	{
		if(p != NULL)
		{
			cout << p->key << " ";
			stack[top++] = p;
			p = p->lchild;
		}
		else
		{
			p = stack[--top];
			p = p->rchild;
		}
		
	}
	
}
void BinStree::BSTreePreOrderRecursion(BinSTreeNode* bt)
{
	if(bt != NULL)
	{
		cout << bt->key << " ";
		BSTreePreOrderRecursion(bt->lchild);
		BSTreePreOrderRecursion(bt->rchild);
	}
}
void BinStree::BSTreeInOrder(BinSTreeNode* bt)
{
	BinSTreeNode* stack[NUM];
	BinSTreeNode* p = bt;
	int top = 0;
	while(top != 0 || p !=NULL)
	{
		if(p != NULL)
		{
			stack[top++] = p;
			p = p->lchild;
		}
		else
		{
			p = stack[top-1];
			cout << p->key << " ";
			stack[top--];
			p = p->rchild;
		}
	}
}
void BinStree::BSTreeInOrderRecursion(BinSTreeNode*bt)
{
	if(bt != NULL)
	{
		BSTreeInOrderRecursion(bt->lchild);
		cout << bt->key << " ";
		BSTreeInOrderRecursion(bt->rchild);
	}
}
void BinStree::BSTreePostOrder(BinSTreeNode* bt)
{
	BinSTreeNode* stack[NUM];
	BinSTreeNode* stack2[NUM];
	int top1 = 0;
	int top2 = 0;
	BinSTreeNode* p = bt, *q;
	if(p == NULL)
		return;
	stack[top1++] = p;
	while (top1 != 0)
	{
		q = stack[--top1];
		stack2[top2++] = q;
		if(q->lchild)
			stack[top1++] = q->lchild;
		if(q->rchild)
			stack[top1++] = q->rchild;
	}
	while (top2 != 0)
	{
		p = stack2[--top2];
		cout << p->key << " ";
	}
}
void BinStree::BSTreePostOrderRecursion(BinSTreeNode*bt)
{
	if(bt != NULL)
	{
		BSTreePostOrderRecursion(bt->lchild);
		BSTreePostOrderRecursion(bt->rchild);
		cout << bt->key <<" ";
	}
}
//根据前序和中序构造二叉树,其中Len为划分后数组当中的元素个数
BinSTreeNode* BinStree::RebuildBinTreePreIn(int* preOrder, int* midOrder, int len)
{
	if(len <= 0)
		return NULL;
	//先构造根节点
	BinSTreeNode* root = new BinSTreeNode;
	root->key = *preOrder;
	int* pos = midOrder;
	for(; pos < midOrder+len; ++pos)
		if(*pos== *preOrder)
			break;
	int k = pos - midOrder;
	root->lchild = RebuildBinTreePreIn(preOrder+1, midOrder, k);
	root->rchild = RebuildBinTreePreIn(preOrder+k+1, pos+1, len-k-1);
	return root;
}
//后序,中序遍历重构二叉树,len为每次划分之后的数组长度
BinSTreeNode* BinStree::RebuildBinTreeInPost(int* midOrder, int* postOrder, int len)
{
	if(len <= 0)
		return NULL;
	BinSTreeNode* root = new BinSTreeNode;
	root->key = *(postOrder+len-1);
	int* pos = midOrder;
	for(; pos < midOrder+len; ++pos)
		if(*pos == *(postOrder+len-1))
			break;
	int k = pos-midOrder;
	root->lchild = RebuildBinTreeInPost(midOrder, postOrder, k);
	root->rchild = RebuildBinTreeInPost(pos+1, postOrder+k, len-k-1);
	return root;
}
int main()
{
	int a[NUM]={34, 18, 76, 13, 52, 82, 16, 67, 58, 73,72};
	int i;
	BinStree bst;
	BinSTreeNode *bt = NULL,*p = NULL, *pt = NULL;
	for(i = 0; i< NUM; i++)
	{
		bst.BSTreeInsert(bt, a[i]);
	}
	pt  = bst.BinStreeSearch(bt, 52, p);
	bst.BSTreePreOrder(bt);
    cout << endl;  
    bst.BSTreeDelete( bt, 13 );   //删除无左孩子的情况   
    bst.BSTreePreOrder(bt);  
    cout << endl;  
    bst.BSTreeDelete(bt, 76 );   //删除有左孩子的情况   
    bst.BSTreePreOrder(bt); 
	cout << endl;
	bst.BSTreeInOrder(bt);
	cout<<endl;
	bst.BSTreePostOrder(bt);
    cout << endl;
	
	int pre[NUM-2]={34, 18, 16, 73, 52, 67, 58, 72, 82};
	int mid[NUM-2]={16, 18, 34, 52, 58, 67, 72, 73, 82};
	int post[NUM-2]={16,18, 58, 72, 67, 52, 82, 73, 34};
	BinSTreeNode* root = NULL;
	root = bst.RebuildBinTreePreIn(pre, mid, NUM-2);
	root = bst.RebuildBinTreeInPost(mid, post, NUM-2);
	bst.BSTreePreOrder(root);
	cin >> i;
    return 0;  
}


你可能感兴趣的:(二叉树)