二叉树

定义:

二叉树的每个结点至多只有二棵子树(不存在度大于2的结点),二叉树的子树有左右之分,次序不能颠倒。

二叉树的第i层至多有2^(i-1)个结点;

深度为k的二叉树至多有2^k-1个结点;

对任何一棵二叉树T,如果其终端结点数为n0,度为2的结点数为n2,则n0=n2+1

深度=层数-1

 

定义树的结构:

typedef struct BtNode
{
	BtNode* leftchild;
	BtNode* rightchild;
	ElemType data;
}BtNode,*BinaryTree;

先序遍历:

void preOrder(BtNode* p)
{
	if(p!=NULL)
	{
		cout<<p->data<<" ";
		preOrder(p->leftchild);
		preOrder(p->rightchild);
	}
}

//非递归的先序遍历
void nicePreOrder(BtNode* ptr)
{
	if(ptr==NULL)
		return;

	stack<BtNode*> st;
	st.push(ptr);
	while(!st.empty())
	{
		ptr=st.top();
		st.pop();
		cout<<ptr->data<<" ";
		if(ptr->rightchild!=NULL)
		{
			st.push(ptr->rightchild);
		}
		if(ptr->leftchild!=NULL)
		{
			st.push(ptr->leftchild);
		}
	}
	cout<<endl;
}

中序遍历:

void inOrder(BtNode* p)
{
	if(p!=NULL)
	{
		inOrder(p->leftchild);
		cout<<p->data<<" ";
		inOrder(p->rightchild);
	}
}

//非递归的中序遍历
void niceInOrder(BtNode* ptr)
{
	if(ptr==NULL)
		return;

	stack<BtNode*> st;
	while(ptr!=NULL || !st.empty())
	{
		while(ptr!=NULL)
		{
			st.push(ptr);
			ptr=ptr->leftchild;
		}
		ptr=st.top();
		st.pop();
		cout<<ptr->data<<" ";
		ptr=ptr->rightchild;
	}
	cout<<endl;
}

后序遍历:

void pastOrder(BtNode* p)
{
	if(p!=NULL)
	{
		pastOrder(p->leftchild);
		pastOrder(p->rightchild);
		cout<<p->data<<" ";
	}
}

//非递归的中序遍历
void nicePastOrder(BtNode* ptr)
{
	if(ptr==NULL)
		return;
	
	BtNode * tag=NULL;
	stack<BtNode*> st;
	while(ptr!=NULL || !st.empty())
	{
		while(ptr!=NULL)
		{
			st.push(ptr);
			ptr=ptr->leftchild;
		}
		ptr=st.top();
		st.pop();
		if(ptr->rightchild==NULL || ptr->rightchild==tag)
		{
			cout<<ptr->data<<" ";
			tag=ptr;
			ptr=NULL;
		}
		else
		{
			st.push(ptr);
			ptr=ptr->rightchild;
		}
	}
	cout<<endl;
}

层次遍历:(用到队列,先把根结点入队,每出一个结点就将它的左右孩子入队

//层次打印
void levelOrder(BtNode* p)
{
	if(p==NULL) return;
	queue<BtNode*> qu;
	qu.push(p);
	while(!qu.empty())
	{
		p=qu.front();
		cout<<p->data<<" ";
		if(p->leftchild!=NULL)
			qu.push(p->leftchild);
		if(p->rightchild!=NULL)
			qu.push(p->rightchild);
	}
	cout<<endl;
}

查找:(先找左子树,如果没找到,再去找右边

BtNode* findValue(BtNode* ptr,ElemType x)
{
	if(ptr==NULL || ptr->data==x)
		return ptr;
	else
	{
		BtNode* p=findValue(ptr->leftchild,x);
		if(p==NULL)
		{
			p=findValue(ptr->rightchild,x);
		}
		return p;
	}
}

求结点数:(左右子树结点数加一

int size(BtNode* ptr)
{
	if (ptr==NULL)
		return 0;
	else
		return size(ptr->leftchild)+size(ptr->rightchild)+1;
}

求深度:(左右子树深度较大值加一

int depth(BtNode* ptr)
{
	if(ptr==NULL)
		return 0;
	else
		return Max(depth(ptr->leftchild),depth(ptr->rightchild))+1;
}

判断一棵树是否为满二叉树(每一层结点数目都达到最大值

bool Is_Full_BinaryTree1(BtNode* ptr)
{
	if(ptr==NULL) return true;
	int dep=depth(ptr);
	int s=size(ptr);
	if(s==(int)pow(2,dep)-1)
		return true;
	else
		return false;
}

 完全二叉树(除了最后一层,其他层结点数都达到最大数目) 

思路:层次遍历,出现一个空结点,则后面的结点都必须是空结点,这棵树才为完全二叉树

删除整棵树:

思路:后序遍历一次,然后删掉当前结点

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值