Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <iostream>
- using namespace std;
- struct Node {
- int data;
- Node *left;
- Node *right;
- Node(int x) {
- data = x;
- left = right = NULL;
- }
- };
- void createNode(int x, Node *&tree) {
- if (tree == NULL) tree = new Node(x);
- if (x < tree->data) {
- if (tree->left != NULL) createNode(x, tree->left);
- else tree->left = new Node(x);
- }
- if (x > tree->data) {
- if (tree->right != NULL) createNode(x, tree->right);
- else tree->right = new Node(x);
- }
- }
- void search(int x, Node *tree, int i, char *command) {
- if (tree == NULL) {
- cout << "NO ROUTE";
- return;
- }
- if (x == tree->data) {
- command[i] = 'S';
- command[i + 1] = '\0';
- if (command != '\0') cout << command<<"\n";
- }
- if (x < tree->data) {
- command[i] = 'L';
- search(x, tree->left, i + 1,command);
- }
- if (x > tree->data) {
- command[i] = 'R';
- search(x, tree->left, i + 1,command);
- }
- }
- int main() {
- Node *tree = NULL;
- int x;
- cin >> x;
- while (x != 0) {
- createNode(x, tree);
- cin >> x;
- }
- int p;
- cin >> p;
- for (int j = 0; j < p; j++) {
- int x;
- cin >> x;
- char *command = new char[1000];
- search(x, tree, 0, command);
- }
- return 0;
- }
Add Comment
Please, Sign In to add comment