📌  相关文章
📜  排序从根到二叉树中给定节点的路径

📅  最后修改于: 2021-05-04 14:04:05             🧑  作者: Mango

给定二叉树,任务是对从到二叉树给定节点的特定路径进行排序。您会得到一个关键节点和树。任务是对路径进行排序,直到该特定节点为止。

例子

Input : 
          3 
        /   \ 
       4     5 
      / \     \ 
     1   2     6
       key = 2
Output :
          2 
        /   \ 
       3     5 
      / \     \ 
     1   4     6
Inorder :- 1 3 4 2 5 6
Here the path from root to given key is sorted 
from 3(root) to 2(key).

Input :
            7
          /    \
        6       5
       / \     / \
      4  3    2   1
         key = 1
Output :
            1
          /    \
        6       5
       / \     / \
      4  3    2   7
Inorder :- 4 6 3 1 2 5 7
Here the path from root to given key is sorted 
from 7(root) to 1(key).

算法:以下是一种简单的算法,用于从上到下对路径进行排序(升序)。

  1. 查找从根到给定关键节点的路径,并将其存储在优先级队列中。
  2. 将节点的值替换为优先级队列顶部元素。
  3. 如果左pq的大小大于弹出元素后用左pq替换node的值。
  4. 如果right pq size较大,则在弹出元素后用right pq替换node的值。
  5. 按顺序打印树。

下面是上述方法的实现:

// CPP program to sort the path from root to
// given node of a binary tree
  
#include 
#include 
using namespace std;
  
  
// Binary Tree node
struct Node {
    int data; // store data
    Node *left, *right; // left right pointer
};
  
/* utility that allocates a new Node 
with the given key */
Node* newNode(int data)
{
    Node* node = new Node;
    node->data = data;
    node->left = node->right = NULL;
    return (node);
}
  
// Function to find the inorder traversal
void inorder(struct Node* root)
{
    // base condition
    if (root == NULL)
        return;
  
    // go to left part
    inorder(root->left);
  
    // print the data
    cout << root->data << " ";
  
    // go to right part
    inorder(root->right);
}
  
priority_queue solUtil(Node* root, int key, 
                            priority_queue pq)
{
    priority_queue blank;
  
    // if node is not found
    // then we will return
    // blank priority queue
    if (root == NULL)
        return blank;
  
    // store the path in priority queue
    pq.push(root->data);
  
    // Go to left subtree to store the left path node data
    priority_queue left = solUtil(root->left, key, pq);
  
    // Go to right subtree to store the right path node data
    priority_queue right = solUtil(root->right, key, pq);
  
    // if the key is found then
    if (root->data == key) {
        root->data = pq.top();
        pq.pop();
        return pq;
    }
    else if (left.size()) // if the node in path then
    { // we change the root node data
        root->data = left.top();
        left.pop();
        return left;
    }
    else if (right.size()) // if the node in path then
    { // we change the root node data
        root->data = right.top();
        right.pop();
        return right;
    }
  
    // if no key node found
    // then return blank
    // priority_queue
    return blank;
}
  
// Function to sort path from 
// root to a given key node
void sortPath(Node* root, int key)
{
    // for store the data
    // in a sorted manner
    priority_queue pq;
  
    // call the solUtil function
    // sort the path
    solUtil(root, key, pq);
}
  
// Driver Code
int main()
{
    /*   3 
        / \ 
      4       5 
     / \    \ 
    1   2     6 */
  
    // Build the tree
    // given data
    Node* root = newNode(3);
    root->left = newNode(4);
    root->right = newNode(5);
    root->left->left = newNode(1);
    root->left->right = newNode(2);
    root->right->right = newNode(6);
  
    // given key
    int key = 1;
  
    // Call the function to
    // sort the path till given key tree
    sortPath(root, key);
  
    // call the function to print tree
    inorder(root);
  
    return 0;
}

输出:-

1 3 4 2 5 6

时间复杂度:O(N logN)[N用于遍历所有节点,N * logN用于优先级队列]