/* * recurse get the depth of some one tree * */ int depth_of_tree(struct TreeNode *root) { if (NULL == root) return 0; if (NULL != root && NULL == root->left && NULL == root->right) return 1; return 1 + max(depth_of_tree(root->left), depth_of_tree(root->right)); }
int depth_of_tree(struct tree_node* root) { if(root == NULL) return -1; if((root->lson == NULL) && (root->rson == NULL)) return 0; struct tree_node *abt_ls_ptr=root->lson; struct tree_node *abt_rs_ptr=root->rson; if(abt_ls_ptr == NULL) return(1+depth_of_tree(abt_rs_ptr)); if(abt_rs_ptr == NULL) return(1+depth_of_tree(abt_ls_ptr)); return(1+max(depth_of_tree(abt_ls_ptr),depth_of_tree(abt_rs_ptr))); }
int main() { struct tree_node* root=create_root(),*temp; int i,parent,val,depth; while(1) { printf("\n-------MENU--------\n"); printf("1. Insert Left Child\n"); printf("2. Insert Right Child\n"); printf("3. Find\n"); printf("4. Delete the lson\n"); printf("5. Delete the rson\n"); printf("6. Display\n"); printf("7. Depth of tree\n"); printf("8. Number of leaf node\n"); printf("9. Exit\n"); printf("Enter your choice..."); scanf("%d",&i); switch(i) { case 1: printf("Enter the data of the parent node: "); scanf("%d",&parent); printf("Enter the data to be entered: "); scanf("%d",&val); create_lson(parent,root,val); display(root); break; case 2: printf("Enter the data of the parent node: "); scanf("%d",&parent); printf("Enter the data to be entered: "); scanf("%d",&val); create_rson(parent,root,val); display(root); break; case 3: printf("Enter the value to be searched:"); scanf("%d",&val); struct tree_node *tmp=find_node(root,val); if(tmp == NULL) printf("Node does not exist\n"); else printf("Node is present"); break; case 4: printf("Enter the lson to be deleted:"); scanf("%d",&val); temp=delete_lson(val,root); break; case 5: printf("Enter the rson to be deleted:"); scanf("%d",&val); temp=delete_rson(val,root); break; case 6: display(root); break; case 7:depth=depth_of_tree(root); if(depth == -1) printf("Tree not exist\n"); else printf("Depth of the tree is :%d ",depth); break; case 9: exit(0); break; case 8: depth=no_leaf_node(root); printf("Number of leaf node in the tree are:%d",depth); } } return 0; }
bool tree_depth_delta(struct TreeNode *root, struct TreeNode *root2) { if (1 >= abs(depth_of_tree(root) - depth_of_tree(root2))) return true; return false; }