⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 treeclient.c

📁 Binary Search Tree - with additional recursion functions (smallest, parent & successor) etc
💻 C
字号:
//TreeClient.c//An example of client for tree.#include <time.h>#include <stdlib.h>#include "BinaryTree.h"#define SUCCESS (-42)// in the traversals - for each item we invoke 'job'void job(Item *item){    printf("%d  ", item->num);}// prints details about the Recordvoid printWorker(workerRec *worker){    printf("\nThe record's details are: num = %d name ="       "%s", worker->num, worker->name);}// buildTreeFromVec creates a tree from the workers// vector it uses the add function for all of the// items returns the new treeTree buildTreeFromVec(workerRec *workersArr, int vecsize){    int i;	Tree theTree = CreateTree();	// add data to the tree.	for (i = 0; i < vecsize; ++i)	{	   add(theTree, workersArr[i].num, workersArr[i]);	}	return theTree;}void main(){   Tree theTree;   Key key;   Item *pItem;	int choice = -1;	int number = 0;	int succ; // will indicate if the key we want to cancel apear on the tree	workerRec workersArr[] ={													{5,"Mickey Mouse"},													{7,"Buggs Bunny"},													{3,"Donald Duck"},													{6,"Duffy Duck"} ,													{2,"Yogi Bear"},													{9,"Olive Oil"},													{0,"Silvester Cat"},													{4,"Goofy Long"},													{8,"Garfield Fat"},													{1,"Pluto Doggy"}		};	theTree = buildTreeFromVec(workersArr, sizeof(workersArr) / sizeof(workersArr[0]));	printf("\n\nBeginning of binary tree program");//================================================================================// begin of the menu with all choices//================================================================================	while (choice)	{		printf ("\n\n\nWelcome, please enter your choice:\n"						"==================================\n"						"0 - Exit from this program! Good Bye!\n"						"-------------------------------------\n"						"1 - The keys of the records in the tree in PRE_ORDER\n"						"2 - The keys of the records in the tree in IN_ORDER\n"						"3 - The keys of the records in the tree in POST_ORDER\n"						"4 - Find a randomized Item\n"						"5 - Delete an specific Item by key.\n"						"6 - Delete the Tree\n");		printf("\n\nYour choice -> ");		choice=getchar();		getchar();		fflush(stdin);		switch (choice)		{			case '0':			{				printf("\n\nEnd of the program \n");				exit (SUCCESS);			}			case '1':  // print in Pre Order			{				if (theTree)				{					printf("\n\nThe keys of the records in the tree in PRE_ORDER :\n");					traversePreOrder(theTree, job);					break;				}				else				{					printf("\n\nSorry here is no tree anymore!\n");				}			}			case '2': // print in In Order			{				if (theTree)				{					printf("\n\nThe keys of the records in the tree in IN_ORDER :\n");					traverseInOrder(theTree, job);				}				else				{					printf("\n\nSorry here is no tree anymore!\n");				}				break;			}			case '3': // print in Post Order			{				if (theTree)				{					printf("\n\nThe keys of the records in the tree in POST_ORDER :\n");					traversePostOrder(theTree, job);					break;				}				else				{					printf("\n\nSorry here is no tree anymore!\n");				}			}			case '4': // Find an randomize			{				if (theTree)				{					srand((unsigned) time(0));					// for generation of random numbers using the time					// of the system					key = rand()% (sizeof(workersArr) / sizeof(workersArr[0]));						// a random key-num for searching						// Now we will search in the tree the record						// with this key					pItem = Find(theTree, key);					if (pItem)					{						printf("\n\nThe record %d is found in the tree.",key);						printWorker(pItem);					}					else					{						printf("\n\nThe recrord %d is NOT found in"									 "the tree.",key);					}				}					break;			}			case '5': // Delete an specific Item by key.			{				if (theTree)				{					printf("\nEnter your Key please:");					scanf("%d",&number);					getchar();					succ=deleteNode(theTree, number);					if((theTree != NULL) && (succ))					{						printf("The requested Node was deleted.\n");						printf("\n\nThe keys of the records in the tree after the Deleted Key in POST_ORDER:\n");						traversePreOrder(theTree, job);					}					else					{						printf("\n\nSorry, The Key you requested does not apear on the tree!\n");					}				}				else				{					printf("The Tree doesn't exist.\n");				}				break;			}			case '6': // Delete the Tree			{				if (theTree)				{					deleteTree(theTree);					// the tree was deleted					theTree = NULL;					if (!theTree)					{						printf("The Tree Was deleted\n");					}					else					{						printf("\nThe Tree could not be deleted\n");					}				}				else				{					printf("The Tree Was already deleted\n");				}				break;			}		}			// end switch	}				// end while for menu}					// end main

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -