在二进制搜索树中对对象值排序
Ordering object values in a binary search tree
我将以下项目插入到二进制搜索树中:
Mayweather,Floyd,1,105,105,0,Boxing
Ronaldo,Cristiano,2,80,52,28,Soccer
James,LeBron,3,72.3,19.3,53,Basketball
Messi,Lionel,4,64.7,41.7,23,Soccer
Bryant,Kobe,5,61.5,30.5,31,Basketball
Woods,Tiger,6,61.2,6.2,55,Golf
Federer,Roger,7,56.2,4.2,52,Tennis
Mickelson,Phil,8,53.2,5.2,48,Golf
Nadal,Rafael,9,44.5,14.5,30,Tennis
Ryan,Matt,10,43.8,42,1.8,Football
Pacquiao,Manny,11,41.8,41,0.8,Boxing
Ibrahimovic,Zlatan,12,40.4,36.4,4,Soccer
Rose,Derrick,13,36.6,17.6,19,Basketball
Bale,Gareth,14,36.4,25.4,11,Soccer
Falcao,Radamel,15,36.4,32.4,3,Soccer
其中rank是csv中的第一个整数。正如你所看到的,大多数已经有序了,但一旦它们进入二进制搜索树,如果我进行前、后或有序遍历,它们也会无序。
我尝试了很多方法,但假设不能使用数组、向量或任何其他对象——只使用树,怎么能做到这一点?
void displayRank(Athlete& anItem)
{
cout << "Player: " << anItem.getRank() << endl;
}
void AthleteDatabase::displayByRank(void)
{
athleteDatabaseBST.preorderTraverse(displayRank);
}
内置的遍历以随机顺序打印排名,因为姓氏是关键。非常感谢您的帮助!
下面是BinarySearchTree.h文件:
class BinarySearchTree : public BinaryNodeTree<ItemType>
{
private:
BinaryNode<ItemType>* rootPtr;
protected:
//------------------------------------------------------------
// Protected Utility Methods Section:
// Recursive helper methods for the public methods.
//------------------------------------------------------------
// Recursively finds where the given node should be placed and
// inserts it in a leaf at that point.
BinaryNode<ItemType>* insertInorder(BinaryNode<ItemType>* subTreePtr,
BinaryNode<ItemType>* newNode);
// Removes the given target value from the tree while maintaining a
// binary search tree.
BinaryNode<ItemType>* removeValue(BinaryNode<ItemType>* subTreePtr,
const ItemType target,
bool& success);
// Removes a given node from a tree while maintaining a
// binary search tree.
BinaryNode<ItemType>* removeNode(BinaryNode<ItemType>* nodePtr);
// Removes the leftmost node in the left subtree of the node
// pointed to by nodePtr.
// Sets inorderSuccessor to the value in this node.
// Returns a pointer to the revised subtree.
BinaryNode<ItemType>* removeLeftmostNode(BinaryNode<ItemType>* subTreePtr,
ItemType& inorderSuccessor);
// Returns a pointer to the node containing the given value,
// or nullptr if not found.
BinaryNode<ItemType>* findNode(BinaryNode<ItemType>* treePtr,
const ItemType& target) const;
public:
//------------------------------------------------------------
// Constructor and Destructor Section.
//------------------------------------------------------------
BinarySearchTree();
BinarySearchTree(const ItemType& rootItem);
BinarySearchTree(const BinarySearchTree<ItemType>& tree);
virtual ~BinarySearchTree();
//------------------------------------------------------------
// Public Methods Section.
//------------------------------------------------------------
bool isEmpty() const;
int getHeight() const;
int getNumberOfNodes() const;
ItemType getRootData() const throw(PrecondViolatedExcep);
void setRootData(const ItemType& newData) const throw(PrecondViolatedExcep);
bool add(const ItemType& newEntry);
bool remove(const ItemType& anEntry);
void clear();
ItemType getEntry(const ItemType& anEntry) const throw(NotFoundException);
bool contains(const ItemType& anEntry) const;
//------------------------------------------------------------
// Public Traversals Section.
//------------------------------------------------------------
void preorderTraverse(void visit(ItemType&)) const;
void inorderTraverse(void visit(ItemType&)) const;
void postorderTraverse(void visit(ItemType&)) const;
//------------------------------------------------------------
// Overloaded Operator Section.
//------------------------------------------------------------
BinarySearchTree<ItemType>& operator=(const BinarySearchTree<ItemType>& rightHandSide);
}; // end BinarySearchTree
当您无法将树的内容转储到向量中并在不同字段上对向量进行排序时,我想您可以尝试以下操作:
步骤1:根据新的排序字段查找、报告并保留最小元素的副本。(搜索整棵树)
步骤2(及后续步骤):查找、报告并保留最小元素的副本,该副本也大于前一个最小元素。(搜索整棵树)
效率不是很高,但我认为你应该能够做到这一点。
祝你好运。
相关文章:
- 在c++中尝试对对象数组进行排序时,出现std:bad_alloc错误
- 对象接收堆栈溢出异常 c++ 的排序向量
- 如何使用 STL 排序对具有模板专用化的自定义类对象进行排序?
- 排序谓词没有传递对索引对象的引用?
- 使用指针指向对象C++对向量进行排序
- 按类成员的顺序对包含类对象的C++向量进行排序
- 无法使用接口类型对priority_queue中的对象进行排序<T>
- 基于不同字段的对象向量的排序功能
- 基于3个对象的自定义排序
- 尝试使用比较运算符对对象向量进行排序
- C++排序的对象集
- 从文件中读取并将其内容放入对象数组中,然后对它们进行排序
- q排序类对象的列表
- std::atexit 从全局对象的构造函数调用时的排序
- 尝试对对象数组进行排序时程序终止
- std::排序为排序自定义对象时出现的向量引发错误
- 通过其char键对对象指针的C 向量进行排序
- 排序算法,使用模板按内部数据对对象进行排序
- 在对象向量中的对象字段排序
- 如何使用 std::sort 对数组中的特定对象进行排序