Thread: hanoi tree qustion..

  1. #1
    Banned
    Join Date
    Oct 2008
    Posts
    1,535

    hanoi tree qustion..

    in the hatoi tree function
    why do need "temp" variable
    Code:
    #include<stdio.h>
    #include<stdlib.h>
    
    typedef struct node Node;
    struct node{
    	int width;
    	int source;
    	int target;
    	int temp;
    	Node* left;
    	Node* right;
    };
    
    Node * hanoi_tree(int width, int source, int target, int temp){
    	Node *root;
    
    	?? 1 ?? ;
    	root = (Node*)malloc(sizeof(Node));
    	root -> width = width;
    	root -> source = source;
    	root -> target = target;
    	root -> temp = temp;
    	?? 2 ?? = hanoi_tree( ?? 3 ?? );
    	?? 4 ?? = hanoi_tree( ?? 5 ?? );
    	?? 6 ??;
    }
    
    void print(Node* root){
    	?? 7 ??;
    	print(root -> left);
    	printf("Move disk from tower %d to tower %d\n", ?? 8 ??);
    	print(root ->right);
    }
    
    void main(){
    	Node *tree;
    	tree = hanoi_tree( 4, 1, 2, 3);
    	print(tree);
    }

  2. #2
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,732
    I guess, its used to find out the the no. of piles which is to be holded by each tree???

    -ssharish
    Life is like riding a bicycle. To keep your balance you must keep moving - Einstein

  3. #3
    Registered User
    Join Date
    Apr 2004
    Posts
    29

    Arrow aux.

    Quote Originally Posted by transgalactic2 View Post
    in the hatoi tree function
    why do need "temp" variable
    Code:
    #include<stdio.h>
    #include<stdlib.h>
    
    typedef struct node Node;
    struct node{
    	int width;
    	int source;
    	int target;
    	int temp;
    	Node* left;
    	Node* right;
    };
    
    Node * hanoi_tree(int width, int source, int target, int temp){
    	Node *root;
    
    	?? 1 ?? ;
    	root = (Node*)malloc(sizeof(Node));
    	root -> width = width;
    	root -> source = source;
    	root -> target = target;
    	root -> temp = temp;
    	?? 2 ?? = hanoi_tree( ?? 3 ?? );
    	?? 4 ?? = hanoi_tree( ?? 5 ?? );
    	?? 6 ??;
    }
    
    void print(Node* root){
    	?? 7 ??;
    	print(root -> left);
    	printf("Move disk from tower %d to tower %d\n", ?? 8 ??);
    	print(root ->right);
    }
    
    void main(){
    	Node *tree;
    	tree = hanoi_tree( 4, 1, 2, 3);
    	print(tree);
    }
    I believe the answer to that is that it's better to call temp var "aux", because the Tower of hanoi problem involves 3 poles, named Source, Auxiliary, and destination, and the reason this coder decided to use the work "temp" is because the aux pole is a temporary holding ground for the rings that are going to be too big to put on top of the smaller rings below on the dest pole.

    - Edward

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Interpreter.c
    By moussa in forum C Programming
    Replies: 4
    Last Post: 05-28-2008, 05:59 PM
  2. Binary Tree, couple questions
    By scoobasean in forum C Programming
    Replies: 3
    Last Post: 03-12-2005, 09:09 PM
  3. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM
  4. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 10:33 AM
  5. BST/Red and Black Tree
    By ghettoman in forum C++ Programming
    Replies: 0
    Last Post: 10-24-2001, 10:45 PM