Thread: Binary Search Tree

  1. #1
    Registered User
    Join Date
    Feb 2009
    Location
    Indiana
    Posts
    99

    Binary Search Tree

    For my project 4 I have to build a Binary Search Tree using Inorder Traversal. I know what it does but I dont know how to go about creating a code for one. This is what I got so far:
    Code:
    #include<iostream>
    using namespace std;
    
    typedef BinNode;
    typedef char Elem;
    
    class BinNodePtr
    {
    private:
    	Elem it;	/*The node's value*/
    	BinNodePtr*lc;	/*Pointer to left child*/
    	BinNodePtr*rc;	/*Pointer to right child*/
    public:
    	/*Two constructors -- with and without initial values*/
    	BinNodePtr() {lc = rc = NULL;}
    	BinNodePtr(Elem e, BinNodePtr* l =NULL, BinNode* r =NULL)
    	{ it = e; lc = l; rc = r; }
    	~BinNodePtr() {}	/*Destructor*/
    	Elem& val() {return it;}
    	void setVal(const Elem& e) {it = e;}
    	inline BinNode*left() const { return lc;}
    	void setLeft(BinNode*b) {lc = (BinNodePtr*)b;}
    	inline BinNode*right() const { return rc; }
    	void setRight(BinNode*b) { rc = (BinNodePtr*)b; }
    	bool isLeaf() { return (lc == NULL) && (rc == NULL); }
    };
    
     void treeInsert(TreeNode *&root, string newItem) {
              if ( root == NULL ) {
                 root = new TreeNode( newItem );
                 return;
              }
              else if ( newItem < root->item ) {
                 treeInsert( root->left, newItem );
              }
              else {
                 treeInsert( root->right, newItem );
              }
           } 
    
    void inorder(BinNode* subroot){
    	if(subroot == NULL) return;
    	inorder(subroot->left());
    	visit(subroot);
    	inorder(subroot->right());
    }
    
    bool treeContains( TreeNode *root, string item ) {
                 if ( root == NULL ) {	
                    return false;
                 }
                 else if ( item == root->item ) {	
                    return true;
                 }
                 else if ( item < root->item ) {
                    return treeContains( root->left, item );
                 }
                 else {
                    return treeContains( root->right, item );
                 }
              }  // end treeContains()

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    Do you have a specific problem or question?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 0
    Last Post: 11-04-2006, 11:07 AM
  2. BST (Binary search tree)
    By praethorian in forum C++ Programming
    Replies: 3
    Last Post: 11-13-2005, 09:11 AM
  3. searching and insertion in a binary search tree
    By galmca in forum C Programming
    Replies: 1
    Last Post: 03-26-2005, 05:15 PM
  4. binary search and search using binary tree
    By Micko in forum C++ Programming
    Replies: 9
    Last Post: 03-18-2004, 10:18 AM
  5. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 10:33 AM