الفريق العربي للبرمجةأرشيف المنتديات · 2000 – 2023
نسخة أرشيفية للقراءة فقط — التسجيل والمشاركة مغلقان، والمحتوى محفوظ كما كان.

AVL TREE

بدأه سروا في 27 يناير 2010 · 10 رد · 1,593 مشاهدة · في الأسئلة المجابة
مشاركة: واتساب X فيسبوك تيليجرام
#1 صاحب الموضوع

السلام عليكم

اريد مساعدتكم في تصحيح هدا الكود.حاولت فيه بس مايتنفد ياريت تساعدوني ولكم مني خالص الشكر. الكود اشوي طويل تجدوه في المرفقات

assie1.rar

#2

ممكن توضيح أكتر؟

شو اللي مش شغال؟ في اي دالة بتواجه مشكلة؟.

هل يعمل البرنامج لكن النتائج مش صحيحة؟ ولا لا يعمل ابدا؟

هل الحديث عن AVL ولا Binary tree عادي؟

#3

انا مشكلتي في الاكواد مااعرف الخطا وين . هده الاكواد بال binary tree بس انا اريدها بال Avl tree . بس مش مشكلة المهم اعرف الخطا فيها كاbinary tree.

ولك مني خالص الشكر. وبارك الله فيك على الرد

#4

مشروعك بالسي : ليس فيه أخطأ فقط دالة المسح clrscr بدلتها system("cls")

وهذا الكود كامل :

#include<stdio.h>
#include<stdlib.h>
#include<time.h>
#include<conio.h>
#define size 50


struct node
{
 int data;
 struct node *left, *right;
};

typedef struct node Node;
typedef Node * TreePtr;


void 	Build_Tree 	(TreePtr*);
void 	ReadDataFromFile 	(TreePtr*,FILE *);

int 	menu 	();

void 	PrintChoice 	(TreePtr );
void 	inorder 	(TreePtr );
void 	preorder 	(TreePtr );
void 	postorder 	(TreePtr );

void 	InsertChoice 	(TreePtr *);
void 	Insert_1_Key 	(TreePtr *);
void 	Insert_Manually_Keys	(TreePtr *);
void 	Insert_Random_Keys 	(TreePtr *);
void 	insert 	(TreePtr *,int);

void 	DeleteChoice 	(TreePtr *);
void 	Delete_1_Key 	(TreePtr *);
TreePtr Search_Victem 	(TreePtr,int);
TreePtr Search_Parent 	(TreePtr,int);
int 	delete 	(TreePtr *,int);
void 	Delete_All_Tree 	(TreePtr *);


void 	height 	(TreePtr);
void 	Height_Of_Tree 	(TreePtr,int *);

int 	IsEmpty 	(TreePtr);

void 	SaveInFile 	(TreePtr);
void 	PrintTreeInFile 	(TreePtr,FILE *);

int main()
{
 TreePtr tree = NULL;
 int choice;

 Build_Tree(&tree);

 do
 {
	choice = menu();

	switch (choice)
	{

 	case 1 : PrintChoice(tree);
		break;

 	case 2 : InsertChoice(&tree);
		break;

 	case 3 : DeleteChoice(&tree);
		break;


 	case 4 : SaveInFile(tree);
		break;

 	default : printf("\n\n\nInvalid choice, try again\n");
		printf("\n\n\n\n\n\n\nPress ENTER or any key to continue. \n");
		getch();
		break;
	}

 }while (choice != 4);

 return 0;
}


void Build_Tree (TreePtr *tree)
{
 FILE *fptr;
 int key;

 if ((fptr=fopen("tree.dat","r"))==NULL)
	printf("\nThe File could not open \n");
 else
	ReadDataFromFile(&*tree,fptr);
 fclose(fptr);
}




void ReadDataFromFile (TreePtr *tree,FILE *fptr)
{
 int key;

 fscanf(fptr,"%d",&key);
 if (!feof(fptr)) 	/* To ckeck before store the file is empty or no */
	insert(&*tree,key);

 while (!feof(fptr))
 {
	fscanf(fptr,"%d",&key);
	insert(&*tree,key);
 }
}


int menu ()
{
 int choice;

 system("cls");

 printf("AVL tree menu : \n");
 printf("========================== \n");
 printf("1 - Print tree in screen (( 3 ways to print )).\n");
 printf("2 - Insert keys (( 3 ways to insert )).\n");
 printf("3 - Delete keys (( 2 ways to delete )).\n");
 printf("4 - Quit.\n");

 printf("\n\nEnter your choice: \n");
 scanf("%d", &choice);

 return(choice);
}


