算法学习-求最大二查搜索子树
来源:互联网 发布:时尚杂志软件 编辑:程序博客网 时间: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
- 算法学习-求最大二查搜索子树
- 最大二叉搜索子树
- 最大二叉搜索子树
- 最大二叉搜索子树问题
- 最大二叉搜索子树练习
- hdu 1856 并查集 求最大的子树含有元素的个数
- 算法学习二----求子数组的最大和
- 二叉树10:最大二叉搜索子树
- 打印普通二叉树最大搜索子树
- 最大二叉搜索子树 后序遍历
- 数据结构与算法分析笔记与总结(java实现)--二叉树10:最大二叉搜索子树练习题
- dinic算法求最大流的学习
- 算法学习---求数组中的最大数
- Microsoft 2016 面试题1 | 最大二叉搜索子树
- 数据结构 找到二叉树中的最大搜索二叉子树
- 找到二叉树中的最大搜索二叉子树
- 找到二叉树中的最大搜索二叉子树
- 找出二叉树中最大的子树,且子树为二叉搜索树
- 外网无法内网FTP (200 Type set to A)
- Codeforces Round 10 A. Gabriel and Caterpillar 模拟
- 【hpuoj】数学问题
- 【转载自amobbs IDzhcj66】FATFS读取数据问题
- poj1222 EXTENDED LIGHTS OUT 开关问题
- 算法学习-求最大二查搜索子树
- WordPress CSV/excel导入插件 WP All Import Pro v4.2.9[正在汉化]
- 常用的DDL语句
- C语言基本数据结构之三(图的广度及深度遍历,求单源最短路径的Dijkstra算法)
- 键盘快捷键
- 广播
- Ibatis基本的增删改查操作
- 【hpuoj】英语问题
- Netty 学习 - EventLoop