Пример #1
0
void InsertTree( int insertData, NODE* leafNode )
{
	if ( leafNode->data == NOT_NUM )
	{
		leafNode->data = insertData;
		return;
	}

	if ( insertData < leafNode->data )
	{
		if ( leafNode->leftChild )
		{
			InsertTree( insertData, leafNode->leftChild );
		}
		else
		{
			leafNode->leftChild = new NODE;
			leafNode->leftChild->data = insertData;
		}
	}
	else if ( insertData >= leafNode->data )
	{
		if ( leafNode->rightChild )
		{
			InsertTree( insertData, leafNode->rightChild );
		}
		else
		{
			leafNode->rightChild = new NODE;
			leafNode->rightChild->data = insertData;
		}
	}
}
Пример #2
0
void InsertTree(struct TALNode** root,int data){
    if(!(*root)){
struct  TALNode* newNode=(struct TALNode*)malloc(sizeof(struct TALNode));
newNode->data=data;
newNode->large=NULL;
newNode->small=NULL;
(*root)=newNode;
    }
    else if(data<(*root)->data){
    InsertTree(&(*root)->small,data);
    }else{
    InsertTree(&(*root)->large,data);
    }


}
Пример #3
0
void DlgCScenarioManager::OnCreatChild() 
{
	// TODO: Add your command handler code here
	HTREEITEM hItem = m_tree.GetSelectedItem();
	if(hItem == NULL)
		return;
    //根据key值找到对应的工况
	DWORD key = m_tree.GetItemData(hItem);
	Scenario *pScenario = GetCurScenario();
	
	DlgScenarioName dlg;
	if(IDOK == dlg.DoModal())
	{
		//检查名称没有存在子工况则增加一个子工况
		CString strName = dlg.GetName();
		if(pScenario->CheckName(strName))
		{
			InsertTree(m_manager.AddChildScenario(pScenario,strName),hItem);
			InitPage();
			UpdateData(FALSE);
		}
		else
		{
			AfxMessageBox(IDS_NAMEERROR);
		}
	}

}
Пример #4
0
int main(void)
{
    objekt *start,ny,*tmp;
    int k;
    int cnt = 0;
    srand(time(NULL));
    int s;
    for(s=0;s<1000;s++) {
        start=NULL;
        for(k=1; k<=1000; k++)
        {
            ny.nyckel = rand()%1000000+1;
            ny.left=NULL;
            ny.right=NULL;
            start=InsertTree(start,ny);
        }
        if(height(start) == 20) {
            cnt++;
        }
        while (start!=NULL)
            DeleteTree(&start);
    }
    printf("\n\nTotal times tree height was 20 (of 1000): %d\n",cnt);
    float perc = ((float)cnt/10.0);
    printf("Percent: %.2f %\n",perc);
}
Пример #5
0
// Rekusiv insättning
objekt *InsertTree(objekt *p,objekt ny)
{
    objekt *z,*q;

    if(p==NULL)
    {
        z=(objekt *) malloc(sizeof(objekt));
        *z=ny;
        z->left=NULL;
        z->right=NULL;
        return z;
    }
    else if(ny.nyckel < p->nyckel)
        p->left=InsertTree(p->left,ny);
    else
        p->right=InsertTree(p->right,ny);
    return p;
}
Пример #6
0
objekt *InsertTree(objekt *p,objekt ny){
	objekt *z;

	if(p==NULL){
		z=(objekt *) malloc(sizeof(objekt));
		*z=ny;
		z->left=NULL;
		z->right=NULL;
		return z;
	} else {
		if(ny.tal<p->tal) {
			p->left=InsertTree(p->left,ny);
		} else {
			p->right=InsertTree(p->right,ny);
		}
	}
	return p;
}
Пример #7
0
/*  InsertTree: insere um novo noh na arvore.newnode e x ==> sao
      os dados dessa ABBcontudo, a inclusao mantem as propriedadesda ABB,
       tal que a chave principal eh newnode*/
   arv_bin *InsertTree(arv_bin *root, char * newnode, int x)
   {
      if (!root)
      { /*    char *strcpy(char *dest, const char *src);
       */
         root = aloca_no();
         strcpy(root -> info , newnode);
         root -> ordem = x;
         root->left = root->right = NULL;
      }
      else if (LT( newnode , root -> info ))
         root->left = InsertTree(root->left, newnode, x);
      
      else
         root->right = InsertTree(root->right, newnode, x);
   
      return (root);
   }
