int Tree<T>::get_max_depth(Tree<T>::Node *root) { if (!root) { return 0; } return 1 + std::max(get_max_depth(root->left), get_max_depth(root->right)); }
//最长路径 int get_max_depth (PNODE n) { int left = 0; int right = 0; if (n == NULL) { return 0; } if (n->left != NULL) { left = 1 + get_max_depth (n->left); } if (n->right != NULL) { right = 1 + get_max_depth (n->right); } return (left > right ? left : right ); }
static int get_max_depth(struct label *node) { int depth = 0; struct label* child = node->first_child; while (child) { int child_depth = get_max_depth(child); if (child_depth > depth) { depth = child_depth; } child = child->next_sibling; } return depth + 1; }
/* Lay out a tree using Walker/Buchheim's layout algorithm. The tree is rooted at node->xcoord, node->ycoord */ void walker_layout(struct label *node, struct walker_layout_rules* rules) { if (!node) { return; } struct layout_ctx ctx; ctx.prev_node_at_level = calloc(get_max_depth(node), sizeof(node)); ctx.rules = rules; ctx.x_top_adjustment = node->xcoord; ctx.y_top_adjustment = node->ycoord; first_walk(&ctx, node); second_walk(&ctx, node, 0, -node->xcoord); }
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; }
bool Tree<T>::is_tree_balanced2(Tree<T>::Node *root) { return abs(get_max_depth(root) - get_min_depth(root)) <= 1; }