A dictionary implemented by a binary search tree and stores the structure in the file

zhaozj2021-02-16  55

Super Power Dictionary

C.third Edition

This is the third edition of my Dictionary and it is very, very powerful now! Read in 20k word from a 2.3m text

File Only Uses Less Than 2seconds. However, Ranking The Will Take 41seconds! As it need to TRACK EACH Node from

"Wordtree" from "big to small" by "frequency" and "rank" them by this sequence and then copy each node to a node

of "ranktree" the "insert" The New Ranktree Node INTO RANKTREE.

1. Basic IDEA: I Found Out Some Mistakes in "Ranking" functions. And it is really a hardime to debug for a

Whole Day in Lab (Almost 8Hours from 10:00 AM to 18:30 PM).

2. Program design: The Biggest Problem Is Still How To Walk Round The "Key" Problem of Ranktree By Using "Frequency"

AS key to compare and at Same Keep All Ancester Class with Their Original Functions.

3. Major Function:

A. void traverse (...);

I add one more parameters to let "Ranktree Forest" to Start Traversing from Its Own "root".

B. Void insertrank (Tree * Node, Void * Userptr)

This is a special "callback" function to construct "Ranktree" Forest by "travers" Each Node of

"Wordtree".

C. Void Tree :: Maxwalk (...);

There is so new maxORDER WAY OF TRAVERSING. Actually it is so-caled "from big to small"

Exactly Opposite to "Midorder".

4. Further Improvement:

A. I Have to Go Soon, So, No More Time.

B. STILL NEED IMPROVE.

C. Ranking Counting and Wordtree Counting is Differed by One, And i Have Not Found The Bug.

D. I Hope i can shutrite the Basic string-related part to enable the dictionary to read Chinese.

#include

#include #include

#include

#include

Using namespace std;

Const char * defaultbackup = "c: //nickback.txt";

Const MaxWordLength = 255;

ENUM TRAVERSEORDER

{

Preorder, Midorder, Postorder, Maxorder

}

Class Tree

