Welcome to Dream.In.Code
Getting C++ Help is Easy!

Join 118,309 C++ Programmers for FREE! Ask your question and get quick answers from experts. There are 1,696 online right now! We've got more than 500 tutorials and 2,000 snippets. Join and find out why Dream.In.Code is the #1 programming help community on the internet! Registration is fast and FREE... Join Now!



Using a Binary Tree in conjunction with a Linked List

 
Reply to this topicStart new topic

Using a Binary Tree in conjunction with a Linked List, Need help with insertion algorithm

stritone
post 7 Aug, 2008 - 03:03 PM
Post #1


New D.I.C Head

*
Joined: 7 Aug, 2008
Posts: 21


My Contributions


Hello,
I'm working on a data structure that incorporates a binary search tree and also a linked list. This data structure probably has a name but I'm not sure what it is. I apologize in advance if this is already a well known data structure that has seen previous posts. I'm brand new here. This program is sort of my first attempt at doing a little bit of object oriented programming and also trying to incorporate the BST and LLL structures.

I'm working on the insertion algorithm that basically should call a function that will input some basic user information (first name, last name) and then insert it into a binary search tree. The function then prompts if the user would like to input more contacts that will be placed in a linked list flowing from the initial contact in the BST. So, each node of the BST will have a linked list that flows from it that can be traversed, et cetera. At any rate, it would be great to get some help on the insertion, retrieval, and display algorithms for this project. It is not a university assignment, just something I'm working on over the summer. Thanks in advance to anyone who would like to help. I'm still very much a novice with programming and apologize if my coding/posting style is not acceptable.

Cheers!

CODE

#include "p1a.h"
#include <iostream.h>

int main()
{
  E_System E;
  E.User_Interface();
  return 0;
}

/******************************************/
/**************************************************************************
* This the header file for the Emergency Contact System Program
* ************************************************************************/
struct Node
{
  char * Position;
  char * F_Name;
  char * L_Name;
  char * Address;
  char * City;
  char * State;
  int Zip;
  char * Phone;
  char * Email;

  Node * Next;
  Node * LChild, * RChild;
};

/*************************************************************************/
class LL
{
  public:
    LL();
    ~LL();
    void LL_Insert(Node *& treeNode);
    void LL_ListInsert(); /* This is called from the User Interface to append contacts to a user account*/
    void LL_Delete();
    void LL_Display();
    bool LL_Is_Empty() const;

  protected:
    Node * Head;
};

/************************************************************************/
class BST : public LL
{
  public:
    BST();
    ~BST();
    void BST_Insert();
    void BST_InsertContact(Node *& treePtr, Node *& treePtr, bool & Success);
/*    void BST_Remove(); */
    void BST_DisplayInorder();
    void BST_Inorder(Node * treePtr);
    bool BST_Is_Empty() const;
    void BST_ProcessLeftmost(Node *& treePtr, Node *& treePtr);
    void BST_Visit(Node * treePtr);
    void BST_InorderDel(Node * treePtr);
    void BST_Del_Visit(Node * treePtr);
    void BST_DestroyTree(Node *& treePtr);
    void BST_Delete();
    void BST_DeleteContact(Node *& treePtr, Node *& treePtr, bool & Success);
    void BST_DeleteContactInfo(Node *& treePtr);
    void BST_Retrieve();
    void BST_RetrieveContact(Node *& treePtr, Node *& treePtr, bool & Success);

  protected:
    Node * Root;
};

/*******************************************/
class E_System : public BST
{
  public:
    E_System();
    ~E_System();
    void Call_E_Contact();
    void Call_Alt_E_Contact();
    void Email_E_Contact();
    void Email_Alt_E_Contact();
    void User_Interface();

  private:
    char * Emergency;          /* I'm not sure about this field */
};

/********************************************/
E_System::E_System()
{
  Emergency = NULL;
}

E_System::~E_System()
{
  delete [] Emergency;
}

