二叉树的相关算法(不包含线索二叉树、排序二叉树以及哈弗曼树,后序补上)

#ifndef NEWTREE_H
#define NEWTREE_H
#include<iostream>
using namespace std;

struct TreeNode
{
	char data;
	TreeNode* lchild;
	TreeNode* rchild;
	TreeNode(char d=0):data(d),lchild(0),rchild(0){}
};
const int MAXSIZE=100;
template <class T>
class TreeStack
{
private:
	
	T arr[MAXSIZE];
	int top;
public:
	TreeStack()
	{
		top=-1;
	}
	void push(T item)
	{
		if(top==MAXSIZE-1)
		{
			throw("上溢!");
			return;
		}
		top=top+1;
		arr[top]=item;
	}
	void pop()
	{
		if(top==-1)
		{
			throw("下溢!");
			return ;
		}
		top=top-1;
	}
	T getTop()
	{
		if(top==-1)
		{
			throw("栈中没有数据!");
			return 0;
		}
		return arr[top];
	}
	bool isEmpty()
	{
		return (top==-1)?true:false;
	}
	int getLength()
	{
		return top+1;
	}
};

class TreeQueue
{
private:
	TreeNode* arr[MAXSIZE];
	int first;
	int rare;
public:
	TreeQueue()
	{
		first=-1;
		rare=-1;
	}
	void in(TreeNode* p)
	{
		if(rare==MAXSIZE-1)
		{
			throw("上溢!");
			return;
		}
		rare=rare+1;
		arr[rare]=p;
	}
	void out()
	{
		if(rare==first)
		{
			throw("下溢!");
			return ;
		}
		first=first+1;
	}
	TreeNode* getFirst()
	{
		if(rare==first)
		{
			throw("队中没有数据!");
			return 0;
		}
		return arr[first+1];

	}
	bool isEmpty()
	{
		return (first==rare)?true:false;
	}


};
//建立二叉树
//非递归(广义表),已知非空二叉树采用广义表形式作为输入,根据输入建立二叉树的二叉链表存储结构,例如A(B(D,E(G)),C(F(,H)))@
TreeNode* getTree()
{
	char ch=0;
	int flag=0;
	TreeNode* T=0;
	TreeNode* p=0;
	TreeStack<TreeNode*> stack;
	while(1)
	{
		ch=getchar();
		switch(ch)
		{
		case '@':return T;break;
		case '(':stack.push(p);
			flag=1;
			break;
		case ',':flag=2;
			break;
		case ')':stack.pop();
			break;
		default:
			p=new TreeNode(ch);
			if(T==0)T=p;
			else
				if(flag==1)stack.getTop()->lchild=p;
				else stack.getTop()->rchild=p;

		}

	}
}
//递归前序遍历建立二叉树,ABC  DE  F  G  
void BUILDTREE(TreeNode*& root)
{
	char ch=getchar();
	if(ch==' ')
	{
		root=0;
		return;
	}
	root=new TreeNode(ch);
	BUILDTREE(root->lchild);
	BUILDTREE(root->rchild);

}
//销毁二叉树
//后序遍历递归删除二叉树
void DESTORYBT(TreeNode* root)
{
	if(root!=0)
	{
		DESTORYBT(root->lchild);
		DESTORYBT(root->rchild);
		delete root;
	}
}
void CLEARTREE(TreeNode*& root)
{
	DESTORYBT(root);
	root=0;
}
//后序遍历非递归删除二叉树
void deleteTree(TreeNode*& root)
{
	TreeStack<TreeNode*> stackNode;
	TreeStack<int> stackFlag;
	TreeNode* p=root;
	while(stackNode.isEmpty()==false||p!=0)
	{
		while(p!=0)
		{
			stackNode.push(p);
			stackFlag.push(1);
			p=p->lchild;
		}
		if(stackFlag.getTop()==1)
		{
			stackFlag.pop();
			stackFlag.push(2);
			p=stackNode.getTop()->rchild;
		}
		else
		{
			delete stackNode.getTop();
			stackNode.pop();
			stackFlag.pop();
			p=0;
		}
	}
	root=NULL;
}
//删除指定关键字的结点以及以此结点为根的二叉树
void deleteItem(TreeNode* root,char key)
{
	TreeNode* p=root;
	TreeNode* q=NULL;
	TreeStack<TreeNode*> stack;
	while(stack.isEmpty()==false || p!=NULL)
	{
		while(p!=NULL)
		{
			/*cout<<p->data<<" ";*/
			if(p->data==key)
			{
				if(q->lchild==p)
					q->lchild=NULL;
				else
					q->rchild=NULL;
				TreeStack<TreeNode*> nodeStack;
				TreeStack<int> flagStack;
				while(nodeStack.isEmpty()==false || p!=NULL)
				{
					while(p!=NULL)
					{
						nodeStack.push(p);
						flagStack.push(1);
						p=p->lchild;
					}
					if(flagStack.getTop()==1)
					{
						flagStack.pop();
						flagStack.push(2);
						nodeStack.getTop()->rchild;
					}
					else
					{
						p=nodeStack.getTop();
						nodeStack.pop();
						flagStack.pop();
						delete p;
						p=NULL;
					}
				}
				return ;
			}
			q=p;
			stack.push(p);
			p=p->lchild;
		}
		p=stack.getTop();
		stack.pop();
		q=p;
		p=p->rchild;
	}
}

//前序遍历递归复制二叉树
TreeNode* COPYTREE(TreeNode* root1)
{
	if(root1==NULL)
		return 0;
	else
	{
		TreeNode* root2=new TreeNode(root1->data);
		root2->lchild=COPYTREE(root1->lchild);
		root2->rchild=COPYTREE(root1->rchild);
		return root2;
	}
}

//递归判断两个二叉树是否相似
bool SIMILARTREE(TreeNode* root1,TreeNode* root2)
{
	if(root1==NULL && root2==NULL)
		return true;
	if(root1!=NULL && root2!=NULL && SIMILARTREE(root1->lchild,root2->lchild) && SIMILARTREE(root1->rchild,root2->rchild))
		return true;
	return false;
}
//递归判断两个二叉树是否等价
bool EQUALTREE(TreeNode* root1,TreeNode* root2)
{
	if(root1==NULL && root2==NULL)
		return true;
	if(root1!=NULL && root2!=NULL && root1->data==root2->data && EQUALTREE(root1->lchild,root2->lchild) && EQUALTREE(root1->rchild,root2->rchild))
		return true;
	return false;
}

//求树的深度
//递归求树的深度
int GETDEEP(TreeNode* root)
{
	if(root==0)
		return 0;
	int ldeep=GETDEEP(root->lchild);
	int rdeep=GETDEEP(root->rchild);
	return (ldeep>rdeep)?1+ldeep:1+rdeep;
}
//非递归中序遍历求树的深度
int getDeep(TreeNode* root)
{
	TreeStack<TreeNode*> stackNode;
	TreeStack<int> stackLevel;
	TreeNode* p=root;
	int maxLevel=0;
	int currentLevel=0;
	while(stackNode.isEmpty()==false || p!=NULL)
	{
		while(p!=NULL)
		{
			++currentLevel;
			stackNode.push(p);
			stackLevel.push(currentLevel);
			p=p->lchild;

		}
		p=stackNode.getTop();
		currentLevel=stackLevel.getTop();
		stackNode.pop();
		stackLevel.pop();
		if(p->lchild==NULL && p->rchild==NULL)
			maxLevel=(currentLevel>maxLevel)?currentLevel:maxLevel;
		p=p->rchild;
	}
	return maxLevel;
}

//非递归后序遍历查找结点所在的层次,遍历过程中访问到一个结点就半段该结点是否为满足条件的结点,若是满足条件的结点,则此时栈中保留原色的个数加1即为该结点的层次数
int getLevel(TreeNode* root,char key)
{
	TreeStack<TreeNode*> nodeStack;
	TreeStack<int> flagStack;
	TreeNode* p=root;
	while(nodeStack.isEmpty()==false || p!=NULL)
	{
		while(p!=NULL)
		{
			nodeStack.push(p);
			flagStack.push(1);
			p=p->lchild;
		}
		if(flagStack.getTop()==1)
		{
			flagStack.pop();
			flagStack.push(2);
			p=nodeStack.getTop()->rchild;
		}
		else
		{
			flagStack.pop();
			p=nodeStack.getTop();
			nodeStack.pop();
			if(p->data==key)
				return nodeStack.getLength()+1;
			p=NULL;
		}
	}
	return -1;
}


//求树的叶子结点数
//递归求树的叶子结点数
int GETLEAF(TreeNode* root)
{
	if(root==0)
		return 0;
	if(root->lchild==0&&root->rchild==0)
		return 1;
	return GETLEAF(root->lchild)+GETLEAF(root->rchild);
}
//非递归前序遍历求树的叶子结点树
int getLeaf(TreeNode* root)
{
	TreeNode* p=root;
	TreeStack<TreeNode*> stack;
	int count=0;
	while(stack.isEmpty()==false || p!=NULL)
	{
		while(p!=NULL)
		{
			if(p->lchild==NULL && p->rchild==NULL)
				++count;
			stack.push(p);
			p=p->lchild;
		}
		p=stack.getTop();
		stack.pop();
		p=p->rchild;
	}
	return count;
}

//树的遍历
//递归遍历
//递归前序遍历
void PREORDER(TreeNode* root)
{
	if(root==0)
		return ;
	cout<<root->data<<" ";
	PREORDER(root->lchild);
	PREORDER(root->rchild);
}
//递归中序遍历
void INORDER(TreeNode* root)
{
	if(root==0)
		return ;
	INORDER(root->lchild);
	cout<<root->data<<" ";
	INORDER(root->rchild);
}
//递归后序遍历
void POSTORDER(TreeNode* root)
{
	if(root==0)
		return;
	POSTORDER(root->lchild);
	POSTORDER(root->rchild);
	cout<<root->data<<" ";
}

//非递归遍历
//非递归前序遍历
void preOrder(TreeNode* root)
{
	TreeStack<TreeNode*> stack;
	TreeNode* p=root;
	while(stack.isEmpty()==false||p!=0)
	{
		while(p!=0)
		{
			cout<<p->data<<" ";
			stack.push(p);
			p=p->lchild;
		}
		p=stack.getTop()->rchild;
		stack.pop();
	}

}

//非递归中序遍历
void inOrder(TreeNode* root)
{
	TreeStack<TreeNode*> stack;
	TreeNode* p=root;
	while(stack.isEmpty()==false||p!=0)
	{
		while(p!=0)
		{
			stack.push(p);
			p=p->lchild;
		}
		cout<<stack.getTop()->data<<" ";
		p=stack.getTop()->rchild;
		stack.pop();
		
	}
}

//非递归后序遍历
void postOrder(TreeNode* root)
{
	TreeStack<TreeNode*> stackNode;
	TreeStack<int> stackFlag;
	TreeNode* p=root;
	while(stackNode.isEmpty()==false||p!=0)
	{
		while(p!=0)
		{
			stackNode.push(p);
			stackFlag.push(1);
			p=p->lchild;
		}
		if(stackFlag.getTop()==1)
		{
			stackFlag.pop();
			stackFlag.push(2);
			p=stackNode.getTop()->rchild;
		}
		else
		{
			cout<<stackNode.getTop()->data<<" ";
			stackNode.pop();
			stackFlag.pop();
			p=0;
		}
	}
}
//非递归层次遍历
void levelOrder(TreeNode* root)
{
	TreeQueue queue;
	TreeNode* p=0;
	if(root==0)return ;
	queue.in(root);

	while(queue.isEmpty()==false)
	{
		p=queue.getFirst();
		cout<<p->data<<" ";		
		if(p->lchild!=0) queue.in(p->lchild);
		if(p->rchild!=0) queue.in(p->rchild);	
		queue.out();
	}
}

//交换二叉树中的所有结点
void exchangeTree(TreeNode* root)
{
	TreeQueue queue;
	TreeNode* p=root;
	TreeNode* temp=NULL;
	if(root==NULL)return ;
	queue.in(p);
	while(queue.isEmpty()==false)
	{
		p=queue.getFirst();
		queue.out();
		temp=p->lchild;
		p->lchild=p->rchild;
		p->rchild=temp;
		if(p->lchild!=NULL) queue.in(p->lchild);
		if(p->rchild!=NULL) queue.in(p->rchild);

	}
}

#endif

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值