Thread: ABt Binary Search Trees

  1. #1
    Registered User
    Join Date
    Oct 2009
    Posts
    2

    ABt Binary Search Trees

    Write a program that will allow randomized 0..100 numbers and the 15 generated numbers will serve as the input to ur binary search tree. The program should then print out the contents of your BST using preorder, inorder, and postorder traversals, store the result in the text file ( bst.txt)Recall the convention to place any duplicate number in the left subtree of that letter.

    So far ive only done the randomize process... help me do the other parts...please any idea will help... or if you give the code much better,,,

    Thx ...

    Code:
    #include<stdlib.h>
    #include<iostream.h>
    #include<conio.h>
    
    int main()
    
          {
    
          clrscr();
    
          srand((unsigned)time(0));
          int random_integer = rand();
    
          for(int index=0; index<15; index++) {
    	random_integer = (rand()%100)+1;
          cout << random_integer << endl;
    	}
          getch();
          }

  2. #2
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    >Write a program that will allow randomized 0..100 numbers
    > random_integer = (rand()%100)+1;
    That gives you numbers from 1..100.

  3. #3
    Registered User
    Join Date
    Oct 2009
    Posts
    2
    How can I traverse those 15 random numbers??

  4. #4
    Registered User
    Join Date
    Oct 2009
    Posts
    12
    use an array
    Code:
    int random_integer[15]
    	for(int index=0; index<14; index++) {
    		random_integer[i] = (rand()%100)+1;
    		cout << random_integer[i] << endl;
    	}
    	for(int index=14; index>0; index--) {// check through in reverse order
    		cout << random_integer[i] << endl;
    	}
    they are the only ways you can transverse through the list(?), though you can point to a specific array element by using random_interger[5] or whatever number element you want to look at.

    also this isn't a binary search tree that you are asking for, however you have written "this will be the input for your search tree" so I assume that you already have a separate search tree?. http://en.wikipedia.org/wiki/Binary_search_tree
    Last edited by robolee; 10-17-2009 at 08:39 AM.

  5. #5
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Quote Originally Posted by ging ging View Post
    How can I traverse those 15 random numbers??
    You need to build the binary search tree containing those random numbers.
    You could use an array with the same indexing scheme as a heap, but the normal way to do it is to build a tree out of separately allocated nodes. Do you know how to build a linked-list? Building a tree is just a step up from there.

    Now you've been told that your code does not generate numbers in the correct range (does not include zero). Have you figured out how to fix that yet?
    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. Binary Search Trees
    By lorannex in forum C Programming
    Replies: 3
    Last Post: 04-25-2009, 06:24 AM
  2. Performance issue!
    By maven in forum C Programming
    Replies: 42
    Last Post: 03-23-2009, 11:57 AM
  3. Ornery binary search algorithm
    By Unregistered in forum C Programming
    Replies: 3
    Last Post: 11-17-2001, 03:32 PM
  4. Newbie questions regarding binary search trees
    By Ham in forum C++ Programming
    Replies: 1
    Last Post: 11-04-2001, 07:48 AM