void E_System::Call_E_Contact()
{
  BST_Retrieve();
  cout << "Calling Emergency Contact..." << endl;

}

void E_System::Call_Alt_E_Contact()
{
  cout << "Calling Alternate Emergency Contacts..." << endl;
}

void E_System::Email_E_Contact()
{
  cout << "Emailing Emergency Contact..." << endl;
}

void E_System::Email_Alt_E_Contact()
{
  cout << "Emailing Alternate Emergency Contacts..." << endl;
}

void E_System::User_Interface()
{
  E_System E;
  int choice;
  char response = 'y';


  do
  {
  cout << "***************************" << endl;
  cout << "*                  User Menu           *" << endl;
  cout << "***************************" << endl;
  cout << "1) Add a new account to the emergency contact program" << endl;
  cout << "2) Add a contact to an existing account" << endl;
  cout << "3) Delete an account from the emergency contact program" << endl;
  cout << "4) Delete a contact from an existing account" << endl;
  cout << "5) Display all contact accounts in the tree" << endl;
  cout << "6) Display all contacts within an emergency contacts list" << endl;
  cout << "7) Call an emergency contact" << endl;
  cout << "8) Call alternate emergency contacts" << endl;
  cout << "9) Email an emergency contact" << endl;
  cout << "10) Email alternate emergency contacts" << endl;
  cout << "11) Quit" << endl;
  cin >> choice;

  switch(choice)
  {
    case 1:
        E.BST::BST_Insert();
        break;
    case 2:
/*        E.LL::LL_ListInsert();  */
        break;
    case 3:
        E.BST::BST_Delete();
        break;
    case 4:
        E.LL::LL_Delete();
        break;
    case 5:
        E.BST_DisplayInorder();
        break;
    case 6:
        E.BST_Retrieve();
        break;
    case 7:
        E.Call_E_Contact();
        break;
    case 8:
        E.Call_Alt_E_Contact();
        break;
    case 9:
        E.Email_E_Contact();
        break;
    case 10:
        E.Email_Alt_E_Contact();
        break;
    case 11:
        cout << "Thanks for using the program." << endl;
        break;
    default:
        cout << "Error in choice selection." << endl;
        break;
  }

  cout << "Enter another selection? (y-yes, n-no)" << endl;
  cin >> response;
  }while(response == 'y');
}
/*****************MY ATTEMPTS AT INSERTION*********************/

void LL::LL_Insert(Node *& treeNode)
{
  LL List;
  List.Head = treeNode;    /* treeNode has the Root's data in it */
  char response = 'y';

  while(response == 'y')
  {
  char temp[75];
  Node * CurrPtr;
  CurrPtr = new Node;

  cin.get();
  cout << "Enter contact's first name: " << endl;
  cin.get(temp, 30, '\n');
  CurrPtr->F_Name = new char[strlen(temp) + 1];
  strcpy(CurrPtr->F_Name, temp);

  cin.get();
  cout << "Enter contact's last name: " << endl;
  cin.get(temp, 30, '\n');
  CurrPtr->L_Name = new char[strlen(temp) + 1];
  strcpy(CurrPtr->L_Name, temp);


  if (List.Head == NULL)
  {
    cout << "made it into the head node...inserting here..." << endl;
    List.Head = new Node;
    List.Head->Next = NULL;
    List.Head->LChild = NULL;
    List.Head->RChild = NULL;

    List.Head->F_Name = new char[strlen(CurrPtr->F_Name) + 1];
    strcpy(List.Head->F_Name, CurrPtr->F_Name);

    List.Head->L_Name = new char[strlen(CurrPtr->L_Name) + 1];
    strcpy(List.Head->L_Name, CurrPtr->L_Name);
  }
  else
  {
    cout << "head of this list is not empty...searching for insertion point..." << endl;
    Node * newPtr = List.Head;
    Node * prevPtr = NULL;
    while (newPtr != NULL)
    {
      cout << "made it into the while loop...looping..." << endl;
      prevPtr = newPtr;
      cout << "just set prevPtr = newPtr..." << endl;
      newPtr = newPtr->Next;
      cout << "just set newPtr = newPtr->Next..." << endl;
    }
    cout << "made it out of the while loop..." << endl;
    Node * newNode;
    newNode = new Node;
/*    newNode->Next = NULL;  */
    newNode->LChild = NULL;
    newNode->RChild = NULL;

    newNode->F_Name = new char[strlen(CurrPtr->F_Name) + 1];
    strcpy(newNode->F_Name, CurrPtr->F_Name);

    newNode->L_Name = new char[strlen(CurrPtr->L_Name) + 1];
    strcpy(newNode->L_Name, CurrPtr->L_Name);

    if (prevPtr == NULL)
    {
      newNode->Next = newPtr;
      List.Head = newNode;
    }
    else
    {
      newNode->Next = newPtr;
      prevPtr->Next = newNode;
    }
  }
  cout << "Do you wish to add more contacts to this emergency account? (y-yes, n-no)";
  cout << endl;
  cin >> response;
  }
}


