Skip to content

Binary_tree_question_inorder_traversal #5

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
74 changes: 74 additions & 0 deletions cpp/Inorder_Tree_Traversal _without_Recursion.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
// C++ program to print inorder traversal
// using stack.
#include<bits/stdc++.h>
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<Node *> 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;
}

112 changes: 112 additions & 0 deletions cpp/The Great_Tree-List_ Recursion_Problem.cpp
Original file line number Diff line number Diff line change
@@ -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<iostream>
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;
}