void PrintChoice (TreePtr tree)
{
 int choice;

 system("cls");

 if (!IsEmpty(&*tree))
 {
	printf("Printing Choice : \n");
	printf("================== \n");
	printf("1 - Inorder traversing.\n");
	printf("2 - Preorder traversing.\n");
	printf("3 - Postorder traversing.\n");

	printf("\nEnter your choice : \n");
	scanf("%d", &choice);

	printf("\n");

	if (choice==1)
 	inorder(tree);
	else
	if (choice==2)
 	preorder(tree);
	else
	if (choice==3)
 	postorder(tree);
	else
 	printf("You enter invalid choice try again. \n");
 }
 else
 	printf("Try again the tree is empty. \n");

 printf("\n\n\n\n\nPress ENTER or any key to continue. \n");
 getch();
}


void inorder (TreePtr tree)
{
 if (tree!=NULL)
 {
	inorder(tree->left);
	printf(" %d ", tree->data);
	inorder(tree->right);
 }
}

void preorder (TreePtr tree)
{
 if (tree!=NULL)
 {
	printf(" %d ", tree->data);
	preorder(tree->left);
	preorder(tree->right);
 }
}


void postorder (TreePtr tree)
{
 if (tree!=NULL)
 {
	postorder(tree->left);
	postorder(tree->right);
	printf(" %d ", tree->data);
 }
}


void InsertChoice (TreePtr *tree)
{
 int choice;

 system("cls");

 printf("Inserting Choice : \n");
 printf("=================== \n");
 printf("1 - Insert one key.\n");
 printf("2 - Insert multiple keys manually.\n");
 printf("3 - Insert multople keys randomly.\n");
 printf("\nNOTE : duplicate number will not add to AVL tree.\n");

 printf("\n\n\nEnter your choice : \n");
 scanf("%d", &choice);

 if (choice==1)
 {
	printf("\n\nPlzzz enter key : \n");
	Insert_1_Key(&*tree);
 }
 else
 if (choice==2)
	Insert_Manually_Keys(&*tree);
 else
 if (choice==3)
	Insert_Random_Keys(&*tree);
 else
	printf("\n\nYou enter invalid choice try again. \n");

 printf("\n\n\n\n\n\n\nPress ENTER or any key to continue. \n");
 getch();
}



void Insert_1_Key (TreePtr *tree)
{
 int key;

 scanf("%d",&key);
 insert(&*tree,key);
}


void Insert_Manually_Keys (TreePtr *tree)
{
 int i,Number_Of_Keys;

 system("cls");
 printf("Plzzz enter # of keys to insert in tree manually : \n");
 scanf("%d",&Number_Of_Keys);

 for (i=0;i<Number_Of_Keys;i++)
 {
	printf("\nPlzzz enter element for key # %d : \n",i+1);
	Insert_1_Key(&*tree);
 }
}


void Insert_Random_Keys (TreePtr *tree)
{
 int i,Number_Of_Keys;

 system("cls");
 printf("Plzzz enter # of keys to insert in tree randomly : \n");
 scanf("%d",&Number_Of_Keys);

 srand(time(NULL));

 for (i=0;i<Number_Of_Keys;i++)
 insert(&*tree,rand()%size);
}


void insert (TreePtr *tree,int value)
{
 if ( *tree == NULL )
 {
 	*tree = malloc( sizeof( Node ) );

	if ( *tree != NULL )
	{
 ( *tree )->data = value;
 ( *tree )->left = NULL;
 ( *tree )->right = NULL;
 	}
 	else
 	{
	printf( "%d not inserted. No memory available.\n",value );
	printf("\n\n\n\n\n\n\nPress ENTER or any key to continue. \n");
	getch();
 	}
 }
 else
 	if ( value < ( *tree )->data )
 insert( & ((*tree)->left) , value );
 	else if ( value > ( *tree )->data )
 insert( &(( *tree) ->right) , value );
}


void DeleteChoice (TreePtr *tree)
{
 int choice;

 system("cls");

 if (!IsEmpty(*tree))
 {
	printf("Deleting Choice : \n");
	printf("================== \n");
	printf("1 - Delete one node.\n");
	printf("2 - Delete the whole tree.\n");

	printf("\nEnter your choice : \n");
	scanf("%d", &choice);

	if (choice==1)
 	Delete_1_Key(&*tree);
	else
	if (choice==2)
	{
 	Delete_All_Tree(&*tree);
 	printf("\n\nThe tree has been destroy. \n");
	}
	else
 	printf("You enter invalid choice try again. \n");
 }
 else
 	printf("Try again the tree is empty. \n");

 printf("\n\n\n\n\n\n\nPress ENTER or any key to continue. \n");
 getch();
}



