-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathhanoi_tree.c
executable file
·95 lines (74 loc) · 1.9 KB
/
hanoi_tree.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
typedef struct node *nodeptr;
nodeptr hanoi_tree(char from,char to,char aux,int level);
void inorder(nodeptr p);
int number;
typedef struct stack{
int item[4];
int top;
}stack;
stack A,B,C;
typedef struct node
{
char from;
char to;
nodeptr left;
nodeptr right;
} node;
main()
{
A.top =B.top =C.top =-1;
nodeptr ptree;
int level =-1;
int i,j;
scanf("%d",&number);
j=number;
for(i=0;i<number;i++)
{
A.item[++(A.top)] =j;
j--;
}
ptree =hanoi_tree('A','C','B',level);
inorder(ptree);
getch();
return 0;
}
nodeptr hanoi_tree(char from,char to,char aux,int level)
{ level++;
if(level > number-1)
return NULL;
else
{
nodeptr p;
p =(nodeptr)malloc(sizeof(node));
p->from =from;
p->to =to;
p->left = hanoi_tree( from,aux,to,level);
p->right = hanoi_tree(aux,to,from, level);
return p;
}
}
void inorder(nodeptr p)
{
int x,y;
if(p!=NULL)
{
inorder(p->left);
if(p->from =='A')
x =A.item[(A.top)--];
if(p->from =='B')
x =B.item[(B.top)--];
if(p->from =='C')
x =C.item[(C.top)--];
if(p->to == 'A')
A.item[++(A.top)]=x;
if(p->to == 'B')
B.item[++(B.top)]=x;
if(p->to == 'C')
C.item[++(C.top)]=x;
printf("move %d from %c-to-%c \n",x,p->from,p->to);
inorder(p->right);
}
}