Thread: Creating BInary search tree iterative

  1. #1
    Registered User
    Join Date
    Mar 2012
    Posts
    2

    Creating BInary search tree iterative

    hello

    I am creating binary search tree according to pseudo code posted in this forum but there is some problem can any one help me?
    My code is
    Code:
    #include <conio.h>
    #include <stdio.h>
    #include <iostream.h>
    
    struct list
    {
        int data;
        list *left,*right;
    };
    list *root=NULL;
    void preorder(struct list *root)
    {
        if(root!=NULL)
        {
            cout<<"\t"<<root->data;
            preorder(root->left);
            preorder(root->right);
        }
    }
    void main()
    {
        struct list *current,*temp;
        int n,num;
        clrscr();
    
        cout<<"\n How many nodes you want to create:- ";
        cin>>n;
    
    
        for(int i=0;i<n;i++)
        {
            cout<<"\n Enter data for node :- ";
            cin>>num;
            if(root==NULL)
            {
                temp=new list;
                temp->data=num;
                temp->left=NULL;
                temp->right=NULL;
                root=temp;
            }
            else
            {
                current=root;
                while(current!=NULL)
                {
                    if(num<current->data)
                    {
                        if(current->left==NULL)
                        {
                            
                            current=current->left;
    //                         continue;
                        }
                        else
                        {
                            current->left=new list;
                            current->left->data=num;
                            break;
    
                        }
                    }
                    else if(num > current->data)
                    {
                        if(current->right==NULL)
                        {
                            current=current->right;
    //                         continue;
                        }
                        else
                        {
                            current->right=new list;
                            current->right->data=num;
                            break;
    
                        }
                    }
                    else
                    {
                        cout<<"\n Duplicate node";
                        break;
                    }
                }
            }
    
        }
    
        preorder(root);
        getch();
    }

  2. #2
    Registered User
    Join Date
    May 2010
    Posts
    4,633
    It might help if you would describe your problem.

    Jim

  3. #3
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    If you have "some problem", then please accept "some solution" from me:
    int main()
    You're somewhat welcome.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  4. #4
    Registered User
    Join Date
    Mar 2012
    Posts
    2
    i have solved my problem. Problem is in my code....there is some mistake in code....and finally i got solution...but can you tell me if i want to traverse a BST iterative then is it possible to traves a BST iteratively without use of stack?

  5. #5
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Yes, you can traverse a binary tree in any order without using recursion, or an explicit stack. It usually involves traversing the tree from the root directly to the next node every time.

    I strongly suggest reading this article for next time you post here: Coding Horror: Rubber Duck Problem Solving
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Creating a Binary Search Tree
    By paranoidgnu in forum C Programming
    Replies: 1
    Last Post: 07-14-2011, 11:54 AM
  2. creating a binary search tree
    By krishpatel in forum C Programming
    Replies: 6
    Last Post: 12-23-2010, 11:53 PM
  3. creating a binary search tree
    By krishpatel in forum C Programming
    Replies: 1
    Last Post: 12-23-2010, 01:18 PM
  4. binary tree but iterative, not recursive!
    By budala in forum C Programming
    Replies: 2
    Last Post: 08-06-2009, 12:55 PM
  5. A Binary Search Tree of... Binary Search Trees...
    By SlyMaelstrom in forum C++ Programming
    Replies: 5
    Last Post: 12-10-2005, 02:12 PM