/********BINARY SEARCH TREE INSERTION ATTEMPT********/
void BST::BST_Insert()
{
  Node * treePtr;
  treePtr = new Node;
  treePtr->LChild = NULL;
  treePtr->RChild = NULL;
  treePtr->Next = NULL;
  bool Success;
  char response = 'y';

  char temp[75];

  cin.get();
  cout << "Enter contact's first name: " << endl;
  cin.get(temp, 30, '\n');
  treePtr->F_Name = new char[strlen(temp) + 1];
  strcpy(treePtr->F_Name, temp);

  cin.get();
  cout << "Enter contact's last name: " << endl;
  cin.get(temp, 30, '\n');
  treePtr->L_Name = new char[strlen(temp) + 1];
  strcpy(treePtr->L_Name, temp);

  BST_InsertContact(Root, treePtr, Success);

    cout << "Do you wish to add more contacts to this emergency account? (y-yes, n-no)";
    cout << endl;
    cin >> response;
    if (response == 'y')
      LL::LL_Insert(Root);
}



void BST::BST_InsertContact(Node *& Root, Node *& treePtr, bool & Success)
{
  if (Root == NULL)
  {
    Root = new Node;
    Root->LChild = NULL;
    Root->RChild = NULL;
    Root->Next = NULL;

    Root->F_Name = new char[strlen(treePtr->F_Name) + 1];
    strcpy(Root->F_Name, treePtr->F_Name);

    Root->L_Name = new char[strlen(treePtr->L_Name) + 1];
    strcpy(Root->L_Name, treePtr->L_Name);

    Success = bool(Root != NULL);
  }
  else if (Root->L_Name < treePtr->L_Name)
    BST_InsertContact(Root->LChild, treePtr, Success);
  else
    BST_InsertContact(Root->RChild, treePtr, Success);
}


/**********DISPLAY FUNCTIONS***************/

void BST::BST_DisplayInorder()
{
  BST_Inorder(Root);
}

void BST::BST_Inorder(Node * treePtr)
{
  if(treePtr != NULL)
  {
    BST_Inorder(treePtr->LChild);
    BST_Visit(treePtr);
    BST_Inorder(treePtr->RChild);
  }

void BST::BST_Visit(Node * treePtr)
{
    cout << treePtr->F_Name << " | " << treePtr->L_Name << endl;
}
}



Attached File(s)
Attached File  p1___header_file.txt ( 2.07k ) Number of downloads: 4
Attached File  p1___implementation_file.txt ( 12.12k ) Number of downloads: 1
Attached File  p1___main_user_file.txt ( 112bytes ) Number of downloads: 2
Attached File  p1___output__trying_to_get_the_insert_op_to_work_.txt ( 5.24k ) Number of downloads: 3
User is offlineProfile CardPM

