m.mirzakhani
جمعه 21 آذر 1393, 20:45 عصر
سلام دوستان
دو تا مشکل درمود درخت جستجوی دودویی دارم
ممنون میشم اگه راهنمایی کنید.
1- اول اینکه کد زیر یک لیست از اعداد رو از ورودی میگره و درخت مربوطه رو با پیمایش inorder رسم میکنه
Ù…ÛŒ خواستم اگه ممکنه کنار هر خط یه ØªÙˆØ¶ÛŒØ Ú©ÙˆØªØ§Ù‡ در مورد کاری Ú©Ù‡ انجام میده بنویسید.
مخصوصا در تابع maketree
#include "stdafx.h"
#include <stdlib.h>
#include <stdio.h>
#include <conio.h>
struct threadNode{
threadNode *left;
int info;
int rthread;
threadNode *right;
};
void maketree(threadNode **root, int num);
void inorder (threadNode *s);
int main()
{
threadNode *root = NULL;
int num;
//clrscr();
while(1)
{
printf_s("Enter a number:");
scanf_s("%d", &num);
if(!num)
break;
maketree(&root, num);
}
if(root)
inorder(root);
getchar();
getchar();
return 0;
}
//*****************
void maketree(threadNode **root, int num)
{
threadNode *q, *p, *r;
q = (threadNode *)malloc(sizeof(struct threadNode));
q -> left = q -> right = NULL;
q -> info = num;
q -> rthread = 1;
if(*root == NULL)
*root = q;
else
{
p = *root;
while (p != NULL)
{
if(q -> info > p -> info)
{
if (p -> rthread)
{
p -> rthread = 0;
//save the inorder successor of node p
r = p -> right;
p -> right = q;
q -> left = NULL;
//The inorder successor of node q is the
//previous successor of node p
q -> right = r;
q -> rthread = 1;
break;
}//end of if
else
p = p -> right;
}//end of if
else {
if (p -> left != NULL )
p = p -> left ;
else {
p -> left = q ;
q -> left = NULL;
//The inorder successor of node q is node p
q -> right = p;
q -> rthread = 1;
break ;
} //end of else
} //end of else
}//end of while
}//end else
}
//*******************
void inorder(threadNode *s)
{
threadNode *p, *q;
printf_s("Traverse of tree is :\n");
p = s;
do {
q = NULL;
while(p != NULL) //Traverse left branch
{
q = p;
p = p -> left;
}//end of while;
if(q != NULL)
{
printf_s("%d ", q -> info);
p = q -> right;
while(q -> rthread && p!= NULL)
{
printf("%d ", p -> info);
q = p;
p = p -> right;
}//end of while
}//end of if
} while(q != NULL);
}
2-می خوام همین درخت رو به جای اینکه به صورت خطی نمایش بدم، به صورت درختی نمایش بدم
مثلا مثل زیر:
126544
نکته 1: ØØªÙ…ا Ù…ÛŒ خوام به زبان c کار بشه
نکته 2:کامپایلرم هم vosual studio 2012
دو تا مشکل درمود درخت جستجوی دودویی دارم
ممنون میشم اگه راهنمایی کنید.
1- اول اینکه کد زیر یک لیست از اعداد رو از ورودی میگره و درخت مربوطه رو با پیمایش inorder رسم میکنه
Ù…ÛŒ خواستم اگه ممکنه کنار هر خط یه ØªÙˆØ¶ÛŒØ Ú©ÙˆØªØ§Ù‡ در مورد کاری Ú©Ù‡ انجام میده بنویسید.
مخصوصا در تابع maketree
#include "stdafx.h"
#include <stdlib.h>
#include <stdio.h>
#include <conio.h>
struct threadNode{
threadNode *left;
int info;
int rthread;
threadNode *right;
};
void maketree(threadNode **root, int num);
void inorder (threadNode *s);
int main()
{
threadNode *root = NULL;
int num;
//clrscr();
while(1)
{
printf_s("Enter a number:");
scanf_s("%d", &num);
if(!num)
break;
maketree(&root, num);
}
if(root)
inorder(root);
getchar();
getchar();
return 0;
}
//*****************
void maketree(threadNode **root, int num)
{
threadNode *q, *p, *r;
q = (threadNode *)malloc(sizeof(struct threadNode));
q -> left = q -> right = NULL;
q -> info = num;
q -> rthread = 1;
if(*root == NULL)
*root = q;
else
{
p = *root;
while (p != NULL)
{
if(q -> info > p -> info)
{
if (p -> rthread)
{
p -> rthread = 0;
//save the inorder successor of node p
r = p -> right;
p -> right = q;
q -> left = NULL;
//The inorder successor of node q is the
//previous successor of node p
q -> right = r;
q -> rthread = 1;
break;
}//end of if
else
p = p -> right;
}//end of if
else {
if (p -> left != NULL )
p = p -> left ;
else {
p -> left = q ;
q -> left = NULL;
//The inorder successor of node q is node p
q -> right = p;
q -> rthread = 1;
break ;
} //end of else
} //end of else
}//end of while
}//end else
}
//*******************
void inorder(threadNode *s)
{
threadNode *p, *q;
printf_s("Traverse of tree is :\n");
p = s;
do {
q = NULL;
while(p != NULL) //Traverse left branch
{
q = p;
p = p -> left;
}//end of while;
if(q != NULL)
{
printf_s("%d ", q -> info);
p = q -> right;
while(q -> rthread && p!= NULL)
{
printf("%d ", p -> info);
q = p;
p = p -> right;
}//end of while
}//end of if
} while(q != NULL);
}
2-می خوام همین درخت رو به جای اینکه به صورت خطی نمایش بدم، به صورت درختی نمایش بدم
مثلا مثل زیر:
126544
نکته 1: ØØªÙ…ا Ù…ÛŒ خوام به زبان c کار بشه
نکته 2:کامپایلرم هم vosual studio 2012