算法学习-求最大二查搜索子树

题目:

给定某二叉树,计算它的最大二查搜索子树。返回该最大二查搜索子树的根节点。

规定:如果某子树拥有更多的节点,则该子树更大。一颗树的子树,指以某节点为根的所有节点。

如下图的二叉树,返回81.


题目解析:

若某节点的左右子树都是二查搜索树,且能够计算该节点左子树的最大值max和右子树的最小值min,记该节点的值为value

若value>max且value<min,则该结点形成了更大的二查搜索树。

代码如下

// suanfaxuexi.cpp : 定义控制台应用程序的入口点。
//

#include "stdafx.h"
#include <vector>
#include <iostream>

typedef struct tagSTreeNode  
{  
	int value;  
	tagSTreeNode* pLeft;  
	tagSTreeNode* pRight;  

	tagSTreeNode(int v) : value(v), pLeft(NULL), pRight(NULL){}  
}STreeNode;  

typedef void (*VISIT)(STreeNode* value);  // 定义一个  

class CBinaryTree  
{  
private:  
	STreeNode* m_pRoot;  

private:  
	void Destory(STreeNode* pRoot);  
	bool _Insert(STreeNode*& pRoot, int value);                  // 递归  
	bool _Insert2(int value);                                    // 非递归  
	void _PreOrder(STreeNode* pRoot, VISIT Visit) const;         // 递归  
	void _PreOrder2(VISIT Visit) const;                          // 非递归  
	void _InOrder(STreeNode* pRoot, VISIT Visit) const;          // 递归  
	void _InOrder2(VISIT Visit) const;                           // 非递归  
	void _InOrder3(VISIT Visit) const;                           // 非递归  
	void _PostOrder(STreeNode* pParent, VISIT Visit) const;      // 递归  
	void _PostOrder2(VISIT Visit) const;                         // 非递归  
	void DeleteChildless(STreeNode* pParent, STreeNode* pNode);  // pNode无子  
	void DeleteSingleSon(STreeNode* pParent, STreeNode* pNode);  // pNode是pParent唯一子节点  
	bool _LargestBST(STreeNode* pRoot, int& nMin, int& nMax, int& count, int& nNumber, STreeNode*& pNode) const;
public:  
	CBinaryTree();  
	~CBinaryTree();  
	bool Insert(int value);  
	bool Delete(int value);  
	STreeNode* Find(int value) const;  
	void PreOrder(VISIT Visit) const;  
	void InOrder(VISIT Visit) const;  
	void PostOrder(VISIT Visit) const;  

	int LargestBST(STreeNode*& pNode) const;
	
}; 

int CBinaryTree::LargestBST(STreeNode*& pNode) const
{
	int nMin, nMax, count;
	int nNumber = 0;
	_LargestBST(m_pRoot, nMin, nMax, count, nNumber, pNode);
	return nNumber;
}

bool CBinaryTree::_LargestBST(STreeNode* pRoot, int& nMin, int& nMax, int& count, int& nNumber, STreeNode*& pNode) const
{
	count = 0;
	if (!m_pRoot)
		return true;
	int nMin1 = INT_MAX, nMin2 = INT_MAX;
	int nMax1 = INT_MIN, nMax2 = INT_MAX;
	int c1, c2;
	if (!_LargestBST(pRoot->pLeft, nMin1, nMax1, c1, nNumber, pNode))
		return false;
	if (!_LargestBST(pRoot->pRight, nMin2, nMax2, c2, nNumber, pNode))
		return false;
	if ((pRoot->value < nMax1) || (pRoot->value > nMax2))
		return false;
	count = c1 + c2 + 1;
	nMin = std::min(nMin1, pRoot->value);
	nMax = std::max(nMax2, pRoot->value);
	if (count > nNumber)
	{
		nNumber = count;
		pNode = pRoot;
	}

	return true;
}

void PrintValue(STreeNode* pNode)
{
	std::cout<<pNode->value<<'\t';
}

void ChangValue(STreeNode* pNode)
{
	pNode->value = rand() % 100;
}

int _tmain(int argc, _TCHAR* argv[])
{
	CBinaryTree tree;
	for (int i = 0; i < 10; i++)
		tree.Insert(rand() % 100);
	tree.InOrder(ChangValue);

	tree.InOrder(PrintValue);
	tree.PreOrder(PrintValue);

	STreeNode* pNode;
	int nLargestNumber = tree.LargestBST(pNode);
	std::cout<<pNode->value<<'\t'<<nLargestNumber<<std::endl;

	system("pause");
	return 0;
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值