C++ code to reverse paths in a binary search tree using queues
For example, given a binary search tree, we need to reverse its path from a specific key.
Ways to find solution
In this method we will create a queue and push all the nodes , until the root node is obtained.
p>
Example
#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'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; }
Output
Before Reversing : 20 30 40 50 60 70 80 After Reversing : 20 30 40 80 60 70 50
Explanation of the above code
In this method we just search for the given key. When we traverse the tree, we put all the nodes into a queue, now when we find the node with the key value, we swap the values of all the path nodes that came before, in this process, our path
Conclusion
We solved the problem of reversing paths in BST using queues and recursion. We also learned the C program for this problem and the complete method (general) to solve it. We can write the same program in other languages such as C, java, python and other languages. We hope you found this tutorial helpful.
The above is the detailed content of C++ code to reverse paths in a binary search tree using queues. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

Application summary of queue technology in message delay and message retry in PHP and MySQL: With the continuous development of web applications, the demand for high concurrency processing and system reliability is getting higher and higher. As a solution, queue technology is widely used in PHP and MySQL to implement message delay and message retry functions. This article will introduce the application of queue technology in PHP and MySQL, including the basic principles of queues, methods of using queues to implement message delay, and methods of using queues to implement message retries, and give

Performance Analysis and Optimization Strategy of JavaQueue Queue Summary: Queue (Queue) is one of the commonly used data structures in Java and is widely used in various scenarios. This article will discuss the performance issues of JavaQueue queues from two aspects: performance analysis and optimization strategies, and give specific code examples. Introduction Queue is a first-in-first-out (FIFO) data structure that can be used to implement producer-consumer mode, thread pool task queue and other scenarios. Java provides a variety of queue implementations, such as Arr

Implementation of queue task monitoring and task scheduling in PHP and MySQL Introduction In modern web application development, task queue is a very important technology. Through queues, we can queue some tasks that need to be executed in the background, and control the execution time and order of tasks through task scheduling. This article will introduce how to implement task monitoring and scheduling in PHP and MySQL, and provide specific code examples. 1. Working principle of queue Queue is a first-in-first-out (FIFO) data structure that can be used to

Queue in Java is a linear data structure with multiple functions. A queue has two endpoints and it follows the first-in-first-out (FIFO) principle for inserting and deleting its elements. In this tutorial, we will learn about two important functions of queues in Java, which are add() and Offer(). What is a queue? Queue in Java is an interface that extends the util and collection packages. Elements are inserted in the backend and removed from the frontend. Queues in Java can be implemented using classes such as linked lists, DeQueue, and priority queues. A priority queue is an extended form of a normal queue, where each element has a priority. The add() method of the queue is used to insert elements into the queue. It will define the element (as

How to reverse and reverse a PHP array In PHP, an array is a commonly used data structure capable of storing and manipulating large amounts of data. Sometimes we need to reverse or reverse the array to meet specific needs. This article will introduce how to use PHP to reverse and reverse an array, and give corresponding code examples. 1. Reversing an array Reversing an array means rearranging the elements in the array in reverse order according to their original order. PHP provides a variety of methods to reverse arrays. Here are two commonly used methods:

What is the principle and implementation of the PHP mail queue system? With the development of the Internet, email has become one of the indispensable communication methods in people's daily life and work. However, as the business grows and the number of users increases, sending emails directly may lead to server performance degradation, email delivery failure and other problems. To solve this problem, you can use a mail queue system to send and manage emails through a serial queue. The implementation principle of the mail queue system is as follows: when the mail is put into the queue, when it is necessary to send the mail, it is no longer directly

Overview of the PHPSPL Data Structure Library The PHPSPL (Standard PHP Library) data structure library contains a set of classes and interfaces for storing and manipulating various data structures. These data structures include arrays, linked lists, stacks, queues, and sets, each of which provides a specific set of methods and properties for manipulating data. Arrays In PHP, an array is an ordered collection that stores a sequence of elements. The SPL array class provides enhanced functions for native PHP arrays, including sorting, filtering, and mapping. Here is an example of using the SPL array class: useSplArrayObject;$array=newArrayObject(["foo","bar","baz"]);$array

For example, given a binary search tree, we need to reverse its path from a specific key. Ways to find the solution In this approach we will create a queue and push all the nodes until we get the root node. p>Example #include<bits/stdc++.h>usingnamespacestd;structnode{ intkey; structnode*left,*right;};structnode*newNode(intitem){&nb
