Quote
Write a program that:
-reads a sentence from cin
-separates words and inserts them in BST
-prints the tree (inorder traversal)
-prompts for a word to be deleted from BST
-prints the tree (inorder traversal)
-Write a template Insert function for inserting new words into the tree.
-Write a template Delete function for deleting words from the tree.
-reads a sentence from cin
-separates words and inserts them in BST
-prints the tree (inorder traversal)
-prompts for a word to be deleted from BST
-prints the tree (inorder traversal)
-Write a template Insert function for inserting new words into the tree.
-Write a template Delete function for deleting words from the tree.
I don't really know how to continue. I'm attempting to write the INSERT function that will insert the string into the tree at the correct point using recursion. However, I cannot get the data types to match up. I don't know how to compare the values in the TreeNode->Left with the data or completely set up the insertion recursion.
...snippet (eliminating includes)....
template <typename T>
class TreeNode
{
public:
//init-constructor
TreeNode(const T& value, TreeNode<T>* left = NULL, TreeNode<T>* right = NULL)
{
Value = value;
Left = left;
Right = right;
}
//get value
const T GetValue() const
{
return Value;
}
//get Left
TreeNode<T>* GetLeft() const
{
return Left;
}
//get Right
TreeNode<T>* GetRight() const
{
return Right;
}
private:
T Value;
TreeNode<T>* Left;
TreeNode<T>* Right;
};
//insert a leaf
template <typename T>
void Insert(TreeNode<T>*& root, const T& data)
{
if (root == NULL)
{
root = new TreeNode<T>(data);
}
else
{
if (root->GetValue() == data)
{
return;
}//end if CNV==data
else
{
if (root->GetValue() > data)
{
}
}//end if CNV>data
} //end else
}
void main()
{
//string
string myString;
//set root to null
TreeNode<string>* treeRoot = NULL;
cout << "Enter a string. " << endl;
while (cin.peek() != '\n')
{
cin >> myString;
Insert(treeRoot, myString);
}
system("pause");
}

New Topic/Question
Reply



MultiQuote





|