📄 classtree.cpp
字号:
// lassTree.cpp: implementation of the ClassTree class.
//
//////////////////////////////////////////////////////////////////////
#include "ClassTree.h"
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
ClassTree::ClassTree()
{
root=current=NULL;
}
ClassTree::~ClassTree()
{
}
bool ClassTree::FirstChild()
{
if(current!=NULL&¤t->pFirstChild!=NULL)
{
current=current->pFirstChild;
return 1;
}
return 0;
}
bool ClassTree::NextSibling()
{
if(current!=NULL&¤t->pNextSibling!=NULL)
{
current=current->pNextSibling;
return 1;
}
return 0;
}
bool ClassTree::Parent()
{
if(current==NULL||current==root)
{
current=NULL;
return 0;
}
current=current->pParent;
return 1;
}
bool ClassTree::Root()
{
if(root==NULL)
{
current=NULL;
return 0;
}
else
{
current=root;
return 1;
}
}
void ClassTree::BuildRoot(CString strKindName)
{
root=current=new ClassTreeNode(NULL,strKindName); //create root node
}
bool ClassTree::InsertChild(int nDimOfVector,CString strKindName)
{
//intsert a new node under the current node ,if the current node have no child,
//set the new node as firstchild,if not,set the new node as the most right child
if(current==NULL)
return false;
ClassTreeNode * newNode=new ClassTreeNode(nDimOfVector,strKindName);
if(current->pFirstChild==NULL)
{
current->pFirstChild=newNode;
newNode->pParent=current;
}
else
{
ClassTreeNode *p=current->pFirstChild;
while(p->pNextSibling!=NULL)
p=p->pNextSibling;
p->pNextSibling=newNode;
newNode->pParent=current;
}
return true;
}
bool ClassTree::RemoveTree()
{
if(root==NULL)
return false;
else
{
RemoveSubTree(root);
return true;
}
}
void ClassTree::RemoveSubTree(ClassTreeNode *p)
{
ClassTreeNode *q=p->pFirstChild;
ClassTreeNode *next;
while(q!=NULL)
{
next=q->pNextSibling;
RemoveSubTree(q);
q=next;
}
delete p;
}
bool ClassTree::InsertSibling(int nDimOfVector,CString strKindName)
{
if(current==NULL)
return false;
ClassTreeNode * newNode=new ClassTreeNode(nDimOfVector,strKindName);
current->pNextSibling=newNode;
newNode->pParent=current->pParent;
return true;
}
void ClassTree::SavePreOrder(FILE *fp)
{
//just take current node as root,in order to visit with preorder
if(current!=NULL)
{
SaveNode(fp);
ClassTreeNode *p=current;
bool i=FirstChild();
while(i)
{
SavePreOrder(fp);
i=NextSibling();
}
current=p;
}
}
void ClassTree::PostOrder()
{
if(current!=NULL)
{
ClassTreeNode *p=current;
bool i=FirstChild();
while(i)
{
PostOrder();
i=NextSibling();
}
current=p;
// visit(fp);
}
}
void ClassTree::SaveNode(FILE *fp)
{
//if current node is root,not save
if(current==root)
return;
int NumOfVector=current->nDimOfVector;
fwrite(&NumOfVector,sizeof(int),1,fp);
POSITION rPosition=current->WordVector.GetHeadPosition();
CListNode &pNode=current->WordVector.GetHead();
for(int i=0;i<NumOfVector;i++)
{
fwrite(&pNode.WordIndex,sizeof(int),1,fp);
fwrite(&pNode.Weight,sizeof(float),1,fp);
pNode=current->WordVector.GetNext(rPosition);
}
}
int ClassTree::GetChildNum()
{
if(current==NULL)
return -1;
if(!FirstChild())
return 0;
int NumOfChild=1;
while(NextSibling())
NumOfChild++;
Parent();
return NumOfChild;
}
void ClassTree::SaveTree(FILE *fp)
{
//move to root
Root();
//start to preorder
SavePreOrder(fp);
}
void ClassTree::UpdateTree(FILE *fp)
{
//move to root
Root();
//start to preorder
UpdatePreOrder(fp);
}
void ClassTree::UpdatePreOrder(FILE *fp)
{
//just take current node as root,in order to visit with preorder
if(current!=NULL)
{
UpdateNode(fp);
ClassTreeNode *p=current;
bool i=FirstChild();
while(i)
{
UpdatePreOrder(fp);
i=NextSibling();
}
current=p;
}
}
void ClassTree::UpdateNode(FILE *fp)
{
//if current node is root,not update
if(current==root)
return;
//read nDimOfVector
int NumOfVector;
fread(&NumOfVector,sizeof(int),1,fp);
current->nDimOfVector=NumOfVector;
//add CListNode to the WordVector
CListNode node;
//read the weight list
for(int i=0;i<NumOfVector;i++)
{
fread(&node.WordIndex,sizeof(int),1,fp);
fread(&node.Weight,sizeof(float),1,fp);
current->WordVector.AddTail(node);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -