二叉树问题

本文介绍了一种使用C++实现的二叉搜索树的操作方法,包括树的构建、节点插入、中序遍历、镜像翻转及广度优先遍历等核心功能,并通过实例演示了如何求取树中任意两点间的最大距离。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

// mirror_of_search_tree.cpp : 定义控制台应用程序的入口点。
//二叉树的镜像实现,//
//二叉树的广度优先遍历//
//队列//
#include "stdafx.h"
#include "iostream"
#include "queue"
using namespace std;

struct BSTnode
{
	int data;
	int MaxLeft;
	int MaxRight;
	int maxLength;
	BSTnode* left;
	BSTnode* right;
};

template<typename T>
struct QueueNode
{
	T data;
	QueueNode* next;
};
template<typename T>
class Queue//first dequeue,last enqueue//队列//
{
public:
	Queue()
	{
		first = NULL;
		last = NULL;
	}
	~Queue()
	{
		first = NULL;
		last = NULL;
	}
	bool isEmpty()
	{
		return (first == NULL);
	}
	void enqueue(T data)
	{
		QueueNode<T>* newnode;
		newnode = new QueueNode<T>;
		newnode->data = data;
		newnode->next = NULL;
		if (first == NULL)
		{
			first= newnode;
			last = newnode;
		}
		else
		{
			last->next = newnode;
			last = newnode;
		}
	}
	T dequeue()
	{
		T x;
		QueueNode<T>*p;
		if(first == NULL)
			cout<<"empty"<<endl;
		else
		{
			x = first->data;
			p = first;
			if(first == last)
			{
				first = NULL;
				last = NULL;
			}
			else
			{
				first = first->next;
				delete p;
			}
		}
		return x;
	}
private:
	QueueNode<T>*first;
	QueueNode<T>*last;
};



class BSTtree
{
public:
	BSTtree();
	~BSTtree();
	BSTtree(const BSTtree&);
	void init_tree();
	void insert_item(const int & data);
	void print();
	void mirror_of_search_tree();
	void print_breadthFirst();
	int max_lenth_of_tree();//树中两个节点的最大距离//

private:
	void mirror(BSTnode*);
	void print_inorder(BSTnode*p);
	int max_lenth(BSTnode*pRoot);
	void swap(BSTnode**l,BSTnode**s)
	{
		BSTnode*temp = NULL;
		temp = *l;
		*l = *s;
		*s = temp;
	}
	BSTnode* root;
	int MaxLenth_in_node;
};
BSTtree::BSTtree()
{
	root = NULL;
	MaxLenth_in_node = 0;
}
BSTtree::~BSTtree()
{
	delete root;
	root = NULL;
}
void BSTtree::init_tree()
{
	root = NULL;
}
void BSTtree::insert_item(const int & data)//插入//
{
	BSTnode* newnode = new BSTnode;
	newnode->data = data;
	newnode->left = NULL;
	newnode->right = NULL;
	if (root == NULL)
		root = newnode;
	else 
	{
		BSTnode* p = root;
		BSTnode* trail = p;
		while (p != NULL)
		{
			if(data < p->data)
			{
				trail = p;
				p = p->left;
			}
			else if(data > p->data)
			{
				trail = p;
				p = p->right;
			}
		}
		if(trail->data > data)
			trail->left = newnode;
		else 
			trail->right = newnode;
		//cout<<root->data<<" d"<<endl;
	}
}
void BSTtree::print_inorder(BSTnode*root1)//中序遍历//
{
	BSTnode* p = root1;
	if(p != NULL)
	{
		print_inorder(p->left);
		cout<<p->data<<" ";
		print_inorder(p->right);
	}
	
}
void BSTtree::print()
{
	print_inorder(root);
}
void BSTtree::mirror(BSTnode*p)//二叉树镜像//
{
	if(p == NULL)
		return ;
	else 
	{
		swap(&(p->left),&(p->right));
		mirror(p->left);
		mirror(p->right);
	}
}
void BSTtree::mirror_of_search_tree()
{
	mirror(root);
}
void BSTtree::print_breadthFirst()//广度优先遍历//
{
	Queue<BSTnode*> queue;
	BSTnode* p = root;
	if (p != NULL)
	{
		queue.enqueue(p);
		while (!queue.isEmpty())
		{
			p = queue.dequeue();
			cout<<p->data<<" ";
			if(p->left != NULL)
				queue.enqueue(p->left);
			if(p->right != NULL)
				queue.enqueue(p->right);
		}
	}
}

int BSTtree::max_lenth(BSTnode*pRoot)
{
	if (pRoot == NULL)
		return 0;

	if (pRoot->left == NULL)
		pRoot->MaxLeft = 0;
	else 
		pRoot->MaxLeft = 1+ max_lenth(pRoot->left);

	if (pRoot->right == NULL)
		pRoot->MaxRight = 0;
	else
		pRoot->MaxRight = 1 + max_lenth(pRoot->right);

	if (pRoot == root)
	{
		return pRoot->MaxLeft + pRoot->MaxRight;
	}
	else
	{
		pRoot->maxLength =( (pRoot->MaxLeft > pRoot->MaxRight)? pRoot->MaxLeft:pRoot->MaxRight);
		return pRoot->maxLength;
	}
}
int BSTtree::max_lenth_of_tree()//树中任意两个节点之间的最大距离//
{
	//cout<<"max_lenth_of "<<root->data<<endl;
	MaxLenth_in_node = max_lenth(root);
	return MaxLenth_in_node;
}

void main()
{
	BSTtree tree;
	tree.init_tree();
	tree.insert_item(10);
	tree.insert_item(5);
	tree.insert_item(15);
	tree.insert_item(6);
	tree.insert_item(20);
	tree.insert_item(13);
	tree.insert_item(2);
	tree.insert_item(1);
	//tree.print_breadthFirst();
	//tree.mirror_of_search_tree();
	tree.print();
	cout<<endl;
	cout<<tree.max_lenth_of_tree()<<endl;
}


 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值