Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/fortran/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Data structures 在BST中,假设作为一个比较器,按降序访问所有键需要哪种遍历?_Data Structures_Binary Search Tree_Comparator - Fatal编程技术网

Data structures 在BST中,假设作为一个比较器,按降序访问所有键需要哪种遍历?

Data structures 在BST中,假设作为一个比较器,按降序访问所有键需要哪种遍历?,data-structures,binary-search-tree,comparator,Data Structures,Binary Search Tree,Comparator,在BST中,假设作为一个比较器,按降序访问所有键需要哪种遍历? 答案是相反的- 我想知道为什么会这样 如果它要按递增的顺序访问所有键呢, 下面的答案是什么? 1.在-2.Pre-3.Post-4。倒进-5。6号前的倒车档。反向Post-我假设左子树包含小于根的元素,右子树包含大于根的元素 首先回答第二个问题:这将是中缀遍历。它首先递归地访问左(小)子项,然后递归地访问项目本身,然后递归地访问右(大)子项。不难看出,此方法以升序访问所有元素 但是,按降序访问所有项目必须执行与此相反的操作,因此使用

在BST中,假设作为一个比较器,按降序访问所有键需要哪种遍历? 答案是相反的- 我想知道为什么会这样

如果它要按递增的顺序访问所有键呢, 下面的答案是什么?
1.在-2.Pre-3.Post-4。倒进-5。6号前的倒车档。反向Post-

我假设左子树包含小于根的元素,右子树包含大于根的元素

首先回答第二个问题:这将是中缀遍历。它首先递归地访问左(小)子项,然后递归地访问项目本身,然后递归地访问右(大)子项。不难看出,此方法以升序访问所有元素


但是,按降序访问所有项目必须执行与此相反的操作,因此使用反向中缀。这将首先递归地访问较大的元素,然后是元素本身,然后递归地访问较小的元素。

中缀遍历将按递增顺序打印键。

按顺序表示左根右(递增)

void升序(BST*root)
{
if(root==NULL)返回;
升序(根->左);
标准::cout
void ascending(BST* root)
{
    if(root == NULL) return;
    ascending(root->left);
    std::cout<<root->data<<" ";
    ascending(root->right);
}
void descending(BST* root)
{
    if(root == NULL) return;
    descending(root->right);
    std::cout<<root->data<<" ";
    descending(root->left);
}