首頁  >  文章  >  後端開發  >  使用佇列反轉二元搜尋樹中的路徑的C++程式碼

使用佇列反轉二元搜尋樹中的路徑的C++程式碼

PHPz
PHPz轉載
2023-09-14 19:21:04883瀏覽

例如,給定一個二元搜尋樹,我們需要從特定鍵反轉其路徑。

使用佇列反轉二元搜尋樹中的路徑的C++程式碼

使用佇列反轉二元搜尋樹中的路徑的C++程式碼

#尋找解決方案的方法

在這個方法中,我們將建立一個佇列並推送所有節點,直到獲得根節點。

p>

範例

 
#include <bits/stdc++.h>
using namespace std;
struct node {
   int key;
   struct node *left, *right;
};
struct node* newNode(int item){
   struct node* temp = new node;
   temp->key = item;
   temp->left = temp->right = NULL;
   return temp;
}
void inorder(struct node* root){
   if (root != NULL) {
       inorder(root->left);
       cout << root->key << " ";
       inorder(root->right);
   }
}
void Reversing(struct node** node,
           int& key, queue<int>& q1){
   /* If the tree is empty then
   return*/
   if (node == NULL)
       return;
   if ((*node)->key == key){ // if we find the key
       q1.push((*node)->key); // we push it into our queue
       (*node)->key = q1.front(); // we change the first queue element with current
       q1.pop(); // we pop the first element
   }
   else if (key < (*node)->key){ // if key is less than current node&#39;s value
       q1.push((*node)->key); // we push the element in our queue
       Reversing(&(*node)->left, key, q1); //we go to the left subtree using a recursive call
       (*node)->key = q1.front(); //we reverse the elements
       q1.pop(); // we pop the first element
   }
   else if (key > (*node)->key){ // if key greater than node key then
       q1.push((*node)->key);// we push node key into queue
       Reversing(&(*node)->right, key, q1);// we go to right subtree using a recursive call
       (*node)->key = q1.front();// replace queue front to node key
       q1.pop(); // we pop the first element
   }
   return;
}
struct node* insert_node(struct node* node, // function to insert node nodes in our BST
                           int key){
   if (node == NULL)
       return newNode(key); // if tree is empty we return a new node
   if (key < node->key) // else we push that in our tree
       node->left = insert_node(node->left, key);
   else if (key > node->key)
       node->right = insert_node(node->right, key);
   return node; // returning the node
}
int main(){
   struct node* root = NULL;
   queue<int> q1;
   int k = 80;
/****************Creating the BST*************************/
   root = insert_node(root, 50);
   insert_node(root, 30);
   insert_node(root, 20);
   insert_node(root, 40);
   insert_node(root, 70);
   insert_node(root, 60);
   insert_node(root, 80);
   cout << "Before Reversing :" << "\n";
   inorder(root);
   cout << "\n";
   Reversing(&root, k, q1);
   cout << "After Reversing :" << "\n";
   // print inorder of reverse path tree
   inorder(root);
   return 0;
}

輸出

Before Reversing :
20 30 40 50 60 70 80
After Reversing :
20 30 40 80 60 70 50

上述程式碼的解釋

#在這個方法中,我們只需搜尋給定的鍵。當我們遍歷樹時,我們將所有節點放入佇列中,現在當我們找到具有鍵值的節點時,我們交換排在前面的所有路徑節點的值,在這個過程中,我們的路徑

結論

我們使用佇列和遞歸解決了BST 中反轉路徑的問題。我們也學習了該問題的 C 程序以及解決該問題的完整方法(普通)。我們可以用其他語言像是C、java、python等語言來寫同樣的程式。我們希望本教學對您有所幫助。

以上是使用佇列反轉二元搜尋樹中的路徑的C++程式碼的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文轉載於:tutorialspoint.com。如有侵權,請聯絡admin@php.cn刪除