{

Private:

Tree * Pleft;

Tree * pright;

Tree * pParent;

CHAR * PNAME;

Int index;

Static int trecounter;

Static Tree * Proot;

Int freq;

Int Rank;

Void Maxwalk (Tree * Node, Void (* visit));

Void Prewalk (Tree * Node, Void (* Visit));

Void Midwalk (Tree * Node, Void (* visit));

Void Postwalk (Tree * Node, Void (* Visit));

Void Maxwalk (Tree * Node, Void (* Visit) (Tree * Node, Void * Userptr), Void * Userptr

Void Prewalk (Tree * Node, Void (* Visit) (Tree * Node, Void * Userptr), VOID * Userptr;

Void Midwalk (Tree * Node, Void (* Visit) (Tree * Node, Void * Userptr), VOID * Userptr;

Void Postwalk (Tree * Node, Void (* Visit) (Tree * Node, Void * Userptr), Void * Userptr;

protected:

Void * pkey;

PUBLIC:

Void decount () {trecounter -;

Const int GETRANK () {Return Rank;}

Void SetRank (const Int newrank) {rank = newrank;

Const int getfreq () {returnif;}

Void setfreq (const orig newfreq) {freq = newfreq;}

Void setFreq () {FREQ ;}

Tree * left ();

Tree * right ();

Tree * parent ();

Virtual void * key ();

Virtual Void SetKey (VOID * NEWKEY)

Void Setroot (Tree * Node);

Const char * name ();

Const int getIndex ();

Void setleft (Tree * Leftson);

Void Setright (Tree * Rightson);

Void setParent (Tree * Father);

Void setIndex (const int number);

Virtual Int Compare (Const void * OtherKey);

Void setName (const char * str);

Void givebirth ();

Virtual Bool Operator> (Tree &);

Virtual Bool Operator == (Tree &); Virtual Bool Operator <(TREE &);

Tree ();

Virtual ~ tree ();

Static Tree * root () {return

Const Int count ();

Void Traverse (Tree * Node), TraverseOrder Defaultorder = MIDORDER,

Tree * start = root ());

Void Traverse (Tree * Node, Void * Userptr), Void * Userptr,

TraverseOrder defaultorder = midorder, Tree * start = root ());

}

Void Visitrank (Tree * Node, Void * Userptr);

Void Defaultvisit (Tree * Node);

Void insertrank (Tree * Node, Void * Userptr);

Class binarytree: Public Tree

{

Private:

PUBLIC:

Virtual Void Remove (binarytree * node);

Virtual binarytree * next ();

Virtual binarytree * prev ();

Virtual Bool Insert (binarytree * st = (binarytree *) root ());

Virtual ~ binarytree ();

}

Class Wordtree: Public BinaryTree

{

Private:

Static int WordLength;

Static int WordCounter;

Wordtree * AddNode (Wordtree * Node, File ** Stream);

protected:

Void setWordCounter ();

Void readword (file ** file);

Void AddWord (Char * Buffer);

PUBLIC:

Wordtree * Searchword (Wordtree * Node, Const Char * Buffer);

Wordtree * Readnode (Wordtree * Node, File ** Stream);

Wordtree * readfromfile (const char * filename);

Void Savenode (WordTree * Node, File ** Stream);

Virtual Int Compare (Const void * OtherKey);

Void SetWordLength; Const Int Newlength;

Static const Int getWordLength ();

Virtual Void SetKey (VOID * NEWKEY)

Virtual Bool Operator> (Tree &);

Virtual Bool Operator == (Tree &);

Virtual Bool Operator <(Tree &);

Void OpenFile (Const Char * filename);

WordTree (const char * filename);

Wordtree ();

Int WordCount ();

Virtual Void SetName (Const Char * STR);

Void Savetofile (Const Char * filename);

~ Wordtree ();

Virtual Wordtree * Findword (const char * buffer);

Class RankTree: Public Wordtree

{

Private:

protected:

PUBLIC:

Virtual Bool Operator> (Tree & Node);

Virtual Bool Operator <(Tree & node);

Void setRankTree (WordTree * WordRoot);

Ranktree ();

}

INT count = 0;

int main ()

{

Wordtree * PTR = New Wordtree;

Char Choice [6], FileName [30];

Long start = 0;

PTR = Ptr-> ReadFromFile (DefaultBackup);

COUT << "Quit? Or Input? / NENTER Your Choice: / N";

CIN >> CHOICE;

While (Strcmp (Choice, "Quit")! = 0 && Strcmp (Choice, "Input")! = 0)

{

COUT << "QUIT? OR INPUT? / N Enter your chome: / n";

CIN >> CHOICE;

}

IF (Strcmp (Choice, "Quit") == 0)

{

COUT << "The Total Number of Words in Dictionary IS" << Ptr-> WordCount () << Endl;

Return 0;

}

Else

{

IF (Strcmp (Choice, "Input") == 0)

{

COUT << "/ nnow input file name here; input 'quit' to exit): / n";

Filename;

While (STRCMP (FileName, "Quit")! = 0)

{

START = Time (0);

Ptr-> OpenFile (filename);

Cout << "/ NTOTAL TIMES SPENT for Read File IS:" << Time (0) -start << Endl;

COUT << "/ nnow input file name here; ing (Input Quit to EXIT): / N";

Filename;

}

Cout << "/ nthe total number of words in dictionary is:" << ptr-> count () << ENDL;

COUT << "/ ndo you want to see contents? (yes or no) / n";

CIN >> CHOICE;

IF (strcmp (choice, "yes") == 0)

{

Ptr-> Traverse (Defaultvisit, Preorder);

}

COUT << "/ ndo you want to rank the tree? (yes or no) / n";

CIN >> CHOICE;

IF (strcmp (choice, "yes") == 0) {

START = Time (0);

INT RANKCOUNT = 0;

Ranktree * rankptr = new ranktree;

Rankptr-> setfreq (ptr-> getfreq ());

Rankptr-> setname ((char *) PTR-> key ());

Ptr-> Traverse (INSERTRANK, (VOID *) Rankptr);

Rankptr-> Traverse (Visitrank, (Void *) (Rankcount), Maxorder, Rankptr

Cout << "/ NTOTAL TIME for Ranking IS:" << Time (0) - Start << Endl;

COUT << "Do you want to see result of ranking? / n";

CIN >> CHOICE;

IF (strcmp (choice, "yes") == 0)

{

Rankptr-> Traverse (Defaultvisit, Maxorder, Rankptr);

}

Cout << "/ nthe total number in ranktree is:" << rankptr-> count () << Endl;

}

Cout << "The Total Number of Words in Dictionary IS" << Ptr-> WordCount ();

Ptr-> Savetofile (DefaultBackup);

}

}

Return 0;

}

Int Wordtree :: WordCounter = 0;

Int Wordtree :: WordLength = 20;

Void insertrank (Tree * Node, Void * Userptr)

{

Ranktree * rankptr; //

Rankptr = (Ranktree *) ((Ranktree *) Userptr) -> Searchword ((Ranktree *) Userptr, (char *) Node-> key ())))));

IF (rankptr == null)

{

Rankptr = new ranktree;

Rankptr-> setName ((char *) node-> key ());

Rankptr-> setfreq (Node-> getFreq ());

((RankTree *) (Userptr)) -> Insert (Rankptr, (Ranktree *) Userptr);

}

}

Ranktree :: RankTree ()

{

Deccount ();

}

Void Visitrank (Tree * Node, Void * Userptr)

{

Node-> setRank (* (int *));

* (int *) (userptr) = 1;

}

Bool Ranktree :: Operator <(Tree & node)

{

Int result;

Result = getfreq () - node.getfreq ();

IF (Result! = 0)

{

Return (Result <0);

}

Else

{

Return (Strcmp ((char *) key (), (char *) node.key ()) <0);

}

}

Bool RankTree :: Operator> (Tree & node) {

Int result;

Result = getfreq () - node.getfreq ();

IF (Result! = 0)

{

Return (Result> 0);

}

Else

{

Return (Strcmp ((char *) key (), (char *) node.key ())> 0);

}

}

Void RankTree :: setRankTree (WordTree * WordRoot)

{

Ranktree * PTR;

IF (WordRoot! = null)

{

IF (this-> root ()! = null)

{

PTR = New Ranktree;

Ptr-> setfreq (WordRoot-> getFreq ());

Ptr-> setkey ((void *) WordRoot-> key ());

PTR-> INSERT (PTR);

}

Else

{

this-> set (this);

This-> setfreq (WordRoot-> getFreq ());

this-> setkey ((void *) wordroot-> key ());

}

SetrankTree ((Wordtree *) WordRoot-> Left ());

SetrankTree (Wordtree *) Wordroot-> Right ());

}

}

BinaryTree :: ~ binarytree ()

{

}

Wordtree :: ~ wordtree ()

{

}

Void Wordtree :: setWordCounter ()

{

WordCounter ;

}

Const int Wordtree :: getWordLength ()

{

Return WordLength;

}

Wordtree * Wordtree :: AddNode (Wordtree * Node, File ** Stream)

{

Char buffer [MAXWORDLENGTH];

Int index, pindex, number;

Wordtree * Domino;

Char * ptr = buffer, ch;

Fread ((Void *) (& INDEX), SIZEOF (Int), 1, * stream;

Fread ((Void *) (& Pindex), Sizeof (int), 1, * stream);

IF (index == - 1 && pindex == - 1)

{

Return NULL;

}

DO

{

CH = FGETC (* stream);

* PTR = CH;

PTR ;

}

While (ch! = '/ 0');

Fread ((Void *), Sizeof (int), 1, * stream);

SetWordCounter (); // add a word

IF (PINDEX == -1) // this is root

{

Node-> setIndex (INDEX);

Node-> setfreq (number);

Node-> setName (buffer);

Node-> Stroot (Node);

Return node;

}

While (PINDEX! = node-> getIndex ())

{

IF (Node-> Parent () == NULL)

{

Return NULL;

}

Node = (Wordtree *) Node-> Parent ();

}

IF (node! = null && pindex == node-> getIndex ()) {

Domino = New Wordtree;

Domino-> setIndex (Index);

Domino-> setName (buffer);

Domino-> setfreq (number);

Domino-> setParent (Node);

IF (* Domino <* node)

{

Node-> Setleft (Domino);

}

Else

{

Node-> setRight (Domino);

}

Return Domino;

}

Else

{

Return NULL;

}

}

Wordtree * Wordtree :: readnode (Wordtree * Node, File ** Stream)

{

Wordtree * Domino = Node;

While (Domino! = null)

{

Domino = addnode (domino, stream);

}

Return (Wordtree *) node-> root (); // this is ugly as i since ket - s ot

}

Wordtree * WordTree :: readfromfile (const char * filename)

{

File * stream;

IF ((stream = fopen (filename, "r b") == null)

{

COUT << "Unable to open file << filename << endl;

Return NULL;

}

Else

{

Wordtree * PTR = New Wordtree;

Return Readnode (PTR, & Stream);

}

Fclose (stream);

}

Void Wordtree :: Savenode (Wordtree * Node, File ** Stream)

{

Int first, second, Number;

First = node-> getIndex ();

Number = getfreq ();

FWRITE ((Void *) (& first), Sizeof (int), 1, * stream);

IF (Node-> Parent () == NULL)

{

SECOND = -1;

}

Else

{

Second = Node-> Parent () -> getIndex ();

}

FWRITE ((void *) (& Second), Sizeof (int), 1, * stream);

FWRITE ((void *) (Node-> Name ()), SIZEOF (CHAR), STRLEN (Node-> Name ()), * stream);

FPUTC ('/ 0', * stream); // i Have to Put this '/ n' Otherwise there is always problem!

FWRITE ((Void *), Sizeof (int), 1, * stream);

}

Void Traverse (Wordtree * Node, File ** Stream)

{

IF (Node! = NULL)

{

Node-> Savenode (Node, stream);

Traverseetree ((Wordtree *) Node-> Left (), stream;

Traverseetree (Wordtree *) Node-> Right (), stream;

}

Void Writend (file ** stream)

{

INT TEMP1 = -1, TEMP2 = -1;

FWRITE ((void *) (& TEMP1), SIZEOF (Int), 1, * stream);

FWRITE ((Void *), Sizeof (int), 1, * stream);

}

Void Wordtree :: Savetofile (const char * filename)

{

Void Traverse (Wordtree * Node, File ** Stream);

Void Writend (file ** stream);

Wordtree * Domino;

File * stream;

IF ((stream = fopen ("c: //nickback.txt", "w b")) == NULL)

{

Cout << "Unable to save to file" << filename << Endl;

}

Else

{

Domino = (Wordtree *) root ();

Traverseetree (Wordtree *) Domino, & Stream);

}

WriteEnd (& Stream);

Fclose (stream);

}

Const Int Tree :: count ()

{

Return Treecounter;

}

Void Wordtree :: setWordLength (const INT Newlength)

{

IF (Newlength> MaxWordLength)

{

COUT << "Exceed Max Word Length." << endl;

}

Else

{

WordLength = Newlength;

}

}

Int Wordtree :: Compare (const void * OtherKey)

{

Return strcmp ((char *) Key (), (char *) OtherKey);

}

Int Wordtree :: WordCount ()

{

Return WordCounter;

}

Wordtree :: WordTree ()

{

}

Void Wordtree :: OpenFile (const char * filename)

{

File * stream;

IF ((stream = fopen (filename, "r b") == null)

{

COUT << "Unable open file!" << endl;

}

Else

{

READWORD (& Stream);

}

Fclose (stream);

}

Void Wordtree :: readword (file ** stream)

{

Char buffer [MAXWORDLENGTH];

Char * ptr = buffer;

CHAR CH;

INT counter = 0;

While (! Feof (* stream))

{

PTR = Buffer;

COUNTER = 0;

CH = TouPper (FGETC (* stream));

While (Isalnum (CH) &&! Feof (* stream) && counter

{

* PTR = CH;

CH = TouPper (FGETC (* stream));

PTR ; Counter ;

}

IF (PTR! = Buffer)

{

* PTR = '/ 0';

AddWord (Buffer);

}

}

}

Wordtree * Wordtree :: Searchword (Wordtree * Node, Const Char * Buffer)

{

Int result;

IF (Node! = NULL)

{

Result = strcmp ((char *) (Node-> Key (), buffer;

IF (result == 0)

{

Return node;

}

Else

{

IF (Result <0)

{

Return Searchword (Wordtree *) Node-> Right (), Buffer;

}

Else

{

Return Searchword (Wordtree *) Node-> Left (), buffer;

}

}

}

Else

{

Return NULL;

}

}

Wordtree * WordTree :: Findword (const char * buffer)

{

Return SearchWord ((Wordtree *) root (), buffer;

}

Void WordTree :: Addword (char * buffer)

{

// NEED Add A Findword Method and Find First and Then New

Wordtree * PTR;

PTR = findword (buffer);

IF (PTR == Null)

{

PTR = New Wordtree;

Ptr-> setName (buffer);

IF (PTR-> Insert (PTR))

{

SetWordCounter ();

}

}

Else

{

PTR-> setfreq ();

}

}

Void Wordtree :: SetName (const char * str)

{

Tree :: setName (STR);

SetKey (Void *) Name ());

}

INT tree :: treecounter = 0;

Wordtree :: WordTree (const char * filename)

{

Wordtree :: WordTree ();

Openfile (filename);

}

Void Wordtree :: SetKey (Void * Newkey)

{

PKEY = newkey;

}

Bool Wordtree :: Operator <(Tree & Second)

{

IF (strcmp ((char *) key (), (char *) second.key ()) <0)

{

Return True;

}

Else

{

Return False;

}

}

BOOL Wordtree :: Operator == (Tree & Second)

{

IF (strcmp ((char *) key (), (char *) second.key ()) == 0)

{

Return True;

}

Else

{

Return False;

}

}

Bool Wordtree :: Operator> (Tree & Second)

{

IF (strcmp ((char *) keY (), (char *) second.key ())> 0)

{

Return True;

}

Else

{

Return False;

}

}

Void Defaultvisit (Tree * Node)

{

Cout << "/ n / n";

COUT << "default visiting tree index: << node-> getIndex () << endl; cout <<" and tree name is: "<< node-> name () << Endl;

Cout << "and its frequency is:" << (node) -> getfreq () << ENDL;

COUT << "and its rank is:" << node-> getrank () << Endl;

IF (node-> parent ()! = null)

{

COUT << "ITS Parent IS:" << node-> parent () -> name () << endl;

IF (node ​​== node-> parent () -> left ())

{

Cout << "and it is left Son." << endl;

}

Else

{

Cout << "And it is right Son << Endl;

}

}

Else

{

COUT << "IT HAS NO PARENT" << Endl;

}

Cout << "/ n / n";

}

Void Tree :: Prewalk (Tree * Node, Void (* Visit) (Tree * Node))

{

IF (Node! = NULL)

{

Visit (Node);

Prewalk (Node-> Left (), VISIT);

Prewalk (Node-> Right (), VISIT);

}

}

Void Tree :: Postwalk (Tree * Node, Void (* VID)))

{

IF (Node! = NULL)

{

Postwalk (Node-> Left (), VISIT);

Postwalk (Node-> Right (), VISIT);

Visit (Node);

}

}

Void Tree :: MaxWalk (Tree * Node, Void (* VIDE))

{

IF (Node! = NULL)

{

Maxwalk (Node-> Right (), VISIT);

Visit (Node);

Maxwalk (Node-> Left (), VISIT);

}

}

Void Tree :: Midwalk (Tree * Node, Void (* VID)

{

IF (Node! = NULL)

{

Midwalk (Node-> Left (), VISIT);

Visit (Node);

Midwalk (Node-> Right (), VISIT);

}

}

Void Tree :: Prewalk (Tree * Node, Void (* Visit) (Tree * Node, Void * Userptr), VOID * Userptr

{

IF (Node! = NULL)

{

Visit (Node, Userptr);

Prewalk (Node-> Left (), Visit, Userptr;

Prewalk (Node-> Right (), Visit, Userptr;

}

}

Void Tree :: Midwalk (Tree * Node, Void (* Visit) (Tree * Node, Void * Userptr), Void * Userptr) {

IF (Node! = NULL)

{

Midwalk (Node-> LEFT (), Visit, Userptr;

Visit (Node, Userptr);

Midwalk (Node-> Right (), VISIT, Userptr;

}

}

Void Tree :: Postwalk (Tree * Node, Void (* Visit) (Tree * Node, Void * Userptr), Void * Userptr

{

IF (Node! = NULL)

{

Postwalk (Node-> LEFT (), Visit, Userptr;

Postwalk (Node-> Right (), Visit, Userptr;

Visit (Node, Userptr);

}

}

Void Tree :: Maxwalk (Tree * Node, Void (* Visit) (Tree * Node, Void * Userptr), Void * Userptr

{

IF (Node! = NULL)

{

Maxwalk (Node-> Right (), VISIT, Userptr;

Visit (Node, Userptr);

Maxwalk (Node-> LEFT (), Visit, Userptr;

}

}

Void Tree :: Traverse (Void (* Visit) (Tree * Node, Void * Userptr), Void * Userptr,

TraverseOrder Defaultorder, Tree * Start)

{

Tree * Domino;

Domino = start;

Switch (DefaultOrder)

{

Case Preorder:

Prewalk (Domino, Visit, Userptr);

Break;

Case MIDORDER:

Midwalk (Domino, Visit, Userptr);

Break;

Case PostOrder:

Postwalk (Domino, Visit, Userptr);

Break;

Case Maxorder:

Maxwalk (Domino, Visit, Userptr);

Break;

}

}

Void Tree :: Traverse (Tree * Node), TraverseOrder Defaultorder, Tree * Start "VOISEORDER DefaultOrder

{

Tree * Domino;

Domino = start;

Switch (DefaultOrder)

{

Case Preorder:

Prewalk (Domino, Visit);

Break;

Case MIDORDER:

Midwalk (Domino, Visit);

Break;

Case PostOrder:

Postwalk (Domino, Visit);

Break;

Case Maxorder:

Maxwalk (Domino, Visit);

Break;

}

}

Tree * Tree :: Proot = NULL;

Void binarytree :: remove (binarytree * node)

{

BinaryTree * Ptr = NULL;

IF (node-> left () == null || node-> right () == NULL)

{

IF (node-> left ()! = null)

{

Node-> parent () -> setleft (node-> left ());

Node-> left () -> setparent (node-> parent ());

IF (node-> right ()! = null)

{

Node-> parent () -> set-> right ());

Node-> right () -> setparent (node-> parent ());

}

}

Else

{

PTR = Node-> next ();

REMOVE (PTR);

Ptr-> setParent (node-> parent ());

IF (Node-> Parent () == NULL)

{

Node-> setRoot (PTR);

}

Else

{

IF (node ​​== node-> parent () -> left ())

{

Node-> Parent () -> SETLEFT (PTR);

}

Else

{

Node-> parent () -> setRight (PTR);

}

Ptr-> setleft (node-> left ());

Ptr-> setRight (node-> right ());

}

}

}

BOOL Tree :: Operator == (Tree & Second)

{

IF (this-> compare (second.key ()) == 0)

{

Return True;

}

Else

{

Return False;

}

}

BOOL Tree :: Operator <(Tree & Second)

{

IF (this-> compare (second.key ()) <0)

{

Return True;

}

Else

{

Return False;

}

}

BOOL Tree :: Operator> (Tree & Second)

{

IF (this-> compare (second.key ())> 0)

{

Return True;

}

Else

{

Return False;

}

}

INT tree :: Compare (const void * OtherKey)

{

RETURN * ((int *) (key ())) - * (INT *);

}

Void * tree :: key ()

{

Return PKEY;

}

Void Tree :: SetKey (Void * Newkey)

{

PKEY = malloc (sizeof (int));

* (int *) = * (int *);

}

Void Tree :: Stroot (Tree * Node)

{

ProT = Node;

}

Bool binarytree :: insert (binarytree * node, binarytree * start)

{

Tree * Ptr, * Dummy;

PTR = START;

Dummy = PTR;

While (PTR! = null)

{

Dummy = PTR;

IF (* ptr> * node)

{

PTR = Ptr-> Left ();

}

Else

{

IF (* ptr <* node)

{

PTR = Ptr-> Right ();

}

Else

{

IF (* ptr == * node)

{

Return False;

}

}

}

}

Node-> setParent (DUMMY);

IF (dummy == null)

{

Stroot (Node);

}

Else

{

// node-> set (Dummy-> root ()); if (* Dummy> * Node)

{

Dummy-> setleft (node);

}

Else

{

IF (* Dummy <* node)

{

Dummy-> setRight (Node);

}

} // if "==", DONOT INSERT;

}

Return True;

}

Void Tree :: setleft (Tree * Leftson)

{

PLEFT = Leftson;

}

Void Tree :: Setright (Tree * Rightson)

{

PRIGHT = Rightson;

}

Void Tree :: setParent (Tree * Father)

{

PParent = Father;

}

void tree :: Givebpter ()

{

Tree * Lefttree = New Tree;

Tree * righttree = new tree;

Char Str [128];

Lefttree-> setIndex (this-> getIndex () * 2);

Righttree-> setIndex (this-> getIndex () * 2 1);

This-> setleft (lefttree);

This-> setRight (Righttree);

Lefttree-> setParent (this);

Righttree-> setParent (this);

STRCPY (STR, "LEFT SON OF");

STRCAT (STR, this-> name ());

Lefttree-> setName (STR);

STRCPY (STR, "Right Son of");

STRCAT (STR, this-> name ());

Righttree-> setName (STR);

Lefttree-> setParent (this);

Righttree-> setParent (this);

IF (root () == null)

{

Stroot (this);

SetParent (null);

}

/ *

Else

{

LEFTTREE-> SETROOT (Root ());

Righttree-> setRoot (root ());

}

* /

}

Void Tree :: SetIndex (const int number)

{

Index = Number;

}

Const int tree :: getIndex ()

{

Return Index;

}

Const char * tree :: name ()

{

Return PNAME;

}

Void Tree :: SetName (const char * str)

{

IF (str! = null)

{

PNAME = (char *) Malloc (Strlen 1);

STRCPY (PNAME, STR);

}

}

Tree :: Tree ()

{

SetIndex (TreeCounter);

Treecounter ;

PLEFT = NULL;

PRIGHT = NULL;

PParent = NULL;

PNAME = NULL;

PKEY = NULL;

FREQ = 0;

Rank = 0;

}

Tree :: ~ Tree ()

{

IF (PNAME! = NULL)

{

Free (PNAME);

}

IF (PKEY! = NULL)

{

Free (PKEY);

}

Tree * tree :: left ()

{

Return PLEFT;

}

Tree * tre :: right ()

{

RETURN PRIGHT;

}

Tree * Tree :: Parent ()

{

Return PParent;

}

BinaryTree * binarytree :: next ()

{

Tree * result = null;

IF (right ()! = null)

{

Result = right ();

While (result-> left! = null)

{

Result = result-> left ();

}

}

Else

{

IF (parent ()! = null)

{

Result = THIS;

While (result-> parent ()! = null& results == result-> parent () -> right ())

{

Result = result-> parent ();

}

}

// Result = null

}

Return (binarytree *) result;

}

BinaryTree * binarytree :: prev ()

{

Tree * result = null;

IF (left ()! = null)

{

Result = left ();

While (result-> right ()! = null)

{

Result = result-> right ();

}

}

Else

{

IF (parent ()! = null)

{

Result = THIS;

While (result-> parent ()! = null && results == result-> parent () -> left ())

{

Result = result-> parent ();

}

}

}

Return (binarytree *) result;

}

http://www3.sympatico.ca/qingzhehuang/superDictionary.htm

转载请注明原文地址:https://www.9cbs.com/read-23849.html

New Post(0)