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

来源:互联网 发布:时尚杂志软件 编辑:程序博客网 时间:2024/05/16 09:54

题目:

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

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

如下图的二叉树,返回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
原创粉丝点击