无耻的我又来求助了..

ls1991lsok ls1991lsok 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区
#ifndef TREE_NODE_H
#define TREE_NODE_H

/**
Forward declaration
*/
template <typename T>
class BSTree;

template <typename T>
class TreeNode;

/**
StackNode Interface
*/
template <typename T>
class TreeNode {
public:
inline TreeNode( const T& nData ) : data(nData) , left(0) , right(0) {} // <-- Do nothing.
inline T get_value() const { return data; }

friend class BSTree<T>;
private:
T data;
TreeNode<T>* left;
TreeNode<T>* right;
};

#endif


#ifndef HOMEMADE_BST_H
#define HOMEMADE_BST_H

#include "tree_node.h"
#include <iostream>
#include <cstdlib>
#include <vector>

using std::ostream;
using std::vector;
using std::endl;
using std::size_t;

/**
Function declarations
*/
template <typename T>
ostream& operator<<( ostream&, const BSTree<T>& );

/**
Stack Interface
*/
template <typename T>
class BSTree {
public:
/**
Other member functions
*/
inline bool is_empty() const
{ return ( root == 0 ); }
inline TreeNode<T>* get_root() const
{ return root; }

inline TreeNode<T>* find_maximum( TreeNode<T>* pos ) const
{ while ( pos->right ) pos = pos->right; return pos; }
inline TreeNode<T>* find_minimum( TreeNode<T>* pos ) const
{ while ( pos->left ) pos = pos->left; return pos; }
inline TreeNode<T>* find_maximum() const
{ return find_maximum(root); }
inline TreeNode<T>* find_minimum() const
{ return find_minimum(root); }

TreeNode<T>* next_in_order( TreeNode<T>* pos ) const;

bool operator==( const BSTree<T>& b ) const ;
inline bool operator!=( const BSTree<T>& b ) const
{ return !( *this == b );}

void pre_order( TreeNode<T>* localRoot , ostream& out ) const;
void pre_order( ostream& out ) const
{ pre_order(root, out); }
void pre_order_push( TreeNode<T>* localRoot , vector<T>& v ) const;

void to_vector( TreeNode<T>* localRoot , vector<T>& v ) const ;
void to_vector( vector<T>& v ) const
{ to_vector(root, v); }

private:
// Common statements in: copy constr. & operator=
void deep_copy( TreeNode<T>* nodeInSourceTree );

TreeNode<T>* root;
};

template <typename T>
TreeNode<T>* BSTree<T>::next_in_order( TreeNode<T>* pos ) const {
// case 1
if ( pos->right )
return find_minimum(pos->right);

// case 2
TreeNode<T>* parentNode = get_parent(pos);
while ( parentNode ){
// If moving right (i.e., pos is the left child), done.
if ( parentNode->left == pos )
return parentNode;

// If not, keep moving up
pos = parentNode;
parentNode = get_parent(parentNode);
}

return 0; // <-- Only reach this part if there is no next in order.
}

template <typename T>
void BSTree<T>::pre_order( TreeNode<T>* localRoot , ostream& out ) const {
if ( localRoot ){
out << localRoot->data << " ";
pre_order(localRoot->left,out);
pre_order(localRoot->right,out);
}
return;
}

template <typename T>
void BSTree<T>::deep_copy( TreeNode<T>* nodeInSourceTree ){
if ( nodeInSourceTree ){
insert( nodeInSourceTree->data );
deep_copy( nodeInSourceTree->left );
deep_copy( nodeInSourceTree->right );
}
return;
}



/** 
THIS IS THE PLACE WHERE YOU SHOULD PLACE YOUT CODE
*/

template <typename T>
void BSTree<T>::pre_order_push( TreeNode<T>* localRoot , vector<T>& v ) const {
if ( localRoot ){
v.push_back(localRoot->data);
pre_order_push(localRoot->left,v);
pre_order_push(localRoot->right,v);
}
return;
}