void Delete_1_Key (TreePtr *tree)
{
 int key;

 printf("\n\nPlzzz enter key to delete it : \n");
 scanf("%d",&key);

 if (delete(&*tree,key))
	printf("\n\nThe key %d delete from tree. \n",key);
 else
	printf("\n\nThe key %d not fount in the tree. \n",key);
}


TreePtr Search_Victem (TreePtr tree,int key)
{
 if ( tree->data==key)
	return tree;
 else
 if (tree->left==NULL && tree->right==NULL)
	return NULL;
 else
 if (key<tree->data)
	return Search_Victem (tree->left,key);
 else
	return Search_Victem (tree->right,key);
}


TreePtr Search_Parent (TreePtr tree,int key)
{
 if (tree->left->data==key && tree->left!=NULL)
	return tree;
 if (tree->right->data==key && tree->right!=NULL)
	return tree;
 if (key<tree->data)
	return Search_Parent (tree->left,key);
 else
	return Search_Parent (tree->right,key);
}



int delete (TreePtr *tree,int data)
{
	TreePtr parent,victem;
	int MaxLeft;

	victem=Search_Victem(*tree,data);



	if ( victem->left==NULL && victem->right==NULL && victem==*tree )
	{
 	free(*tree);
 	*tree=NULL;
 	return 1; 	/* Victem delete */
	}

	else

	if ( victem->left==NULL && victem==*tree )
	{
 	*tree=victem->right;
 	free(victem);
 	return 1; 	/* Victem delete */
	}

	else

	if ( victem->right==NULL && victem==*tree )
	{
 	*tree=victem->left;
 	free(victem);
 	return 1; 	/* Victem delete */
	}

	else

	/* This condition to check if victem not found not need
 	to search the parent of victem and not do delete operations */

	if ( victem!=NULL )
	{
 	/* This condition if we delete the root is victem not need
 to search parent of victem */

 	if ( victem!=*tree )
	parent=Search_Parent(*tree,data);

 	/* This for if victem is leaf */

 	if ( victem->left == NULL && victem->right == NULL )
 	{
	if ( victem->data < parent->data )
 parent->left=NULL;
 	else
	if ( victem->data > parent->data )
 parent->right=NULL;
	free(victem);
 	}
 	else

 	/* Delete victem if it have child in right and indicate the pointer
 of parent to the right of child of victem */

 	if ( victem->left == NULL )
 	{
	if ( victem->data < parent->data )
 parent->left = victem->right;
	else
 parent->right = victem->right;
	free(victem);
 	}
 	else

 	/* Delete victem if it have child in left and indicate the pointer
 of parent to the left of child of victem */

 	if ( victem->right == NULL )
 	{
	if ( victem->data < parent->data )
 parent->left = victem->left;
	else
 parent->right = victem->left;
	free(victem);
 	}
 	else

 	/* Find largest # in left tree to be parent */

 	{
	MaxLeft = FindLargestNode (victem->left);
	victem->data = MaxLeft;

	/* Special case if MaxLeft == victem */

	if ( victem->data == victem->left->data && parent!=NULL )
	{
 parent=victem;
 victem=victem->left;

 if ( victem->left == NULL ) 	/* Right NULL not need to check */
 {
		parent->left=NULL;
		free(victem);
 }
 else 	/* Left not NULL */
 {
		parent->left = victem->left;
		free(victem);
 }
	}
	else
 delete(&victem->left,MaxLeft);
 	}

 	return 1; 	/* Victem delete */

	}

	else

 	return 0;	/* Victem not delete */
}



void Delete_All_Tree (TreePtr *tree)
{
 if (*tree!=NULL)
 {
	Delete_All_Tree(&(*tree)->left);
	Delete_All_Tree(&(*tree)->right);
	delete(&*tree,(*tree)->data);
 }
}



void largest (TreePtr tree)
{
 system("cls");

 if (!IsEmpty(tree))
	printf("The maximum # in tree = %d \n",FindLargestNode(tree));
 else
	printf("Try again the tree is empty. \n");

 printf("\n\n\n\n\n\n\nPress ENTER or any key to continue. \n");
 getch();
}



