// node.cpp: implementation of the K_node, GK_node 
// classes.
//////////////////////////////////////////////////////////////////////

#include <stdlib.h>
//#include "node.h"
#include "tree.h"
#include "common.h"


//////////////////////////////////////////////////////////////////////
// Functions of K_node Class
//////////////////////////////////////////////////////////////////////

K_node::K_node(K_node* parent, int itemname)
{
	par = parent;
	itemName = itemname;
	leftchild = NULL;
	rightsibling =NULL;
	next = NULL;
}

void K_node::init(K_node* parent, int itemname)
{
	par = parent;
	itemName = itemname;
	leftchild = NULL;
	rightsibling =NULL;
	next = NULL;
}

K_node::~K_node()
{
	K_node* temp, *temp1;;
	if(this->leftchild!=NULL)
	{
		temp=this->leftchild;
		temp1=temp->rightsibling;
		while(temp1!=NULL)
		{
			temp=temp1->rightsibling;;
			delete temp1;
			temp1=temp;
		}
		delete this->leftchild;
	}
}

K_node* K_node::append(K_tree *ktree, K_node *sib, int itemno)
{
	K_node* child;

	child = (K_node*)buf->newbuf(1, sizeof(K_node));
	child->init(this, itemno);
	
	if(this->leftchild==NULL)this->leftchild=child;
	else
		sib->rightsibling = child;
	
	return child;
}

//////////////////////////////////////////////////////////////////////
// Functions of GK_node Class
//////////////////////////////////////////////////////////////////////

GK_node::GK_node(GK_node* parent, int itemname)
{
	par = parent;
	itemName = itemname;
	leftchild = NULL;
	rightsibling =NULL;
	next = NULL;

	blborder = false;
	index = -1;
	support = 0;
	negNext = NULL;
}

void GK_node::init(GK_node* parent, int itemname)
{
	par = parent;
	itemName = itemname;
	leftchild = NULL;
	rightsibling =NULL;
	next = NULL;

	blborder = false;
	index = -1;
	support = 0;
	negNext = NULL;
}

GK_node::~GK_node()
{
	GK_node* temp, *temp1;;
	if(this->leftchild!=NULL)
	{
		temp=this->leftchild;
		temp1=temp->rightsibling;
		while(temp1!=NULL)
		{
			temp=temp1->rightsibling;;
			delete temp1;
			temp1=temp;
		}
		delete this->leftchild;
	}
}

GK_node* GK_node::append(GloK_tree *ktree, GK_node *sib, int itemno)
{
	GK_node* child;

	child = (GK_node*)buf->newbuf(1, sizeof(GK_node));
	child->init(this, itemno);
	
	if(this->leftchild==NULL)this->leftchild=child;
	else
	{
		if(sib)
		{
			child->rightsibling = sib->rightsibling; //insert
			sib->rightsibling = child;
		}
		else
		{
			child->rightsibling = this->leftchild; //insert
			this->leftchild = child;
		}
		
	}
	
	return child;
}

//////////////////////////////////////////////////////////////////////
// Functions of C_node Class
//////////////////////////////////////////////////////////////////////
C_node::C_node(C_node* parent, int itemname)
{
	par = parent;
	rightsibling = NULL;
	leftchild = NULL;
	next = NULL;

	itemName = itemname;
	index = -1;
	level = -255;
}


C_node::~C_node()
{
	C_node* temp, *temp1;;
	if(this->leftchild!=NULL)
	{
		temp=this->leftchild;
		temp1=temp->rightsibling;
		while(temp1!=NULL)
		{
			temp=temp1->rightsibling;;
			delete temp1;
			temp1=temp;
		}
		delete this->leftchild;
	}
}

void C_node::init(C_node* parent, int itemname)
{
	par = parent;
	rightsibling = NULL;
	leftchild = NULL;
	next = NULL;

	itemName = itemname;
	index = -1;
}

C_node* C_node::append(C_tree *ctree, C_node *sib, int itemno, int lvl)
{
	C_node* child;

	child = (C_node*)buf->newbuf(1, sizeof(C_node));
	child->init(this, itemno);
	
	if(this->leftchild==NULL)this->leftchild=child;
	else
		sib->rightsibling = child;

	child->level = lvl;

	C_node * linkHead=ctree->header[itemno];
	child->next = linkHead->next;
	linkHead->next = child;
	
	return child;
}



