GeeksforGeeks » Trees specific questions

implement a stack (push and pop) using binary search tree

(5 posts)

Tags:

  1. rsag
    guest
    Posted 8 months ago #

    how to implement a stack (push and pop) using binary search tree??

  2. Saurabh
    guest
    Posted 8 months ago #

    I think, BST node should have the following structure.
    struct BSTStack{
    Data *data;
    int *stackIndex;
    BSTStack *left, *right;
    };

    Now trick is push and pop the nodes in BST according to the stackIndex. i.e push wiill push the node in BST with higest stackIndex and pop will pop out the node with higest BSTStack in BST.

    Saurabh
    saurabhsinghals@gmail.com

  3. saurabh
    guest
    Posted 8 months ago #

    Consider this structure

    struct BSTStack{
    Data *data;
    int stackIndex;
    BSTStack *left, *right;
    };

  4. Amit
    guest
    Posted 7 months ago #

    #include <stdio.h>
    #include <stdlib.h>

    #define FALSE 0
    #define TRUE 1

    struct node
    {
    int data;
    int stackindex;
    struct node* left;
    struct node* right;
    };

    int top=-1;

    struct node* newNode(int data, int index)
    {
    struct node* node = (struct node*)malloc(sizeof(struct node));
    node->data = data;
    node->stackindex = index;
    node->left = NULL;
    node->right = NULL;

    return(node);
    }

    struct node* push(struct node *root,int data)
    {
    struct node *temp=root;
    if(temp == (struct node *)0)
    {
    temp=newNode(data,++top);
    }
    else if(data<temp->data)
    {
    temp->left=push(temp->left,data);
    }
    else
    {
    temp->right=push(temp->right,data);
    }
    return temp;
    }

    void search (struct node **sr, int num, struct node **par, struct node **x, int *found )
    {
    struct node *q ;

    q = *sr ;
    *found = FALSE ;
    *par = NULL ;

    while ( q != NULL )
    {
    // if the node to be deleted is found
    if ( q->stackindex == num )
    {
    *found = TRUE ;
    *x = q ;
    return ;
    }

    *par = q ;

    if ( q->data > num )
    q = q->left ;
    else
    q = q->right ;
    }
    }

    void deleteNode ( struct node **sr, int num )
    {
    int found ;
    struct node *parent, *x, *xsucc ;

    // if tree is empty
    if ( *sr == NULL )
    {
    printf("\nTree is empty");
    return ;
    }

    parent = x = NULL ;

    // call to search function to find the node to be deleted
    search ( sr, num, &parent, &x, &found ) ;

    // if the node to deleted is not found
    if ( found == FALSE )
    {
    printf("\nData to be deleted, not found");
    return ;
    }

    if( parent == NULL ){
    free(x);
    *sr = NULL;
    return;
    }
    // if the node to be deleted has two children
    if ( x->left != NULL && x->right != NULL )
    {
    parent = x ;
    xsucc = x->right ;

    while ( xsucc->left != NULL )
    {
    parent = xsucc ;
    xsucc = xsucc->left ;
    }

    x->data = xsucc->data ;
    x = xsucc ;
    }

    // if the node to be deleted has no child
    if ( x->left == NULL && x->right == NULL )
    {
    if ( parent->right == x )
    parent->right = NULL ;
    else
    parent->left = NULL ;

    free(x) ;
    return ;
    }

    // if the node to be deleted has only right
    if ( x->left == NULL && x->right != NULL )
    {
    if ( parent->left == x )
    parent->left = x->right ;
    else
    parent->right = x->right ;

    free(x) ;
    return ;
    }

    // if the node to be deleted has only left child
    if ( x->left != NULL && x->right == NULL )
    {
    if ( parent->left == x )
    parent->left = x->left ;
    else
    parent->right = x->left ;

    free(x) ;
    return ;
    }
    }

    void pop(struct node** root){
    if(top==-1){
    printf("Stack is empty!!\n");
    }
    else{
    deleteNode(&(*root),top);
    --top;
    }
    }

    void printInorder(struct node* node)
    {
    if (node == NULL)
    return;

    printInorder(node->left);
    printf("%d[%d]\n", node->data,node->stackindex);
    printInorder(node->right);
    }

    int main()
    {
    struct node *root = (struct node *)0;
    root=push(root,6);
    root=push(root,2);
    root=push(root,1);
    root=push(root,4);
    root=push(root,3);
    root=push(root,5);
    root=push(root,8);
    root=push(root,7);
    root=push(root,10);
    root=push(root,9);
    root=push(root,11);
    printf("Before : \n");
    printInorder(root);
    pop(&root);
    pop(&root);
    printf("\nAfter : \n");
    printInorder(root);
    return 0;
    }

  5. fre@k
    guest
    Posted 7 months ago #

    struct BSTStack
    {
    Data *data;
    BSTStack *left, *right;
    BSTStack *previous;
    };

    BSTStack *top;

    Each node will contain a pointer to previous node it points too.
    On insertion of a new node

    BstStack *NewNode;
    NewNode->previous= top;
    top= NewNode;

    On deletion

    BSTStack *temp= top;
    top= temp->previous;
    delete(temp);


Reply

You must log in to post.

RSS feed for this topic