home *** CD-ROM | disk | FTP | other *** search
- Newsgroups: comp.programming
- Path: sparky!uunet!gumby!wupost!uwm.edu!csd4.csd.uwm.edu!markh
- From: markh@csd4.csd.uwm.edu (Mark)
- Subject: Re: Teaching the basics
- Message-ID: <1992Aug25.174243.10436@uwm.edu>
- Sender: news@uwm.edu (USENET News System)
- Organization: Computing Services Division, University of Wisconsin - Milwaukee
- References: <1992Aug19.154830.11787@uwm.edu> <MJN.92Aug23031941@pseudo.uucp> <7116@charon.cwi.nl>
- Date: Tue, 25 Aug 1992 17:42:43 GMT
- Lines: 81
-
- In article <7116@charon.cwi.nl> tromp@cwi.nl (John Tromp) writes:
- >>On top of it all, the recursive version does not have an arbitrary
- >>limit on the number of disks, and can be easily translated to any
- >>language supporting recursion.
- >
- >Well, nobody in his right mind would ever run hanoi on 32 discs or more:-)
- >
- >One of the prime applications of recursion, IMHO,
- >and where you cannot hope to replace it by anything else,
- >is in a tree search algorithm like alpha beta or plain old minimax.
-
- Wrong. In fact, the non-recursive version (tree search) is far smaller and
- simpler. No stacks either.
-
- #include <malloc.h>
- #include <stdio.h>
-
- typedef struct Tree *Tree;
- struct Tree {
- char *S; int Key, State;
- Tree Left, Right;
- };
- Tree Root;
-
- Tree Find(int Key) {
- Tree Prev, Cur;
- for (Prev = 0, Cur = Root; Cur != Prev; ) {
- if (Cur->Key == Key) return Cur;
- Prev = Cur, Cur = (Cur->Key < Key)? Cur->Left: Cur->Right;
- }
- Cur = (Tree)malloc(sizeof *Cur); /* Add in your own error checks */
- Cur->Key = Key, Cur->State = 0;
- Cur->Left = Cur->Right = Cur;
- if (Prev == 0) Root = Cur;
- else if (Prev->Key < Key) Prev->Left = Cur; else Prev->Right = Cur;
- return Cur;
- }
-
- #define PRE_ORDER 0
- #define IN_ORDER 1
- #define POST_ORDER 2
-
- #define BACKWARDS 0
- #define FORWARDS 1
- void Traverse(int Dir, int Order) {
- Tree Prev, Cur;
- for (Prev = 0, Cur = Root; Cur != 0; ) {
- Tree T;
- if (Cur->State == Order) printf(" %s", Cur->S);
- Cur->State = (Cur->State + 1)%3;
- if (Dir == BACKWARDS) {
- T = Cur->Left, Cur->Left = Cur->Right,
- Cur->Right = Prev, Prev = Cur, Cur = T;
- } else {
- T = Cur->Right, Cur->Right = Cur->Left,
- Cur->Left = Prev, Prev = Cur, Cur = T;
- }
- }
- }
-
- main() {
- Tree T0, T1, T2, T3, T4, T5, T6, T7;
- Root = 0;
- T3 = Find(3), T0 = Find(0), T4 = Find(4), T6 = Find(6),
- T5 = Find(5), T1 = Find(1), T7 = Find(7), T2 = Find(2);
- T0->S = "This", T1->S = "is", T2->S = "a", T3->S = "non",
- T4->S = "recursive", T5->S = "tree", T6->S = "traversal",
- T7->S = "routine";
- printf("Backwards, pre order:"); Traverse(BACKWARDS, PRE_ORDER);
- putchar('\n');
- printf("Backwards, post order:"); Traverse(BACKWARDS, POST_ORDER);
- putchar('\n');
- printf("Backwards, in order:"); Traverse(BACKWARDS, IN_ORDER);
- putchar('\n');
- printf("Forwards, pre order:"); Traverse(FORWARDS, PRE_ORDER);
- putchar('\n');
- printf("Forwards, post order:"); Traverse(FORWARDS, POST_ORDER);
- putchar('\n');
- printf("Forwards, in order:"); Traverse(FORWARDS, IN_ORDER);
- putchar('\n');
- }
-