template <typename T>
bool BSTree<T>::operator==( const BSTree<T>& param1 ) const {
TreeNode<T>* localRoot = this->root;
TreeNode<T>* param1Root = param1.get_root();
TreeNode<T>* localMin = find_minimum(this->root);
TreeNode<T>* param1Min = param1.find_minimum();
if (localRoot != param1Root) {
return false;
}
while (localMin == param1Min) {
if (localMin->data == 10) {
return true;
}
localMin = next_in_order(localMin);
param1Min = next_in_order(param1Min);
}
}

template <typename T>
void BSTree<T>::to_vector( TreeNode<T>* param1 , vector<T>& param2 ) const {
pre_order_push(param1, param2);
return;
}


template <typename T>
ostream& operator<<( ostream& param1 , const BSTree<T>& param2 ) {
TreeNode<T>* localRoot = param2.get_root();
if ( localRoot ) {
param2.pre_order( localRoot, param1);
}
param1 << endl;
return param1;
}

#endif


第三幅图的代码是我要写的,是和第二幅图连在一起的,只是我分开给大家看,因为第一幅图和第二幅图的内容都是老师固定的(我省略了一些不必要的代码没贴上来,一是位置不足,二是和我需要写的部分没什么联系)我几乎可以肯定是超载operator==的时候有问题,可是我修改了无数次也还是不行。
我会在2楼放上测试用的cpp,主要是我并不知道哪里有错误,怎么改都是有错误。

如果用上面的代码的,加上测试用的cpp的话,会出现以下结果:

但是我需要的结果是这样的:



PS:(数字是随机的不用纠结,我测试了一下,我写的代码里面除了operator==应该都是对的)

如果可以的话求打什么帮忙修改一下这个operator==,怎么写的都有问题,我也是醉了..

对了忘记说了,这些代码都是说BST的,就是Binary Search Tree,我百度了中文是二叉树..
...全文

相关推荐

Brass——它为什么又指“厚颜无耻”呢

该文章有1000个习语Brass——它为什么又指“厚颜无耻”呢

cikecn

鉴于本站无耻恰饭行为,下架所有文章资源

鉴于本站无耻恰饭行为,下架所有文章资源

篽風

jack:使用修复程序构建游戏“天灾无耻”

杰克 使用修复构建游戏“Jack of Scourges”。 此版本已过时,请从其他存储库获取最新版本: :

谁家扁舟子

无耻之徒 高清壁纸 新标签页 热门电视剧 主题-crx插件

语言:English,中文 (简体),中文 (繁體) 每次打开新标签时都会获得无耻之徒不同的高清壁纸。这个新主题除此以外还包括天气,时间,记事本,时钟等其他你想要的强大功能。 无耻之徒是由Showtime出品的美国电视剧,这部电视剧集合了多位导演、副导演、编剧、制作人的智慧而成,由威廉姆·H·梅西、埃米·罗森、贾斯汀·查特文主演。以下是您将获得的其他功能列表: - 待办事项清单 - 设置收藏你的最爱壁纸 - 发现新的热门 - 书签 - 社交网络通知 - 每个新标签的高品质壁纸 - 时钟,显示日期和时间 - 天气小工具 - 打开即可搜索

weixin_38656297

无耻的壁纸。「Shameless Wallpapers.」-crx插件

添加我们的无耻壁纸到您的浏览器,并享受高清主题每个新标签。 每天,在新标签页的背景下,您都会看到漂亮的新墙纸。 每天根据自己的心情进行更改,您的日子将永远不会灰暗! 无耻的墙纸和高清主题可帮助您在监视器屏幕上创建专属的合适个人空间,因为这是任何桌面的整体设计和风格的主要元素。 该扩展名是免费的,不需要注册。 支持语言:English

weixin_38529436
评论 14
收藏 已收藏
分享
返回顶部
ls1991lsok
C++ 语言 65,213 成员 250,513 内容