示例#1
0
文件: 50.C 项目: JackDrogon/Study
void pre_order_traversal(PNODE n)
{
    if (n != NULL) {
		printf ("%i ", n->value);
        pre_order_traversal (n->left);
        pre_order_traversal( n->right);
    }
}
static void pre_order_traversal(Aatree *node, void (*doit)(const char *))
{
   if (node->left != &aa_nil) {
      pre_order_traversal(node->left, doit);
   }

   doit(node->value);

   if (node->right != &aa_nil) {
      pre_order_traversal(node->right, doit);
   }
}
void pre_order_traversal(struct tree_node *root) {
	if (root == NULL) {
		return;
	}

	printf("*** Entered %d\n", root->key);
	printf("    L:\n");
	pre_order_traversal(root->left);
	printf("    R:\n");
	pre_order_traversal(root->right);
	printf("*** Leaving %d\n", root->key);
}
 /**
  * Ensures that all the beliefs are normalized.
  * The underlying junction tree must be calibrated.
  */
 void normalize() {
   assert(calibrated_ && !jt_.empty());
   // Compute the normalization constant z, and normalize the root
   // and every message in the direction from the root
   vertex_type root = *jt_.vertices().begin();
   result_type z = belief(root).marginal();
   jt_[root] /= z;
   pre_order_traversal(jt_, root, [&](const edge_type& e) {
       jt_[e](e) /= z;
     });
 }
int main(int argc, char *argv[])
{
   dstr line;
   Aatree * root = &aa_nil;

   d_init(argc, argv);

   d_printf("# Index\n");

   while ((d_getline(line))) {
      if (d_match(line, "^\\[([^\\]]*)")) {
         const char *ref = d_submatch(1);
         char *s = xstrdup(ref);
         root = aa_insert(root, s, s);
      }
   }

   pre_order_traversal(root, print_link);

   aa_destroy(root);

   return 0;
}
示例#6
0
文件: 50.C 项目: JackDrogon/Study
int main() {
	char buf[50];
	int  option;
	PNODE tree = NULL;
    PNODE node = NULL;
    	while (1) {
		printf ("--------------------------\n");
		printf ("Options are:\n\n");
		printf (" 0  Exit\n");
		printf (" 1  Insert node\n");
		printf (" 2  Delete node\n");
		printf (" 3  Find node\n");
		printf (" 4  Pre order traversal\n");
		printf (" 5  In order traversal\n");
		printf (" 6  Post order traversal\n");
		printf (" 7  Max depth\n");
		printf (" 8  Min depth\n");
		printf (" 9  Max value\n");
		printf (" 10 Min value\n");
		printf (" 11 Node Count\n\n");
		printf ("--------------------------\n");
		printf ("Select an option: ");
		fgets (buf, sizeof(buf), stdin);
		sscanf (buf, "%i", &option);
		printf ("--------------------------\n");
		if (option < 0 || option > 11) {
		    fprintf (stderr, "Invalid option");
		    continue;
		}
			switch (option) {
		    case 0:
		        exit (0);
		    case 1:
		        printf ("Enter number to insert: ");
				fgets (buf, sizeof(buf), stdin);
				sscanf (buf, "%i", &option);
				printf ("\n\n");
				insert_node (&tree, option);
				break;
		    case 2:
            	printf ("Enter number to delete: ");
				fgets (buf, sizeof(buf), stdin);
				sscanf (buf, "%i", &option);
				printf ("\n\n");
				delete_node (&tree, option);
				break;
		    case 3:
            	printf ("Enter number to find: ");
				fgets (buf, sizeof(buf), stdin);
				sscanf (buf, "%i", &option);
				printf ("\n\n");
				node = find_node (tree, option);
				if (node != NULL) {
				    printf ("Found node\n\n");
				} else {
				    printf ("Couldn't find node\n\n");
				}
				break;
		    case 4:
				printf ("Pre order traversal: ");
				pre_order_traversal (tree);
				printf ("\n\n");
				break;
		    case 5:
		        printf ("In order traversal: ");
		    	in_order_traversal (tree);
		    	printf ("\n\n");
		    	break;
		    case 6:
		        printf ("Post order traversal: ");
		    	post_order_traversal (tree);
       			printf ("\n\n");
		    	break;
		    case 7:
		        printf ("Max depth is %i\n\n", get_max_depth (tree));
		        break;
		    case 8:
		        printf ("Min depth is %i\n\n", get_min_depth (tree));
		        break;
		    case 9:
		        printf ("Max value is %i\n\n", get_max_value (tree));
		        break;
		    case 10:
		        printf ("Min value is %i\n\n", get_min_value (tree));
		        break;
      		case 11:
		        printf ("Node Count is %i\n\n", get_num_nodes (tree));
		        break;
		}
 	}
	return 0;
}
int main(void) {
	printf("The available commands are:\n"
	       "I K - Inserts a key with the value K\n"
	       "F K - Finds the entry with value K\n"
	       "S K - Searches for the successor of K\n"
	       "D - Dumps the tree (pre-order traversal)\n"
	       "Q - Quit\n");

	int key;
	struct tree_node *root = NULL;
	struct tree_node *new_root = NULL;

	int finish = 0;

	while (!finish) {
		printf("> ");
		fflush(stdout);

		if (fgets(input_buff, sizeof input_buff, stdin) == NULL) {
			break;
		}

		if (sscanf(input_buff, " I %d", &key) == 1) {
			new_root = tree_insert(root, key);
			if (new_root == NULL) {
				fprintf(stderr, "Out of memory; couldn't insert node.\n");
			} else {
				root = new_root;
			}
		} else if (sscanf(input_buff, " F %d", &key) == 1) {
			struct tree_node *n = tree_find(root, key);
			if (n == NULL) {
				printf("No such key: %d\n", key);
			} else {
				printf("Found key %d. Left = ", key);

				if (n->left != NULL) {
					printf("%d; ", n->left->key);
				} else {
					printf("N/A; ");
				}

				printf("Right = ");

				if (n->right != NULL) {
					printf("%d; ", n->right->key);
				} else {
					printf("N/A; ");
				}

				printf("Parent = ");

				if (n->parent != NULL) {
					printf("%d; ", n->parent->key);
				} else {
					printf("N/A; ");
				}

				printf("\n");
			}
		} else if (sscanf(input_buff, " S %d", &key) == 1) {
			struct tree_node *n1 = tree_find(root, key);
			if (n1 == NULL) {
				fprintf(stderr, "No such key: %d\n", key);
				continue;
			}
			struct tree_node *n2 = tree_successor(n1);
			if (n2 == NULL) {
				printf("Node %d has no successor.\n", n1->key);
			} else {
				printf("Successor of %d is %d\n", n1->key, n2->key);
			}
		} else {
			char op;
			if (sscanf(input_buff, " %c", &op) == 1) {
				switch (op) {
				case 'D':
					pre_order_traversal(root);
					break;
				case 'Q':
					finish = 1;
					break;
				default:
					fprintf(stderr, "Invalid input\n");
					break;
				}
			} else {
				fprintf(stderr, "Invalid input\n");
				break;
			}
		}
	}

	destroy_tree(root);

	return 0;
}