c++实现二叉树及笔试题(1)

1.创建二叉树
2.前序遍历<递归与非递归>
3.中序遍历<递归与非递归>
4.后序遍历<递归与非递归>
5.层次遍历 

6.获得节点叶子的个数
7.二叉树获得的高度
8.二叉树交换的左右儿子
9.求两个节点pNode1和pNode2在以[R为树根的树中的最近公共祖先
10.判断一个节点t是否在以r为根的子树中
11.求两个节点的最近公共祖先;
12.求二叉树中最远的两个节点的距离;即求宽度
BinaryTree.h

//#pragma once
#ifndef  _BINARYTREE_H
#define _BINARYTREE_H

#include<iostream>
#include<queue>
#include<stack>
#include<deque>
using namespace std;
template <class T>
class BinTreeNode
{
public:
	BinTreeNode() :left(NULL), right(NULL) {}
	BinTreeNode(T v) :left(NULL), right(NULL), data(v) {}
	~BinTreeNode(){}


public:
	T data;
	BinTreeNode<T> *left, *right;
};

template <class T>
class BinaryTree
{
public:
	BinaryTree(T v) :root(NULL),flag(v) {}
public:
	void CreateBinTree(const T *&str)
	{
		CreateBinTree(root, str);
	}
	//树的叶子结点数
	int LeafSize()const { return LeafSize(root); }
	//树的结点数
	int size() { return size(root); }
	//树的高度
	int height() { return height(root); }
	//树的高度
	int Weight() { return Weight(root); }
	/*********************采用递归*********************************************/
	void PreOrder() const{PreOrder(root);}//前序遍历
	void InOrder()const {InOrder(root);}//中序遍历
	void PostOrder()const {PostOrder(root);}//后序遍历
	void LeveLOrder() const { LeveLOrder(root); }//层遍历
	/*********************采用非递归*******************************************/
	void Non_Recursive_PreOrder()const { Non_Recursive_PreOrder(root); }
	void Non_Recursive_InOrder() const { Non_Recursive_InOrder(root); }
	void Non_Recursive_PostOrder()const { Non_Recursive_PostOrder(root); }
	/*************************************************************************/
	
	//判断值是否存在在节点中
	bool IsInTree(const T&key)
	{
		return IsInTree(root, key);
	}
	//交换左右子数
	void SwapLeftRightChild() { SwapLeftRightChild(root); }
	//求两个结点的最近的父节点
	BinTreeNode<T>* CommonParent(const T &x, const T &y)
	{
		BinTreeNode<T> *px = Find(root, x);
		BinTreeNode<T> *py = Find(root, y);
		if (px == NULL || py == NULL)
		{
			return NULL;
		}
		return CommonParent(root, px, py);
	}
	//查找结点
	BinTreeNode<T>* Find(const T &key)
	{
		return Find(root, key);
	}
	//求一个结点的父结点
	BinTreeNode<T>*Parent(const T &key)
	{
		BinTreeNode<T> *p = Find(root, key);
		if (p == NULL)
			return NULL;
		return Parent(root, p);
	}
protected:
	int Weight(BinTreeNode<T> *t)const
	{
		if (t == NULL)
			return 0;
		deque<BinTreeNode<T> *> Q1, Q2;

		Q1.push_back(t);
		int max_weight = 1;
		int tmp_weight = 0;
		BinTreeNode<T> *p;
		while (!Q1.empty())
		{
			while (!Q1.empty())
			{
				p = Q1.front();
				Q1.pop_front();
				if (p->left != NULL)
					Q2.push_back(p->left);
				if (p->right != NULL)
					Q2.push_back(p->right);
			}
			tmp_weight = Q2.size();
			if (tmp_weight > max_weight)
			max_weight = tmp_weight;
			Q1 = Q2;
			Q2.clear();
		}
		return max_weight;
	}
	int LeafSize(BinTreeNode<T> *t)const
	{
		if (t == NULL)
			return 0;
		if (t->left == NULL && t->right == NULL)
			return 1;
		else
			return LeafSize(t->right) + LeafSize(t->right);

	}
	int size(BinTreeNode<T> *t )
	{
		if (t == NULL)
			return 0;
		else
			return size(t->left) + size(t->right) + 1;
	}
	int height(BinTreeNode<T> *root)
	{
		if (root == NULL)
			return 0;
		else
		{
			int m = height(root->left);
			int n = height(root->right);
			return (m > n ? m : n) + 1;
		}
	}
	BinTreeNode<T>* CommonParent(BinTreeNode<T>*t, BinTreeNode<T> *x, BinTreeNode<T> *y)
	{
		if (t == NULL)
			return NULL;
		if (IsInTree(x, y->data))return x;
		if (IsInTree(y, x->data))return y;
		BinTreeNode<T> *left, *right, *another_left, *another_right;
		left = IsInTree(t->left, x->data);
		right = IsInTree(t->right, y->data);
		another_left = IsInTree(t->left, y->data);
		another_right = IsInTree(t->right, x->data);

		if ((left&&right) || (another_right&&another_left))
			return t;
		else if (left&&another_left)
			return CommonParent(t->left, x, y);
		else if (right &&another_right)
			return CommonParent(t->right, x, y);
		else
			return NULL;
	}
	BinTreeNode<T>* Find(BinTreeNode<T> *t, const T &key)
	{
		if (t == NULL)
			return NULL;
		if (t->data == key)
			return t;
		BinTreeNode<T> *p = Find(t->left, key);
		if (p != NULL)
			return p;
		p = Find(t->right, key);
		return p;
	}
	BinTreeNode<T>*Parent(BinTreeNode<T> *t,BinTreeNode<T> *p)
	{
		if (t == NULL||p==t)
			return NULL;
		if (t->left == p || t->right == p)
			return t;
		else
		{
			BinTreeNode<T> *tmp = Parent(t->left,p);
			if (tmp != NULL)
				return tmp;
			return Parent(t->right, p);
		}
	}
	BinTreeNode<T>* IsInTree(BinTreeNode<T> *t, const T &key)
	{
		BinTreeNode<T>*p = Find(t, key);
		if (p != NULL)
			return p;
		else
			return NULL;
	}
	void SwapLeftRightChild(BinTreeNode<T> *&t)
	{
		if (t == NULL || (t->left == NULL && t->right == NULL))
			return;
		BinTreeNode<T> *ptmp = t->left;
		t->left = t->right;
		t->right = ptmp;
		SwapLeftRightChild(t->left);
		SwapLeftRightChild(t->right);
	}
	//////////////////////////////////////////////////////////////////////////
	void Non_Recursive_PreOrder(BinTreeNode<T> *t)const
	{
		
		if (t != NULL)
		{
			stack<BinTreeNode<T> *> s;
			s.push(t);
			BinTreeNode<T> *p;
			while (!s.empty())
			{
				p = s.top();
				s.pop();
				cout << p->data << " ";
				if (p->right != NULL)
					s.push(p->right);
				if (p->left != NULL)
					s.push(p->left);
			}
		}
	}
	void Non_Recursive_InOrder(BinTreeNode<T> *t)const
	{
		stack<BinTreeNode<T> *>s;
		BinTreeNode<T> *temp = t;
		while (temp || !s.empty())
		{
			while (temp)  //有右边就要右边左子树入栈
			{
				s.push(temp);
				temp = temp->left;
			}
			temp = s.top();
			cout << temp->data << " ";
			s.pop();
			temp = temp->right;
		}
	}
	void Non_Recursive_PostOrder(BinTreeNode<T> *t)const
	{
		stack<BinTreeNode<T> *>s;
		BinTreeNode<T> *temp = root, *pre = NULL;
		while (temp || !s.empty())
		{
			while (temp)
			{
				s.push(temp);
				temp = temp->left;
			}
			temp = s.top();
			if (!temp->right || temp->right == pre)//判断无右边
			{
				cout << temp->data << " ";
				s.pop();
				pre = temp;
				temp = NULL;
			}
			else
				temp = temp->right;
		}
	}
	///////////////////////////////////////////////////////////////////////////
	void LeveLOrder(BinTreeNode<T> *t)const
	{
		if (t != NULL)
		{
			queue<BinTreeNode<T>*> Q;
			BinTreeNode<T> *p;
			Q.push(t);
			while (!Q.empty())
			{
				p = Q.front();
				Q.pop();
				cout << p->data << " ";
				if (p->left != NULL)
					Q.push(p->left);
				if (p->right != NULL)
					Q.push(p->right);
			}
		}
	}
	void PreOrder(BinTreeNode<T> *t)const
	{
		if (t != NULL)
		{
			cout << t->data << " ";
			PreOrder(t->left);
			PreOrder(t->right);
		}
	}
	void InOrder(BinTreeNode<T> *t)const
	{
		if (t != NULL)
		{
			InOrder(t->left);
			cout << t->data << " ";
			InOrder(t->right);
		}
	}
	void PostOrder(BinTreeNode<T> *t)const
	{
		if (t != NULL)
		{
			PostOrder(t->left);
			PostOrder(t->right);
			cout << t->data << " ";
		}
	}
	void CreateBinTree(BinTreeNode<T> *&t, const T *&str)
	{
		if (*str == flag || *str == '\0')
		{
			t = NULL;
		}
		else
		{
			t = new BinTreeNode<T>(*str);
			CreateBinTree(t->left, ++str);
			CreateBinTree(t->right, ++str);
		}
	}
private:
	T flag;
	BinTreeNode<T>* root;
};
#endif // ! _BINARYTREE_h

main.cpp中

#include"BinaryTree.h"
#include<stdio.h>
int main()
{
	BinaryTree<char> bt('#');
	const char *str = "ABC##DE##F##G#H##";
	bt.CreateBinTree(str);
	/*采用递归方法*/
	bt.PreOrder();    cout << endl;//前序遍历
	bt.InOrder();     cout << endl;//中序遍历
	bt.PostOrder();   cout << endl;//后序遍历
    /*采用非递归方法*/
	bt.Non_Recursive_PreOrder();    cout << endl;//前序遍历
	bt.Non_Recursive_InOrder();     cout << endl;//中序遍历
	bt.Non_Recursive_PostOrder();   cout << endl;//后序遍历
	bt.SwapLeftRightChild();
	bt.PostOrder(); cout << endl;
	BinTreeNode<char>*pt = bt.CommonParent('E','G');
	cout << "size:" << bt.size() << endl;
	cout << "height:" << bt.height() << endl;
	cout << "LeafSize:" << bt.LeafSize() << endl;
	cout << "Weight:" << bt.Weight() << endl;
	return 0;
}

猜你喜欢

转载自blog.csdn.net/zzb2019/article/details/81542540
今日推荐