【发布时间】:2015-04-07 23:08:29
【问题描述】:
我将以下项目插入到二叉搜索树中:
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
【问题讨论】:
-
树的结构由键定义。如果要按排名顺序遍历,请使用排名作为键。
-
@molbdnilo 我同意,但除了修改文件,我该怎么做?
-
如果实现是合理的,应该没有理由修改输入。细节取决于你的树实现,我们对此一无所知。
-
@molbdnilo 很公平。我会添加一些细节,看看你是否可以提供进一步的帮助。我完全不知道如何做到这一点。
-
您的插入方法按某个键的顺序安装记录,在本例中为“排名”。前/中/后顺序遍历仅在排序树的上下文中才有意义。树仍然按插入选项排序。有 15 个条目,从 1..15 开始排名,树的顶部和中间将是 7。因此,有序遍历是唯一“有序”的遍历。前后遍历不会。
标签: c++ sorting binary-search-tree