int FindLargestNode (TreePtr tree)
{
 if (tree->right==NULL)
	return(tree->data);
 return FindLargestNode (tree->right);
}


void smallest (TreePtr tree)
{
 system("cls");

 if (!IsEmpty(tree))
	printf("The minimum # in tree = %d \n",FindSmallestNode(tree));
 else
	printf("Try again the tree is empty. \n");

 printf("\n\n\n\n\n\n\nPress ENTER or any key to continue. \n");
 getch();
}


int FindSmallestNode (TreePtr tree)
{
 if (tree->left==NULL)
	return(tree->data);
 return FindSmallestNode (tree->left);
}



void leafs (TreePtr tree)
{
// system("cls");

 if (!IsEmpty(tree))
	printf("The total # of leafs in the tree = %d \n",Total_Leafs(tree));
 else
	printf("Try again the tree is empty. \n");

 printf("\n\n\n\n\n\n\nPress ENTER or any key to continue. \n");
 getch();
}



int Total_Leafs (TreePtr tree)
{
 if (tree==NULL) /* This condition nothing do just return 0 to end */
	return 0; 	/* compute with out this condition make infinite loop */
 else
 if ( tree->left==NULL && tree->right==NULL)
	return 1;
 else
 return (Total_Leafs(tree->left) + Total_Leafs(tree->right));
}


void height (TreePtr tree)
{
 int height;

system("cls");

 if (!IsEmpty(tree))
 {
	Height_Of_Tree(tree,&height);
	printf("The height of tree = %d \n",height-1);
 }
 else
	printf("Try again the tree is empty. \n");

 printf("\n\n\n\n\n\n\nPress ENTER or any key to continue. \n");
 getch();
}


void Height_Of_Tree (TreePtr tree, int *height)
{
 int left_height, right_height;

 if (tree==NULL)
	*height=0;
 else
 {
	Height_Of_Tree(tree->left, &left_height);
	Height_Of_Tree(tree->right, &right_height);
	if( left_height > right_height)
	*height=left_height+1;
	else
	*height=right_height+1;
 }
}


int IsEmpty (TreePtr tree)
{
 return tree == NULL ;
}


void SaveInFile (TreePtr tree)
{
 FILE *fptr;
// int key;

 if ((fptr=fopen("tree.dat","w"))==NULL)
	printf("\nThe File could not open \n");
 else
	PrintTreeInFile(tree,fptr);
 fclose(fptr);
}


void PrintTreeInFile (TreePtr tree,FILE *fptr)
{
 if (tree!=NULL)
 {
	fprintf(fptr,"%d ",tree->data);
	PrintTreeInFile(tree->left,fptr);
	PrintTreeInFile(tree->right,fptr);
 }
}
2
tvquran_6.gif

#5

السلام عليكم

بارك الله فيك. طلب اخير ياريت تدلني على سوفت وير للسي لانه ممكن المشكلة من السوفت وير اللي عندي. ولك مني خالص الشكر

#6

تستطيع عمل كمبايلر عن طرق سطر الأومر :

كمبايلر vc2008 أكتب :cl file.c

أو كمبايلر mingw أكتب : gcc file.c

أو من خلال الواجهه أختر مشروع سي وليس سي بلس بلس. في devc

tvquran_6.gif

#7

انا اعمل على الفيجول سي بلس بلس 2006. فهل يبمكن تنفيد هدا البرنامج عليها. معليشي طولت عليك باسئلتي. جزاك الله خير. وكيف احصل على واجهة devc

#8

من win32 أختر win32 console

ثم console application من Application type

و من Additionaloption أختر Empty project

و يكون ملفك إمتداده c وليس cpp

tvquran_6.gif

#9

السلام عليكم

اين اجدها هده الاعدادات هل هي في فيجول سي بلس بلس.ارجوا التوضيح اكتر لم افهم ماتعني

شكرا مرة تانيه اخي.

#10

عند إنشاء مشروع جديد تظهر هذه الخيارات.

tvquran_6.gif

#11

أخي استعمل ++turbo c - وهو يعمل في بيئة دوس (يعمل ايضا من خلال ويندوز لكن في بيئة شبه دوس)

انصحك باستخدامه اذا كنت تريد اتقان سي

الكود الذي كتبته انت اعلاه - يعمل في تربو سي++ \ لأن الدالة ()clrscr غير معرفة في ملف <conio.h> الخاص بفيجيوال سي++. لكنها معرفة في ملف <conio.h الخاص بتربو سي++

مواضيع مشابهة