Finding the maximum distance of a node in a binary tree

Source: Internet
Author: User
Tags printf

Source: The beauty of programming 3.8 finding the maximum distance of a binary tree node

If you look at the two-fork tree as a graph, and the connection between the parent and child nodes is considered bidirectional, let's define the number of "distance" between two nodes.

Write a program to find the distance between the two nodes farthest apart in a binary tree.

As the following illustration shows, the edges of the thick arrows represent the longest distance:


The two nodes farthest apart in the tree are a, B

Analysis: For a binary tree, to two nodes u,v the furthest apart, there are two cases:

1, the path from the U node to the V node passes through the root node

2, the path from the U node to the V node does not pass through the root node, in which case the U,V node must be on the left subtree or right subtree of the root node, which translates to the distance between the farthest two nodes in the two-fork tree with the root node as the child node

As mentioned above, the root node refers to the path contains the root node, for example: Join the above image only left dial hand tree Fgha, then the maximum distance of two nodes is F, A, the path contains the root node F, also known as the root node.

So we can recursively solve, according to the binary tree in the middle sequence traversal way to traverse the binary tree, in the process of traversal to find the furthest distance of two nodes.

The program is described as follows:

typedef struct Node {struct node *pleft;    Left child struct Node *pright;           Right child char Chvalue;       The value of the node int leftmaxvalue;      Zuozi maximum distance int rightmaxvalue;

Right subtree longest distance}lnode, *bintree;

    void Findmaxlen (bintree root, int *maxlen) {//Traversal to leaf node, return if (root = NULL) return;

    If the left dial hand tree is empty, the maximum distance to the left of the node is 0 if (root->pleft = = NULL) Root->leftmaxvalue = 0;

    If the right subtree is empty, the maximum distance to the right of the node is 0 if (root->pright = = NULL) Root->rightmaxvalue = 0;

    If the left dial hand tree is not empty, recursively looks for the left subtree longest distance if (root->pleft! = NULL) Findmaxlen (Root->pleft, MaxLen);

    If the right subtree is not empty, recursively look for the right subtree longest distance if (root->pright! = NULL) Findmaxlen (Root->pright, MaxLen); Calculates the longest distance from the root node in the left subtree if (root->pleft! = NULL) {if (Root->pleft->leftmaxvalue > Root->pleft->rig
        Htmaxvalue) Root->leftmaxvalue = Root->pleft->leftmaxvalue + 1; else Root->leftmaxvalue = Root->pleft-> rightmaxvalue + 1; }//calculates the longest distance from the root node in the right subtree if (root->pright! = NULL) {if (Root->pright->leftmaxvalue > Root->prig
        Ht->rightmaxvalue) Root->rightmaxvalue = Root->pright->leftmaxvalue + 1;
    else Root->rightmaxvalue = root->pright->rightmaxvalue + 1; }//update longest distance if (Root->leftmaxvalue + root->rightmaxvalue > *maxlen) *maxlen = Root->leftmaxvalu
E + root->rightmaxvalue; }

The maximum distance between the current two nodes is always stored in int *maxlen and is updated during traversal.

The program described below is intended to test whether the above code is correct, including establishing a two-fork tree, destroying a binary tree, and printing a binary tree:

#include <stdlib.h> #include <stdio.h> typedef struct Node {struct node *pleft;    Left child struct Node *pright;           Right child char Chvalue;       The value of the node int leftmaxvalue;      Zuozi maximum distance int rightmaxvalue;

Right subtree longest distance}lnode, *bintree;

    void Findmaxlen (bintree root, int *maxlen) {//Traversal to leaf node, return if (root = NULL) return;

    If the left dial hand tree is empty, the maximum distance to the left of the node is 0 if (root->pleft = = NULL) Root->leftmaxvalue = 0;

    If the right subtree is empty, the maximum distance to the right of the node is 0 if (root->pright = = NULL) Root->rightmaxvalue = 0;

    If the left dial hand tree is not empty, recursively looks for the left subtree longest distance if (root->pleft! = NULL) Findmaxlen (Root->pleft, MaxLen);

    If the right subtree is not empty, recursively look for the right subtree longest distance if (root->pright! = NULL) Findmaxlen (Root->pright, MaxLen); Calculates the longest distance from the root node in the left subtree if (root->pleft! = NULL) {if (Root->pleft->leftmaxvalue > Root->pleft->rig
        Htmaxvalue) Root->leftmaxvalue = Root->pleft->leftmaxvalue + 1;
  Else          Root->leftmaxvalue = Root->pleft->rightmaxvalue + 1; }//calculates the longest distance from the root node in the right subtree if (root->pright! = NULL) {if (Root->pright->leftmaxvalue > Root->prig
        Ht->rightmaxvalue) Root->rightmaxvalue = Root->pright->leftmaxvalue + 1;
    else Root->rightmaxvalue = root->pright->rightmaxvalue + 1; }//update longest distance if (Root->leftmaxvalue + root->rightmaxvalue > *maxlen) *maxlen = Root->leftmaxvalu
E + root->rightmaxvalue;
    }//Create two fork tree void Buildbintree (Bintree *root) {char ch;    scanf ("%c", &ch);
    Enter an element fpurge (stdin);
    if (ch = = ")//if the input is a space character, indicating that the binary tree is empty, the *root is null *root = NULL;
        else {//If the input is not a space character, the value is assigned to the root node's chvalue, and the Saozi right subtree *root = (bintree) malloc (sizeof (Lnode)) is established recursively;
        (*root)->chvalue = ch;
        (*root)->leftmaxvalue = 0;

        (*root)->rightmaxvalue = 0; Buildbintree (& (*root)->pleft);
    Buildbintree (& (*root)->pright); }}//Destroy binary tree, free memory void Destroybintree (Bintree *root) {if (*root! = NULL) {destroybintree (& (*root)->ple
        FT);

        Destroybintree (& (*root)->pright);
        Free (*root);
    *root = NULL;
        }}//Pre-order traversal binary tree void Preordertraverse (Bintree root) {if (root = NULL) {preordertraverse (root->pleft);
        printf ("%c", Root->chvalue);
    Preordertraverse (Root->pright);
    }} int main () {Bintree root;
    Buildbintree (&root);
    Preordertraverse (root);
    printf ("\ n");
    int maxlen = 0;
    Findmaxlen (Root, &maxlen);
    printf ("MaxLen =%d\n", maxlen);
Destroybintree (&root); }

My micro-blog: Http://weibo.com/caryaliu

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.