Go to the top of the page


Martyr2
post 7 Aug, 2008 - 10:16 PM
Post #2


Programming Theoretician

Group Icon
Joined: 18 Apr, 2007
Posts: 4,638



Thanked 120 times

Expert In: C/C++, Java, VB, VB.NET, C#, PHP, Web Development, HTML & CSS, Javascript

My Contributions


Well I did something similar to this in one of my blog entries. I called it a binary forest and it was actually a bit of the opposite of what you are doing and involved a vector instead of a linked list. I mention it because it may have some logic in it that you can use for insertion into a binary tree. In my example the vector is a list containing binary trees (essentially making up a forest). So I thought I would point it out and if you get no other replies I should be able to help you out with this (it is a bit late for me to help right at this moment).

Check it out and see if it is something you can use...

Martyr2's Programming Underground - Plant a Node, Make a Binary Tree Forest in C++!

Hope it helps a bit

smile.gif

This post has been edited by Martyr2: 7 Aug, 2008 - 10:17 PM
User is offlineProfile CardPM

Go to the top of the page

stritone
post 8 Aug, 2008 - 11:21 AM
Post #3


New D.I.C Head

*
Joined: 7 Aug, 2008
Posts: 21


My Contributions


Martyr2,
Thanks so much for your reply. I read through your blog entry and
did find it helpful. I hadn't seen this kind of structure before either
but it seems very very efficient. I saved the entire entry in a Word
document for later reference. Hope that was alright.

Maybe if I can break down the thought process of how to insert a
new node into the BST structure, it will help to straighten out the
code.

Here's what I've been thinking thus far:
-Main program calls the user interface function
-user interface function calls the BST_Insert function
-BST_Insert function accepts input from the user and stores it in a
new Node
--BST_Insert function then calls BST_InsertContact function to insert
the new node into the BST
--BST_Insert function then prompts the user if they'd like to add
more contacts to this Node's list. (if 'yes', then call LL_Insert; if 'no',
then return to Menu)

--If the user wishes to insert more contacts into the list, LL_Insert is
called
--LL_Insert immediately creates a new LL object List and points the
head of the List it to the treeNode it was passed
-LL_Insert function then enters a loop prompting the user for new
contact info to create new Nodes for and then stores them on the list.
At least, I think this is what this is doing.

One problem I noticed from the output session I ran is that there
seems to be only one list being created and each of the BST Nodes
have a pointer pointing to it? This conclusion was made from running
the display functions.

I think my problem lies in my LL_Insert function.
The algorithm for inserting a new Node into the List is flawed but I'm
not sure exactly where or how to fix this.

If I pass the LL_Insert function a Node to start off with, I need to
create a list where this passed in Node will be the Head of the list.
The insertions should then be added to this list at the end.

How and where would I create the new list object for each insertion
of extra contacts for the BST? Maybe the problem is in the class
definitions? Each BST Node will have it's own LL (not one that's
shared between all the Nodes in the BST).

Thanks again for your help and I'll keep working through the algorithm!

Cheers,
stritone
User is offlineProfile CardPM

Go to the top of the page

stritone
post 8 Aug, 2008 - 03:55 PM
Post #4


New D.I.C Head

*
Joined: 7 Aug, 2008
Posts: 21


My Contributions


Hi Martyr2,
I worked on the insertion functions with this data structure this
afternoon and think that the insertions are working at this point. I
placed the prompt to enter more contacts into the list in the
BST_InsertContact function instead of where it was previously (in
BST_Insert) and I think that solved the problem. The display function
is now passed the head of the list (once the BST is traversed to find
the appropriate starting Node).

Can you take a quick glance at this and see if there's something I
forgot?

Thanks again for your help!

CODE

void LL::LL_Insert(Node *& treeNode)
{

  char response = 'y';

  while(response == 'y')
  {
  char temp[75];
  Node * CurrPtr;
  CurrPtr = new Node;

  cin.get();
  cout << "Enter contact's first name: " << endl;
  cin.get(temp, 30, '\n');
  CurrPtr->F_Name = new char[strlen(temp) + 1];
  strcpy(CurrPtr->F_Name, temp);

  cin.get();
  cout << "Enter contact's last name: " << endl;
  cin.get(temp, 30, '\n');
  CurrPtr->L_Name = new char[strlen(temp) + 1];
  strcpy(CurrPtr->L_Name, temp);

  if (treeNode->Next == NULL)
  {
    cout << "creating new node...inserting as second node in this list..." << endl;
      treeNode->Next = CurrPtr;
      CurrPtr->Next = NULL;
      CurrPtr->LChild = NULL;
      CurrPtr->RChild = NULL;
  }
  else
  {
    cout << "first node of this list already points to a node...searching to insert..." << endl;
    Node * currentPtr = treeNode;
    Node * prevPtr = NULL;
    while (currentPtr != NULL)
    {
      cout << "made it into the while loop...looping..." << endl;
      prevPtr = currentPtr;
      cout << "just set prevPtr = newPtr..." << endl;
      currentPtr = currentPtr->Next;
      cout << "just set newPtr = newPtr->Next..." << endl;
    }
    cout << "made it out of the while loop..." << endl;

      CurrPtr->LChild = NULL;
      CurrPtr->RChild = NULL;

    if (prevPtr == NULL)
    {
      CurrPtr->Next = currentPtr;
      treeNode->Next = CurrPtr;
    }
    else
    {
      CurrPtr->Next = currentPtr;
      prevPtr->Next = CurrPtr;
    }
  }
  cout << "Do you wish to add more contacts to this emergency account? (y-yes, n-no)";
  cout << endl;
  cin >> response;
  }
}


I used the LL_Insert function above with these BST insert functions and I think the algorithm is now working.

CODE

void BST::BST_Insert()
{
  Node * treePtr;
  treePtr = new Node;
  treePtr->LChild = NULL;
  treePtr->RChild = NULL;
  treePtr->Next = NULL;
  bool Success;

  char temp[75];

  cin.get();
  cout << "Enter contact's first name: " << endl;
  cin.get(temp, 30, '\n');
  treePtr->F_Name = new char[strlen(temp) + 1];
  strcpy(treePtr->F_Name, temp);

  cin.get();
  cout << "Enter contact's last name: " << endl;
  cin.get(temp, 30, '\n');
  treePtr->L_Name = new char[strlen(temp) + 1];
  strcpy(treePtr->L_Name, temp);

  BST_InsertContact(Root, treePtr, Success);

}


void BST::BST_InsertContact(Node *& Root, Node *& treePtr, bool & Success)
{
  char response = 'y';

  if (Root == NULL)
  {
    Root = new Node;
    Root->LChild = NULL;
    Root->RChild = NULL;
    Root->Next = NULL;

    Root->F_Name = new char[strlen(treePtr->F_Name) + 1];
    strcpy(Root->F_Name, treePtr->F_Name);

    Root->L_Name = new char[strlen(treePtr->L_Name) + 1];
    strcpy(Root->L_Name, treePtr->L_Name);

    Success = bool(Root != NULL);

    cout << "Do you wish to add more contacts to this emergency account's list? (y-yes, n-no)";
    cout << endl;
    cin >> response;
    if (response == 'y')
      LL::LL_Insert(Root);

  }
  else if (Root->L_Name < treePtr->L_Name)
    BST_InsertContact(Root->LChild, treePtr, Success);
  else
    BST_InsertContact(Root->RChild, treePtr, Success);
}

User is offlineProfile CardPM

Go to the top of the page

Reply to this topicStart new topic
Time is now: 10/10/08 11:48AM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

Bye Bye Ads

Free DIC T-Shirt

T-Shirt Example

Related Sites

Monthly Drawing

Thumb Drive

Partners

Top Contributors

Top 10 Kudos This Month