二叉树练习题

#pragma once
#include<iostream>
#include<queue>
#include<stack>
#include<assert.h>
using namespace std;
enum PointerTag{THREAD,LINK}; //二叉树线索化需要的
struct BinaryTreeNode
{
	BinaryTreeNode(int data)
		:_data(data)
		,_left(NULL)
		,_right(NULL)
		,_parent(NULL)
		,_leftTag(LINK)
		,_rightTag(LINK)

	{}
	BinaryTreeNode* _left;
	BinaryTreeNode* _right;
	BinaryTreeNode* _parent;
	PointerTag _leftTag; //线索化需要
	PointerTag _rightTag; //线索化
	int _data;
};
class BinaryTree
{
public:
	typedef BinaryTreeNode Node;
	BinaryTree()
		:_root(NULL)
	{}
	BinaryTree(int* a,int len)  //创建树
	{
		int index=0;
		_root=_create(a,index,len);
	}
	BinaryTree(BinaryTree& t)//拷贝构造
	{
		_root=_copy(t._root);
	}
	//BinaryTree& operator=(BinaryTree& t) //赋值
	//{
	//	if( this!= &t)
	//	{
	//		_delete(_root);
	//		_copy(t._root);
	//	}
	//	return *this;
	//}
	BinaryTree& operator=(BinaryTree t)
	{
		swap(_root,t._root);
		return *this;
	}

	//~BinaryTree()
	//{
	//	_delete(_root);
	//	_root=NULL;
	//}
	void Mirror()
	{
		_Mirror(_root);
	}
	void PreOrder()  //前序遍历
	{
		_PreOrder(_root);
	}
	void InOrder()  //中序遍历
	{
		_InOrder(_root);
	}
	void PostOrder()  //后序遍历 
	{
		_PostOrder(_root);
	}
	void PreOrder_NoR() //非递归前序遍历
	{
		if(_root==NULL)
		{
			return ;
		}
		Node* cur=_root;
		stack<Node*> s;
		s.push(cur);
		while(!s.empty())
		{
			Node* root=s.top();
			cout<<root->_data<<"->";
			s.pop();
			if(root->_right)
			s.push(root->_right);
			if(root->_left)
				s.push(root->_left);
		}

	}
	void InOrder_NoR() 
	{
		if(_root==NULL)
		{
			return ;
		}
		Node* cur=_root;
		stack<Node*> s; 
		while( cur||!s.empty())
		{
			while(cur)  //一直压左节点
			{
				s.push(cur);
				cur=cur->_left;
			}
			//取栈顶元素
			//把右节点当做一个树,继续压入左节点
			if(!s.empty())
			{
				Node * root=s.top();
				cout<<root->_data<<"->";
				s.pop();
				cur=root->_right;
			}


		}

	}
	void PostOrder_NonR()
	{
		 stack<Node*> s1;
		 Node* cur=_root;
		  Node* prev=NULL; 
		 while(cur || !s1.empty())
		 {
			 while(cur)
			 {
				 s1.push(cur);
				 cur=cur->_left;
			 }
			 Node* root=s1.top();
			
			 if(root->_right==NULL || root->_right==prev) //右节点为空 或者已经访问过了
			 {
				 cout<<root->_data<<"->";
				 s1.pop();
				 prev=root;
			 }
			 else
			 {
				 cur=root->_right;
			 }
			 
		 } 
	}
	bool ExistBtree(BinaryTree t1)
	{
		return _Exist(_root,t1._root);
	}
	void LevelOrder() //非递归 利用队列 层次遍历
	{
		queue<Node*> q1;
		q1.push(_root);
		while(!q1.empty())
		{
			Node* root=q1.front();
			cout<<root->_data<<"->";
			q1.pop();
			if(root->_left)
			q1.push(root->_left);
			if(root->_right)
				q1.push(root->_right);
		}
	}
	int Size()  //总结点
	{
		int size=0;
		_size(_root,size);
		return size;
	}
	int Height() //求高度
	{
		return _height(_root);
	}
	int LeafNum() //叶子节点
	{
		return _leafnum(_root);
	}
	void PrevOrderThreading() //前序线索化
	{
		Node* prev=NULL;
		_prevorderthreading(_root,prev);
	}
	void PreOrderThd() //前序线索化遍历
	{
		if(_root==NULL)
		{
			return;
		}
		Node* cur=_root;
		while(cur)
		{
			cout<<cur->_data<<"->";
			if(cur->_leftTag==LINK)
			{
				cur=cur->_left;
			}
			else
			{
				cur=cur->_right;
			}
		}
	} 
	void InOrderThreading() //中序化线索
	{
		Node* prev=NULL;
		_InOrderThreading(_root,prev);
	}  
	void InOrderThd()
	{
		Node* cur=_root;
		while(cur)
		{
			while(cur->_leftTag==LINK)
			{
				cur=cur->_left;
			}
			if(cur&&cur->_rightTag!=LINK)
			{
				cout<<cur->_data<<"->";
				cur=cur->_right;
			}
			cout<<cur->_data<<"->";
			cur=cur->_right;
		}
	}
private:
	bool _Exist(BinaryTreeNode* root1,BinaryTreeNode* root2)
	{
		if(root2==NULL)
		{
			return true;
		}
		if(root1==NULL)
		{
			return false;
		}
		if(root1->_data==root2->_data)
		{
			return _Exist(root1->_left,root2->_left)&&_Exist(root1->_right,root2->_right);
		}
		if(root1->_data!=root2->_data)
		{
			if( _Exist(root1->_left,root2)==false)
			{
				return _Exist(root1->_right,root2);

			}
			return true;
		}
	}
	void _InOrderThreading(Node* root,Node*& prev) 
	{
		if(root==NULL)
		{
			return;
		}
		prev=root;
		_InOrderThreading(root->_left,prev);
		if(root->_left==NULL)
		{
			root->_leftTag=THREAD;
			root->_left=prev;

		}
		if(prev&&prev->_right==NULL&&prev!=root)
		{
			prev->_rightTag=THREAD;
			prev->_right=root;
		}
		_InOrderThreading(root->_right,prev);
		
	}
	void _prevorderthreading(Node* root,Node* &prev)
	{
		if(root==NULL)
		{
			return ;
		}
		if(root&&root->_left==NULL)
		{
			root->_leftTag=THREAD;
		}
		if(prev&&prev->_right==NULL)
		{
			prev->_right=root;
			prev->_rightTag=THREAD;
		}
		prev=root;
		if(root->_leftTag==LINK)
		{
			_prevorderthreading(root->_left,prev);
		}
		if(root->_rightTag==LINK)
		{
			_prevorderthreading(root->_right,prev);
		}
	}
	
