From e5016aff54554692b6e3388eee90de6d7a793416 Mon Sep 17 00:00:00 2001 From: Ankit Tiwari Date: Sun, 18 Oct 2020 12:23:36 +0530 Subject: [PATCH] New_change_in_cpp --- ...rder_Tree_Traversal _without_Recursion.cpp | 74 ++++++++++++ ...The Great_Tree-List_ Recursion_Problem.cpp | 112 ++++++++++++++++++ 2 files changed, 186 insertions(+) create mode 100644 cpp/Inorder_Tree_Traversal _without_Recursion.cpp create mode 100644 cpp/The Great_Tree-List_ Recursion_Problem.cpp diff --git a/cpp/Inorder_Tree_Traversal _without_Recursion.cpp b/cpp/Inorder_Tree_Traversal _without_Recursion.cpp new file mode 100644 index 000000000..090cf14f7 --- /dev/null +++ b/cpp/Inorder_Tree_Traversal _without_Recursion.cpp @@ -0,0 +1,74 @@ +// C++ program to print inorder traversal +// using stack. +#include +using namespace std; + +/* A binary tree Node has data, pointer to left child +and a pointer to right child */ +struct Node +{ + int data; + struct Node* left; + struct Node* right; + Node (int data) + { + this->data = data; + left = right = NULL; + } +}; + +/* Iterative function for inorder tree +traversal */ +void inOrder(struct Node *root) +{ + stack s; + Node *curr = root; + + while (curr != NULL || s.empty() == false) + { + /* Reach the left most Node of the + curr Node */ + while (curr != NULL) + { + /* place pointer to a tree node on + the stack before traversing + the node's left subtree */ + s.push(curr); + curr = curr->left; + } + + /* Current must be NULL at this point */ + curr = s.top(); + s.pop(); + + cout << curr->data << " "; + + /* we have visited the node and its + left subtree. Now, it's right + subtree's turn */ + curr = curr->right; + + } /* end of while */ +} + +/* Driver program to test above functions*/ +int main() +{ + + /* Constructed binary tree is + 1 + / \ + 2 3 + / \ + 4 5 + */ + struct Node *root = new Node(1); + root->left = new Node(2); + root->right = new Node(3); + root->left->left = new Node(4); + root->left->right = new Node(5); + + inOrder(root); + return 0; +} + diff --git a/cpp/The Great_Tree-List_ Recursion_Problem.cpp b/cpp/The Great_Tree-List_ Recursion_Problem.cpp new file mode 100644 index 000000000..930ddcaf8 --- /dev/null +++ b/cpp/The Great_Tree-List_ Recursion_Problem.cpp @@ -0,0 +1,112 @@ +ilter_none +edit +play_arrow + +brightness_4 +// C++ Program to convert a Binary Tree +// to a Circular Doubly Linked List +#include +using namespace std; + +// To represents a node of a Binary Tree +struct Node +{ + struct Node *left, *right; + int data; +}; + +// A function that appends rightList at the end +// of leftList. +Node *concatenate(Node *leftList, Node *rightList) +{ + // If either of the list is empty + // then return the other list + if (leftList == NULL) + return rightList; + if (rightList == NULL) + return leftList; + + // Store the last Node of left List + Node *leftLast = leftList->left; + + // Store the last Node of right List + Node *rightLast = rightList->left; + + // Connect the last node of Left List + // with the first Node of the right List + leftLast->right = rightList; + rightList->left = leftLast; + + // Left of first node points to + // the last node in the list + leftList->left = rightLast; + + // Right of last node refers to the first + // node of the List + rightLast->right = leftList; + + return leftList; +} + +// Function converts a tree to a circular Linked List +// and then returns the head of the Linked List +Node *bTreeToCList(Node *root) +{ + if (root == NULL) + return NULL; + + // Recursively convert left and right subtrees + Node *left = bTreeToCList(root->left); + Node *right = bTreeToCList(root->right); + + // Make a circular linked list of single node + // (or root). To do so, make the right and + // left pointers of this node point to itself + root->left = root->right = root; + + // Step 1 (concatenate the left list with the list + // with single node, i.e., current node) + // Step 2 (concatenate the returned list with the + // right List) + return concatenate(concatenate(left, root), right); +} + +// Display Circular Link List +void displayCList(Node *head) +{ + cout << "Circular Linked List is :\n"; + Node *itr = head; + do + { + cout << itr->data <<" "; + itr = itr->right; + } while (head!=itr); + cout << "\n"; +} + + +// Create a new Node and return its address +Node *newNode(int data) +{ + Node *temp = new Node(); + temp->data = data; + temp->left = temp->right = NULL; + return temp; +} + +// Driver Program to test above function +int main() +{ + Node *root = newNode(10); + root->left = newNode(12); + root->right = newNode(15); + root->left->left = newNode(25); + root->left->right = newNode(30); + root->right->left = newNode(36); + + Node *head = bTreeToCList(root); + displayCList(head); + + return 0; +} +