我将以下项目插入二叉搜索树:
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
答案 0 :(得分:1)
如果您无法将树的内容转储到矢量中并在不同的字段上对矢量进行排序,我想您可以尝试以下操作:
步骤1:根据新的排序字段查找,报告并保留最小元素的副本。 (搜索整棵树)
步骤2(及后续):查找,报告并保留最大元素的副本,该元素也大于前一个最小元素。 (搜索整棵树)
效率不高,但我认为你应该能够做到这一点。
祝你好运。