Needing help with this function and ->

I have been working on this assignment for class and need help I'm stuck on this function it inserts a word into my dictionary. I have that part down but what i'm having trouble with is how I need to insert words after the first? I think my problem is stemming from my understanding of pointers maybe? I'm unsure. any help is much appreciated

Here's the code I have so far:

#include <stdio.h>
#include <stdlib.h>
#include <string>
#include <iostream>

using namespace std;


const int MAX = 100;
typedef bool BOOL;
typedef string WORD;

/*
structure describing a word entry in the dictionary
*/

typedef struct Pair {
int count; /* frequency count for a particular word */
WORD w; /* the word itself */
} Pair;

typedef struct entry {
Pair e;
struct entry *leftChild; /* left sub-tree */
struct entry *rightChild; /* right sub-tree */
struct entry *next; /* next sibling */
struct entry *prev; /* previous sibling */

} ENTRY;

/*
structure describing the dictionary
*/

typedef struct dict {
int maxEntries; /* maximum number of entries allowed; this is an artificial limit */
/* linked lists can be as big as you want. This limit ensures that */
/* this code tries to behave like the previous ones */

int numWords; /* number of words in the dictionary */
ENTRY *Words; /* pointer to the entries in binary tree */
ENTRY *wordLen; /* pointer to entries in thread */
} DICT;

// Here's the function itself

BOOL InsertWord(DICT &dict, WORD word){
if(FullDictionary(dict)) return false;//If the dictionary is full will return false
if(dict.Words == nullptr){//If there is no ENTRY in the dictionary it will create an ENTRY and
ENTRY *p = new (ENTRY);//add it to the dictionary and set defaults for the ENTRY and return true.
(p->e).w = word;//adds word to ENTRY.
(p->e).count = 1;//set word count in ENTRY to 1.
p->leftChild = p->rightChild = nullptr;
dict.numWords++;
p->next = dict.wordLen;
dict.wordLen = p;
dict.Words = p;

return true;
}
else


/*
adds word to dictionary (tree and thread ), if word can't be added returns false else returns true

*/
}
Last edited on

I MISREAD, thought you said TREE, you can ignore the tree part of this response.

if this is to be a binary search tree or one of its friends, the rule for inserting is lesser values go left, greater values go right.
so if you had some kind of words list...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
my dog ate my homework

it looks like this
my <- root
      my 
     /
   dog    //dog is less than my, it goes left. 

      my 
     /
   dog   
   /
 ate //ate is less than my, but the left child exists so repeat the logic.  ate is less than dog, go left. 

//my is already in there, don't reinsert it? (dictionary rule, one copy??)

and finally
      my 
     /
   dog    
   /    \
 ate    homework //homework is less than my, but bigger than dog, it goes right on dog...


if you added the word zoo next, it goes right of my, and so on, see?
note that if you insert the dictionary in alphabetical order, you have a linked list, a degraded and less efficient tree. Order of inserts matters, and in order or in reverse order are both awful. Random is good.

you do this kind of thing with a temporary pointer.
tmp = root of tree;
if tmp is null, insert like you do now. (this needs to update root, special case) if not,
if tmp's data == new data, stop, word already in dictionary
else if it is less and tmp.left is null, insert there. if its greater and tmp.right is null, insert there. else if less, tmp = tmp.left. else if greater, tmp=tmp.right.
repeat above until inserted or ignored as duplicate.

you are using C headers. Use the C++ version, it can cause big issues in programs to mix them, but works OK in small programs in school.
<cstdlib>
<cstdio>
do you need cstdio? Its for c-strings mostly, but may have a pointer define or two in it, I forget.
you don't need the struct keyword, that is C.
struct foo {things...};

foo x; //fine, don't have to say struct foo every time, that is relic code.
you also do not need typedef on struct: this is also an archaic or C way of coding.
you also should not stick variables on the end of structs or classes }variable; style. This is also archaic and weird. Just do as my simple example above .. again, like this:

struct name
{fields};
name variable;

typedef bool BOOL;
typedef string WORD;
please do not do this. Its legal, but you are making 2 or 3 mistakes here:
1) you just renamed standard types, so now the reader of your code (imagine it was 10k files and 10million lines of code) has to figure out what your special magic BOOL type is and why it is different from bool and why you did that. Ugg!
2) word is an integer type, of the machine's word size (eg a 64 bit computer has a 64 bit word sized integer). You confuse the reader here twice, once like above because you renamed string, and once again because of the way the word word is used in c++. (this is 2 errors in 1)
3) MAX also used in C++, its a function that finds the max of multiple values, so I would not use this word, even though its OK by being all caps, its still annoying to readers, and some older macros use the all caps version on some older compilers.

if you are stuck using a 1990 compiler (a few students lately are having this problem) then you may need to keep doing what you are doing for some of the above, but get rid of as many of these ancient things as your compiler will tolerate.
Last edited on
For some reason I thought I saw 'tree' not linked list. The above tree stuff may be handy later when you get to them so I will leave it. Sorry about that.

a linked list is much easier.
same idea, start with a temp = top of list.
find where it goes in the list … assuming its sorted, you check
is temp null, if not, is temp the same as the new word, if not, is new word less than temp (insert at top: new node -> next = tmp, done. If its after temp, then you have to check if next is null (if so, put it there, temp->next is new, new-> next is null) , if not tmp = tmp->next and repeat this logic (starting after is the top of the list null special case check, start at the is tmp == the new data) until inserted. Don't forget to update prev pointers.

if it isn't sorted, just stick them on the top, its a lot more efficient. If you can read in in reverse order from a sorted input and only stick on the top, that is the same thing.
Last edited on
Topic archived. No new replies allowed.