	int _leafnum(Node* root)
	{
		if(root==NULL)
		{
		  return 0;
		}
		if(root->_left==NULL && root ->_right==NULL)
		{
			return 1;
		}
		return _leafnum(root->_left)+_leafnum(root->_right);
	}
	int _height(Node* root)
	{
		if(root==NULL)
		{
			return 0;
		}
		int left_height=_height(root->_left);
		int right_height=_height(root->_right);
		return left_height>right_height ?left_height+1 :right_height+1;
	}
	void _size(Node* root,int& size)
	{
		if(root==NULL)
		{
			return;
		}
		++size;
		_size(root->_left,size);
		_size(root->_right,size);

	}
	void _delete(Node* root)
	{
		if(root==NULL)
		{
			return;
		}
		_delete(root->_left);
		_delete(root->_right);
		root->_parent=NULL;
		delete root;
	}
	Node* _copy(Node* root)
	{
		if(root==NULL)
		{
			return NULL;
		}
		Node* newRoot=NULL;
		newRoot=new Node(root->_data);
		newRoot->_left=_copy(root->_left);
		if(newRoot->_left!=NULL)
		{
			newRoot->_left->_parent=newRoot;
		}
		newRoot->_right=_copy(root->_right);
		if(newRoot->_right!=NULL)
		{
			newRoot->_right->_parent=newRoot;
		}
		return newRoot;

	}
	Node* _create(int* a,int& index,int len)
	{
		assert(a);
		Node* root=NULL;
		if(a[index]!='#' && index<len)
		{
			root=new Node(a[index]);
			root->_left=_create(a,++index,len);
			if(root->_left!=NULL)
			{
				root->_left->_parent=root;
			}
			root->_right=_create(a,++index,len);
			if(root->_right!=NULL)
			{
				root->_right->_parent=root;
			}

		}
		return root;

	}
	void _PreOrder(Node* root)
	{
		if(root==NULL)
		{
			return;
		}
		cout<<root->_data<<" ->";
		_PreOrder(root->_left);
		_PreOrder(root->_right);
	}
	void _InOrder(Node* root)
	{
		if(root==NULL)
		{
			return;
		}
		
		_InOrder(root->_left);
		cout<<root->_data<<" ->";
		_InOrder(root->_right);

	}
	void _PostOrder(Node* root)
	{
				if(root==NULL)
		{
			return;
		}
		
		_PostOrder(root->_left);
		_PostOrder(root->_right);
		cout<<root->_data<<" ->";
	}
	void _Mirror(Node* root) //二叉树的镜像
	{
		if(root==NULL)
		{
			return;
		}
		if(root->_right || root->_left)
		{
			Node* tmp=root->_left;
			root->_left=root->_right;
			root->_right=tmp;
		}
		if(root->_left)
			_Mirror(root->_left);
		if(root->_right)
			_Mirror(root->_right);
	}
private:
	Node* _root;
};
	BinaryTreeNode* Construct(int* prev,int* mid,int& index,int begin,int end) //根据前序遍历和中序遍历重建树
	{
	   assert(prev);
	   assert(mid);

	   int final=0;
	   for(int i=begin;i<=end;i++)
	   {
		   if(mid[i]==prev[index])
		   {
			   final=i;
			   break;
		   }
	   }
	   BinaryTreeNode* root=new BinaryTreeNode(prev[index]);
	   if(final-1>=0 && begin <= (final-1))
	   root->_left=Construct(prev,mid,++index,begin,final-1);
	   if(final+1<=end)
	   root->_right=Construct(prev,mid,++index, final+1,end);
	   return root;
	}
	void Pre(BinaryTreeNode* root)
	{
		if(root==NULL)
		{
			return;
		}
		cout<<root->_data<<"->";
		Pre(root->_left);
		Pre(root->_right);
	}

