二叉树的四种遍历遍历以及查找拷贝

#include<iostream>
#include<queue>
using namespace std;

class Node//创建节点
{
public:
	Node(char c):m_value(c),m_left(NULL),m_right(NULL){}//初始化参数
	char m_value;
	Node *m_left;
	Node *m_right;
};
class Tree//创建树
{
public:
	Tree(int flag):m_root(NULL),m_flag(flag){}
	//void Create(Node *&root)
	Node* Create(char *&str)//利用字符串创建二叉树
	{
		if(*str == m_flag)
			return NULL;
		else
		{
			Node *p = new Node(*str);
			p->m_left = Create(++str);
			p->m_right = Create(++str);
			return p;
		}
	}
	void PreOrder(Node *root)//前序遍历
	{
		if(root != NULL)
		{
			cout<<root->m_value<<" ";
			PreOrder(root->m_left);
			PreOrder(root->m_right);
		}
	}
	void InOrder(Node *root)//中序遍历
	{
		if(root != NULL)
		{
			InOrder(root->m_left);
			cout<<root->m_value<<" ";
			InOrder(root->m_right);
		}
	}
	void PostOrder(Node *root)//后序遍历
	{
		if(root != NULL)
		{
			PostOrder(root->m_left);
			PostOrder(root->m_right);
			cout<<root->m_value<<" ";
		}
	}
	int Size(Node *root)//二叉树的大小
	{
		if(root == NULL)
			return 0;
		else
			return Size(root->m_left) + Size(root->m_right)+1;
	}
	int Height(Node *root)//二叉树的高度
	{
		if(root == NULL)
			return 0;
		else
		{
			int m1 = Height(root->m_left);
			int m2 = Height(root->m_right);
			return m1 > m2 ? (m1+1) : (m2+1);
		}
	}
	Node *Search(Node *root,char k)//查找二叉树中的某个节点
	{
		if(root == NULL)
			return NULL;
		if(root->m_value == k)
			return root;
		Node *p = Search(root->m_left,k);
		if(p != NULL)
			return p;
		return Search(root->m_right,k);
	}
	Node *Parent(Node *root,char k)//查找二叉树节点的父亲节点
	{
		/*if(root == NULL || root->m_value == k)
			return NULL;
		Node *p = Search(root,k);
		if(p == NULL)
			return NULL;
		if(root->m_left == p || root->m_right == p)
			return root;
		Node *q = Parent(root->m_left,k);
		if(q == NULL)
			return Parent(root->m_right,k);
		return q;*/
		if(root == NULL ||root->m_value == k)
			return NULL;
		Node *p = root;
		if((p->m_left != NULL && p->m_left->m_value == k) || (p->m_right != NULL && p->m_right->m_value == k))
			return p;
		Node *q = Parent(root->m_left,k);
		if(q != NULL)
			return q;
		return Parent(root->m_right,k);
	}
	void copy(Node*&root1,Node*root)//拷贝二叉树
	{
		if(root == NULL)
			root1 = NULL;
		else
		{
			root1 = new Node(root->m_value);
			copy(root1->m_left,root->m_left);
			copy(root1->m_right ,root->m_right);
		}
	}
	void CengCi(Node*root)//层次遍历
	{
		if(root == NULL)
			return;
		queue<Node*> q;
		Node*temp;
		q.push(root);
		while(!q.empty())
		{
			temp = q.front();
			q.pop();
			cout<<temp->m_value<<" ";
			if(temp->m_left != NULL)
				q.push(temp->m_left);
			if(temp->m_right != NULL)
				q.push(temp->m_right);
		}
		cout<<endl;
	}
	Node *m_root;
	int m_flag;
};

void main()
{
	Tree t('#');
	char *str = "abdg##hi####ce#j##f##";
	t.m_root = t.Create(str);
	t.PreOrder(t.m_root);
	cout<<endl;
	t.InOrder(t.m_root);
	cout<<endl;
	t.PostOrder(t.m_root);
	cout<<endl;
	cout<<"size = "<<t.Size(t.m_root)<<endl;
	cout<<"height = "<<t.Height(t.m_root)<<endl;
	Node *q = t.Search(t.m_root,'f');
	if(q != NULL)
	{
		cout<<q<<endl;
		cout<<q->m_value<<endl;
	}
	else
		cout<<"查找的值不存在"<<endl;
	Node *a = t.Parent(t.m_root,'i');
	if(a != NULL)
	{
		cout<<a->m_value<<endl;
	}
	Tree t1('#');
	t1.copy(t1.m_root,t.m_root);
	t1.PreOrder(t1.m_root);
	cout<<endl;
	t1.InOrder(t1.m_root);
	cout<<endl;
	t1.PostOrder(t1.m_root);
	cout<<endl;
	t.CengCi(t.m_root);
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值