Skip to content
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

Traversals #214

Merged
merged 6 commits into from
Dec 15, 2017
Merged
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# Inorder Traversal

![alt text](https://javabeat.net/wp-content/uploads/2013/11/BST_Inorder.jpg)

Algorithm Inorder(tree)
* 1. Traverse the left subtree, i.e., call Inorder(left-subtree)
* 2. Visit the root.
* 3. Traverse the right subtree, i.e., call Inorder(right-subtree)

Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
#include<iostream>
#include<vector>
using namespace std;
#define nullptr 0
typedef struct elem
{
int val;
struct elem* leftchild;
struct elem* rightchild;
struct elem* parent;
}node;//This is the node for the binary tree. Each element in the binary tree contains a value, a pointer to left child,right child and parent.
class BinaryTree
{
private:
int n=0;
node* root=nullptr;
public:
void insert(int x)//Insert function first searches for the place where the node can be inserted and then it inserts it there.
{

if(n==0)
{
root=new node;
root->val=x;
root->leftchild=nullptr;
root->rightchild=nullptr;
root->parent=nullptr;
}
else
{
node* temp=root;
node* newnode=searchnode(x,temp);
if(x>newnode->val)
{
newnode->rightchild=new node;
newnode->rightchild->val=x;
newnode->rightchild->parent=newnode;
newnode->rightchild->leftchild=nullptr;
newnode->rightchild->rightchild=nullptr;
}
else
{
newnode->leftchild=new node;
newnode->leftchild->val=x;
newnode->leftchild->parent=newnode;
newnode->leftchild->leftchild=nullptr;
newnode->leftchild->rightchild=nullptr;
}

}
n++;

}

node* searchnode(int x,node* temproot)//The search function searches for the node whose value is equal to the value it is searching for.It takes O(log n) as it is proportional to the height of the binary tree.
{

if(temproot->val<x)//If value is smaller than it goes to the right child
{
if(temproot->rightchild!=nullptr)
searchnode(x,temproot->rightchild);
else
return temproot;
}
else
{
if(temproot->val>x)//If value if larger it goes to the left child
{
if(temproot->leftchild!=nullptr)
searchnode(x,temproot->leftchild);
else
return temproot;
}
else
{
if(temproot->val==x)//It returns the value when it is equal
return temproot;
}
}

}
void printtree(node* temp)
{
if(temp==nullptr)
return;
else
{
printtree(temp->leftchild);
cout<<temp->val;
printtree(temp->rightchild);
}
}//This is the function to print the tree. It prints the tree in a sorted order.
node* giveroot()
{
return root;
}//This function returns the pointer to root
void inorder_traversal(node* temp)
{
if(temp==nullptr)
return ;
else
{
inorder_traversal(temp->leftchild);
cout<<temp->val<<endl;
inorder_traversal(temp->rightchild);
}
}

};
int main()
{
int n;
cin>>n;
BinaryTree bst;
for(int i=0;i<n;i++)
{
int temp;
cin>>temp;
bst.insert(temp);
}
node* temp=bst.giveroot();

// bst.printtree(temp);
//temp = bst.giveroot();
bst.inorder_traversal(temp);

}






Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
#include<iostream>
#include<vector>
using namespace std;
#define nullptr 0
typedef struct elem
{
int val;
struct elem* leftchild;
struct elem* rightchild;
struct elem* parent;
}node;//This is the node for the binary tree. Each element in the binary tree contains a value, a pointer to left child,right child and parent.
class BinaryTree
{
private:
int n=0;
node* root=nullptr;
public:
void insert(int x)//Insert function first searches for the place where the node can be inserted and then it inserts it there.
{

if(n==0)
{
root=new node;
root->val=x;
root->leftchild=nullptr;
root->rightchild=nullptr;
root->parent=nullptr;
}
else
{
node* temp=root;
node* newnode=searchnode(x,temp);
if(x>newnode->val)
{
newnode->rightchild=new node;
newnode->rightchild->val=x;
newnode->rightchild->parent=newnode;
newnode->rightchild->leftchild=nullptr;
newnode->rightchild->rightchild=nullptr;
}
else
{
newnode->leftchild=new node;
newnode->leftchild->val=x;
newnode->leftchild->parent=newnode;
newnode->leftchild->leftchild=nullptr;
newnode->leftchild->rightchild=nullptr;
}

}
n++;

}

node* searchnode(int x,node* temproot)//The search function searches for the node whose value is equal to the value it is searching for.It takes O(log n) as it is proportional to the height of the binary tree.
{

if(temproot->val<x)//If value is smaller than it goes to the right child
{
if(temproot->rightchild!=nullptr)
searchnode(x,temproot->rightchild);
else
return temproot;
}
else
{
if(temproot->val>x)//If value if larger it goes to the left child
{
if(temproot->leftchild!=nullptr)
searchnode(x,temproot->leftchild);
else
return temproot;
}
else
{
if(temproot->val==x)//It returns the value when it is equal
return temproot;
}
}

}
void printtree(node* temp)
{
if(temp==nullptr)
return;
else
{
printtree(temp->leftchild);
cout<<temp->val;
printtree(temp->rightchild);
}
}//This is the function to print the tree. It prints the tree in a sorted order.
node* giveroot()
{
return root;
}//This function returns the pointer to root

void postorder_traversal(node* temp)
{
if(temp==nullptr)
return ;
else
{
postorder_traversal(temp->leftchild);
postorder_traversal(temp->rightchild);
cout<<temp->val<<endl;
}
}


};
int main()
{
int n;
cin>>n;
BinaryTree bst;
for(int i=0;i<n;i++)
{
int temp;
cin>>temp;
bst.insert(temp);
}
node* temp=bst.giveroot();

// bst.printtree(temp);
//temp = bst.giveroot();
//bst.inorder_traversal(temp);
bst.preorder_traversal(temp);
}






Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# Postorder Traversal

![alt text](https://www.java2blog.com/wp-content/uploads/2014/07/PostOrderTraversalBinaryTree-1.jpg)

Algorithm Postorder(tree)
* Traverse the left subtree, i.e., call Postorder(left-subtree)
* Traverse the right subtree, i.e., call Postorder(right-subtree)
* Visit the root.
Loading