binaryt.c

来自「c compiler with added projects...」· C语言 代码 · 共 126 行

C
126
字号
struct Node
{

	int val;
	struct Node *left;
	struct Node *right;
	struct Node *parent;

};


void main()
{
	int option,count=0,i;
	struct Node *root='\0';
	struct Node *current='\0';
	struct Node *temp='\0';



	while(option!=5)
	{
			clrscr();
			printf("Prees 1 for Add\n");
			printf("Press 2 for Show\n");
			printf("COUNT %d ",count);

			scanf("%d",&option);

			if(option==1)
			{
				current = (struct Node *) malloc(sizeof(struct Node));
				current->left=current->right=current->parent='\0';
				printf("Enter Value");
				scanf("%d",&current->val);

				if(root=='\0')
				{
					root=current;
					count++;
				}
				else
				{
					temp=root;
					while(1)
					{
						if(current->val>temp->val)
							if(temp->right=='\0')
							{
								temp->right=current;
								current->parent=temp;
								count++;
								break;
							}
							else
							temp=temp->right;

						if(current->val<=temp->val)
							if(temp->left=='\0')
							{
								temp->left=current;
								current->parent=temp;
								count++;
								break;
							}
							else
							temp=temp->left;

					}

				}
			} //1st IF statment end

			if(option==2)
			{
				temp=root;

			while(1)
			{
				  if((count==1)&&(temp->parent=='\0'))
						{
							printf("Value of Node is %d  ",temp->val);
							free(temp);
							break;
						}
						else
						{

							while(1)
							{

							if((temp->left=='\0')&&(temp->right=='\0'))
							break;

							if(temp->left!='\0')
								  temp=temp->left;
								  else
								  temp=temp->right;
							}
						}

					current=temp;
					current=current->parent;
					printf("Value of Node is %d  ",temp->val);

					if(temp->val<=current->val)
						current->left='\0';
					else
						current->right='\0';
						free(temp);
						count--;
						temp=current;
				}
			getch();

			}//2nd IF statment end

			if(option==5)
			{
				printf("ALLAH Hafiz");
				getch();
				break;
			}
	}	//Main while loop end
}

⌨️ 快捷键说明

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