Binary Search Tree for strings?

So I have to write a program to use a binary search tree for a movie database by reading in a file that contains movies and actors, and display information based on the movie title. As of right now I only have a binary search tree using numbers, but I was wondering how I could convert this to using strings instead so that I can read in the data file and use it in the tree. Some demonstration on how to change this to use strings instead would be great, or at least on how to start.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121

#include<iostream>
#include<cstdlib>

#include"BST.h"

using namespace std;


BST::BST()
{
  root = NULL;
}

BST::node* BST::CreateLeaf(int key)
{
  node *n = new node;
  n->key = key;

  n->left = NULL;
  n->right = NULL;

  return n;
};

void BST::AddLeaf(int key)
{
  AddLeafPrivate(key, root);
}

void BST::AddLeafPrivate(int key, node* Ptr)
{
  if(root == NULL)             // if tree is empty
  {
    root = CreateLeaf(key);    // add leaf with key value
  }
  else if(key < Ptr->key)      // if key value less than current node value
  {
    if(Ptr->left != NULL)        // check: if left node is not empty
    {
      AddLeafPrivate(key, Ptr->left);       // check left side
    }
     else      // if left side is empty
    {
      Ptr->left = CreateLeaf(key);    // add node to left subtree
    }
  }
  else if(key > Ptr->key)     // Check if value is less than current value
  {
    if(Ptr->right != NULL)    //  check if right subtree is empty
    {
      AddLeafPrivate(key, Ptr->right);    // not empty, check right side
    }
    else
    {
      Ptr->right = CreateLeaf(key);    // if empty add node
    }
  }
  else         // if value equal to current node value
  {
    cout << "The key " << key << " has already been added to the tree.\n";
  }

}

void BST::PrintInOrder()
{
  PrintInOrderPrivate(root);
}

void BST::PrintInOrderPrivate(node* Ptr)
{
  if(root != NULL)    // if tree is not empty
  {
    if(Ptr->left != NULL)    // if left subtree not empty
    {
      PrintInOrderPrivate(Ptr->left);   // go to left subtree
    }
    cout << Ptr->key << " ";    // print current key value
    if(Ptr->right != NULL)     // if right subtree is not empty
    {
      PrintInOrderPrivate(Ptr->right);    // go to right subtree
 }
  }
  else      // if tree is empty
  {
    cout << "The tree is empty\n" << endl;
  }
}

BST::node* BST::ReturnNode(int key)
{
  return ReturnNodePrivate(key, root);    // returns node
}


BST::node* BST::ReturnNodePrivate(int key, node* Ptr)
{
  if(Ptr != NULL)
    {
      if(Ptr->key == key)
        {
          return Ptr;
 }
        else
          {
            if(key < Ptr->key)
              {
                return ReturnNodePrivate(key, Ptr->left);
              }
            else
              {
                return ReturnNodePrivate(key, Ptr->right);
              }
          }
    }
  else
    {
      return NULL;
    }
}
Below is my header file

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56

#include<iostream>
#include<cstdlib>

#include "BST.cpp"

using namespace std;

int main()
{

  ifstream infile("tvDB.txt");
  if(file.is_open())
    {
      string myArray[];

      for(int i = 0; i < 5; ++i)
        {
          file >> myArray[i];
        }
    }




  int TreeKeys[16] = {50, 76, 21, 4, 32, 64, 15, 52, 14, 100, 83, 2, 3, 70, \
87, 80};

  BST myTree;    // Create the binary search tree

  cout << "Printing the tree in ordder\nBefore adding numbers\n";

  myTree.PrintInOrder();   // print tree in order

  for(int i = 0; i < 16; i++)
  {
    myTree.AddLeaf(TreeKeys[i]);   // copy values into tree
  }

  cout << "Printing the tree in order\nAfter adding numbers\n";

  myTree.PrintInOrder();        // print tree in order

  for(int i = 0; i < 16; i++)
  {
    myTree.AddLeaf(TreeKeys[i]);   // copy values into tree
  }

  cout << "Printing the tree in order\nAfter adding numbers\n";

  myTree.PrintInOrder();        // print tree in order

  cout << endl;

  return 0;
}
Topic archived. No new replies allowed.