Пример #8
0
void DlgCScenarioManager::InitTree(Scenario* pScenario,HTREEITEM htreeitem)
{
	HTREEITEM temp = InsertTree(pScenario,htreeitem);
	Scenario *pChild = NULL;
	IteratorPtr<Scenario> iteratorPtr(pScenario->CreatIterator());
	for(iteratorPtr->Fist();!iteratorPtr->IsDone();iteratorPtr->Next())
	{
		pChild = &iteratorPtr->CurrentItem();
		InitTree(pChild,temp);
	}
}
Пример #9
0
void main()
{
	BinaryTree *tree = CreateBinaryTree();
	ElementTree item;

	item.data = 30;
	InsertTree(tree, item);
	item.data = 5;
	InsertTree(tree, item);
	item.data = 40;
	InsertTree(tree, item);
	item.data = 2;
	InsertTree(tree, item);
	item.data = 35;
	InsertTree(tree, item);
	item.data = 80;
	InsertTree(tree, item);

	Preorder(tree->root);
	printf("\n");
	item.data = 80;
	DeleteTreeNode(tree->root, item);

	Preorder(tree->root);

}
Пример #10
0
objekt *Init(void){
	objekt p,*start;
	int i,t,n;
	FILE *infil;

	infil=fopen("uppg5.dat","rt");
	fscanf(infil,"%d",&n);
	start=NULL;
	for(i=1; i<=n; i++){
		fscanf(infil,"%d",&t);
		p.tal=t;
		p.right=NULL;
		p.left=NULL;
		start=InsertTree(start,p);
	}
	fclose(infil);
	return start;
}
Пример #11
0
int main( void )
{
	NODE* rootNode = new NODE;

	int nodeTheNum = 5;

	for ( int i = 0; i < nodeTheNum; ++i )
	{
		InsertTree( rand() % 100, rootNode );
	}

	int calcedNodeNum = 0;
	calcedNodeNum = CountNode( rootNode );

	printf_s( "%d", calcedNodeNum );

	getchar();
	return 0;
}
Пример #12
0
//----------------------------------------------------------------------------
//
// InsertTree
//
// Insert a color into the octree
//
void InsertTree(OctreeType **tree, RGBType *color, uint depth)
{
    int level;

    if (*tree == (OctreeType *)NULL) {
        *tree = CreateOctNode(depth);
	}
    if ((*tree)->isleaf) {
        (*tree)->npixels++;
        (*tree)->redsum += color->r;
        (*tree)->greensum += color->g;
        (*tree)->bluesum += color->b;
	}
	else {
		InsertTree(&((*tree)->child[LEVEL(color, TREEDEPTH-depth)]),
				   color,
				   depth+1);
	}
}
Пример #13
0
int main(){

printf("\nCreating Tree...");
getch();
int i,data;
struct TALNode* root=NULL;
for(i=0;i<5;i++){
    printf("\nEnter data : ");
    scanf("%d",&data);
InsertTree(&root,data);
}

printf("%d nodes inserted ... \n");
getch();
printf("\nEnter to traverse...\n\n");
TraverseTree(root);

getch();

printf("\nEnter to convert BT to DLL\n");
getch();
root=ConvertBT(root);
printf("\nCreated...\n");
printf("\nEnter to traverse \n\n");


getch();

TraverseDLL(root);
getch();




return 0;
}
Пример #14
0
void main(int argc, char *argv[])
{
	double      r, g, b;
	uint        rows, cols;
	char        fname[256];
	double      colormag;
	time_t      tstart, tend;
    int         nrgbr = 63, nrgbg = 63, nrgbb = 63;
	OctreeType  *octree;
	RGBType		color;
	FILE		*f;
	char		title[40];
	char		description[128];
    ulong       i;
    uint        j;
	int			n;
	RGBType		palette[256];
	ulong		image_start;
	union REGS	regs;
	int			resx, resy;
	int			px, py;
	int			ii;
	int			cols2, rows2;
	int			k;
	int			cli;
    int         maxr=0, maxg=0, maxb=0;

#if defined METAWINDO
	rect        screen;
#endif

	printf("Image file : ");
	scanf("%s",fname);
	if ((f = fopen(fname,"rb")) == NULL) {
        printf("%s not found.\n",fname);
		exit(1);
	}

    /*
    ** Read the image file header
    */
	fgets(title,40,f);
	fgets(description,128,f);
	fscanf(f,"%d %d",&cols,&rows);
    fscanf(f,"%lf",&colormag);
	image_start = ftell(f);

	cols2 = cols/2;
	rows2 = rows/2;
	time(&tstart);

	/*
    ** Initialize the color octree
	*/
    octree = CreateOctNode(0);

	/*
    ** Loop through the image and store each unique color.
	*/
	for (i = 0L; i < (ulong)rows*(ulong)cols; i++) {
		/*
		** Show progress...
		*/
		if ((i % (ulong)cols) == 0L) printf("%ld\r",i/cols);

        fscanf(f,"%lf %lf %lf",&r,&g,&b);
		/*
		** Convert input floating point values to bytes.  NOTE: We assume that
		** all input values are between 0..1.0
		*/
        color.r = (unsigned char)(r  * nrgbr);
        color.g = (unsigned char)(g  * nrgbg);
        color.b = (unsigned char)(b  * nrgbb);
		if (color.r > nrgbr) color.r = nrgbr;
		if (color.g > nrgbg) color.g = nrgbg;
		if (color.b > nrgbb) color.b = nrgbb;

		/*
		** Insert this color into the octree
		*/
		InsertTree(&octree, &color, 0);

		/*
		** If there are too many colors in the tree as a result of this
		** insert, reduce the octree
		*/
		while (TotalLeafNodes() > npal) {
			ReduceTree();
		}
	}

	/*
	** Make a pass through the completed octree to average down the
	** rgb components.  When done, 'n' contains the actual number of
	** colors in the palette table.
	*/
	n = 0;
	MakePaletteTable(octree, palette, &n);

	/*
	** How long did it take?
	*/
	time(&tend);
	printf("Processed %ld pixels per second\ninto %d quantized colors\n",
		   ((long)rows*(long)cols)/(tend-tstart), n);

	j = 0;
	while (j != 3) {
		printf("Output to (1)monitor or (2).PCX file or (3) quit: ");
		scanf("%s",title);
		j = atoi(title);
		if (j == 2) {
			fseek(f,image_start,0);
            SaveAsPCX(f, octree, cols, rows, nrgbr, nrgbg, nrgbb, npal, palette);
		}
		else if (j == 1) {
#if defined METAWINDO
			/*
			** NOTE: This section requires MetaWINDOW graphics lib
			**
			** Let the user choose his graphics device and resolution
			*/
			MetQuery(argc,argv);
			if (InitGraphics(GrafixCard) != 0) {
				printf("\n---Error initializing graphics device---\n");
				exit(1);
			}
			SetDisplay(GrafPg0);
			BackColor(0);
			/*
			** Set the VGA palette
			*/
			for (j = 0; j < n; j++) {
				regs.h.al = 0x10;
				regs.h.ah = 0x10;
				regs.h.bl = j;
				regs.h.bh = 0;
				regs.h.ch = (int)(palette[j].g);
				regs.h.cl = (int)(palette[j].b);
				regs.h.dh = (int)(palette[j].r);
				int86(0x10,&regs,&regs);
			}

			/*
			** Center the image on the screen
			*/
			ScreenRect(&screen);
			resx = screen.Xmax;
			resy = screen.Ymax;
			px = resx/2 - npal;

			/*
			** Display a color bar at the top of the screen
			*/
			for (ii = 0; ii < npal; ii++){
				PenColor(ii);
				SetPixel(ii*2+px,1);
				SetPixel(ii*2+px+1,1);
				SetPixel(ii*2+px,2);
				SetPixel(ii*2+px+1,2);
				SetPixel(ii*2+px,3);
				SetPixel(ii*2+px+1,3);
				SetPixel(ii*2+px,4);
				SetPixel(ii*2+px+1,4);
			}

			fseek(f,image_start,0);

			py = resy/2 - rows2 - 1;
			for (ii = 0; ii < rows ; ii++) {
				px = resx/2 - cols2;
				for (k = 0; k < cols; k++) {
					if (fscanf(f,"%f %f %f",&r,&g,&b) == EOF) {
						goto pdone;
					}
					color.r = (byte)(nrgbr * r);
					color.g = (byte)(nrgbg * g);
					color.b = (byte)(nrgbb * b);
					cli = QuantizeColor(octree, &color);
					PenColor(cli);
					SetPixel(px,py);
					px++;
				}
				py++;
			}
pdone:      getch();
			SetDisplay(TextPg0);
			StopGraphics();
#endif
		}
	}
}