#include"BinaryTree.h"
void test1()
{
	int a[]={1, 2, 3, '#', '#', 4, '#', '#', 5, 6};
	int a1[]={6,2,'#',4,'#','#',};
	BinaryTree t1(a,10);
	BinaryTree t2(a1,4);
	//cout<<t1.ExistBtree(t2);
	//int prev[]={1,2,4,7,3,5,6,8};
	//int mid[]={4,7,2,1,5,3,8,6};
	//int index=0;
	//BinaryTreeNode* root=Construct(prev,mid,index,0,7);
 	//Pre(root);
	//t1.Mirror();
	//t1.PreOrder();
	//t1.PreOrder();
	//t1.PreOrder_NoR();
	//t1.InOrder();
	//t1.InOrderThreading();
	//t1.InOrderThd();
   //t1.InOrder_NoR();
	//t1.PostOrder();
	//t1.PostOrder_NonR();
	//t1.PrevOrderThreading();
	//t1.PreOrderThd();
	//t1.LevelOrder();
	//cout<<t1.Size();
	/*cout<<t1.Height();
	cout<<t1.LeafNum();*/
}
int main()
{
	test1();
	system("pause");
	return 0;
}

(1)非递归定义 树(tree)是由n(n≥0)个结点组成的有限集合。n=0的树称为空树;n>0的树T: ① 有且仅有一个结点n0,它没有前驱结点,只有后继结点。n0称作树的根(root)结点。 ② 除结点外n0 , 其余的每一个结点都有且仅有一个直接前驱结点;有零个或多个直接后继结点。 (2)递归定义 一颗大树分成几个大的分枝,每个大分枝再分成几个小分枝,小分枝再分成更小的分枝,… ,每个分枝也都是一颗树,由此我们可以给出树的递归定义。 树(tree)是由n(n≥0)个结点组成的有限集合。n=0的树称为空树;n>0的树T: ① 有且仅有一个结点n0,它没有前驱结点,只有后继结点。n0称作树的根(root)结点。 ② 除根结点之外的其他结点分为m(m≥0)个互不相交的集合T0,T1,…,Tm-1,其中每个集合Ti(0≤i<m)本身又是一棵树,称为根的子树(subtree)。 2、掌握树的各种术语: (1) 父母、孩子与兄弟结点 (2) 度 (3) 结点层次、树的高度 (4) 边、路径 (5) 无序树、有序树 (6) 森林 3、二叉树的定义 二叉树(binary tree)是由n(n≥0)个结点组成的有限集合,此集合或者为空,或者由一个根结点加上两棵分别称为左、右子树的,互不相交的二叉树组成。 二叉树可以为空集,因此根可以有空的左子树或者右子树,亦或者左、右子树皆为空。 4、掌握二叉树的五个性质 5、二叉树的二叉链表存储。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值