Answers for "bst traversal"

C++
0

bst traversal code in data structure with c++

#include<iostream>
using namespace std;
struct node {
   int data;
   struct node *left;
   struct node *right;
};
struct node *createNode(int val) {
   struct node *temp = (struct node *)malloc(sizeof(struct node));
   temp->data = val;
   temp->left = temp->right = NULL;
   return temp;
}
void inorder(struct node *root) {
   if (root != NULL) {
      inorder(root->left);
      cout<<root->data<<" ";
      inorder(root->right);
   }
}
struct node* insertNode(struct node* node, int val) {
   if (node == NULL) return createNode(val);
   if (val < node->data)
   node->left = insertNode(node->left, val);
   else if (val > node->data)
   node->right = insertNode(node->right, val);
   return node;
}
int main() {
   struct node *root = NULL;
   root = insertNode(root, 4);
   insertNode(root, 5);
   insertNode(root, 2);
   insertNode(root, 9);
   insertNode(root, 1);
   insertNode(root, 3);
   cout<<"In-Order traversal of the Binary Search Tree is: ";
   inorder(root);
   return 0;
}
Posted by: Guest on January-18-2021
0

bst traversal

//The following code contains all the DFS traversal 
//I have first created a Binary search tree 
//Then performed DFS search :- 1.Inorder;2.Preorder;3.Postorder
#include <iostream>

using namespace std;
struct node
{
    int data;
    node* left;
    node*right;
};
node* getnode(int value)
{
    node* temp=new node;
    temp->data=value;
    temp->left=NULL;
    temp->right=NULL;
    return temp;
}
node* insert_bst(node* roots,int value)
{
    if(roots==NULL)
    {
       return getnode(value);
    }
    if(roots->data>value)
    {
        roots->left=insert_bst(roots->left,value);
    }
    else if(roots->data<value)
    {
        roots->right=insert_bst(roots->right,value);
    }
    return roots;
}
void Inorder(node* roots)
{
    if(roots==NULL)
    {
       return;
    }
    else
    {
        Inorder(roots->left);
        cout<<roots->data<<" ";
        Inorder(roots->right);
    }
}
void post_order(node* roots)
{
    if(roots==NULL)
    {
        return;
    }
    else
    {
        post_order(roots->left);
        post_order(roots->right);
        cout<<roots->data<<" ";
    }
}
void preorder(node* root)
{
    if(root==NULL)
        return;
    cout<<root->data<<" ";
    preorder(root->left);
    preorder(root->right);
}
int main()
{
    node* root=new node;
    root=NULL;
    int number_of_nodes;
    cin>>number_of_nodes;
    int value;
    for(int i=0;i<number_of_nodes;i++)
    {
        cin>>value;
        root=insert_bst(root,value);
    }
    cout<<"Inorder Travesral:"<<endl;
    Inorder(root);
    cout<<endl;
    cout<<"Postorder traversal:"<<endl;
    post_order(root);
    cout<<endl;
    cout<<"preorder traversal:"<<endl;
    preorder(root);
    cout<<endl;
    return 0;
}
Posted by: Guest on July-26-2021
0

bst traversal preorder

Algorithm Preorder(tree)
   1. Visit the root.
   2. Traverse the left subtree, i.e., call Preorder(left-subtree)
   3. Traverse the right subtree, i.e., call Preorder(right-subtree)
Posted by: Guest on October-14-2021

Browse Popular Code Answers by Language