【问题标题】:Internal Path Length内部路径长度
【发布时间】:2014-10-19 03:57:36
【问题描述】:

我有一个问题需要帮助:

编写一个程序来计算扩展二叉树的内部路径长度。使用它来凭经验调查在随机生成的二叉搜索树中搜索的平均键比较次数。

编辑:

所以我想出了一个用于二叉树的 C++ 类

#include <iostream>

/*Binary tree class based on the struct. Includes basic functions insert, delete, search */

struct node
{
     int data;
     node *left;
     node *right;
};

class binarytree{
public:
     binarytree();
     ~binarytree();

     void insert(int key);
     node *search(int key);
     void destroy_tree();

private:
     void destroy_tree(node *leaf);
     void insert(int key, node *leaf);
     node *search(int key, node *leaf);

     node *root;

};

binarytree::binarytree(){
     root = NULL;
 }

binarytree::~binarytree(){
     destroy_tree();
}

void binarytree::destroy_tree(node *leaf)
{
     if(leaf!=NULL)
     {
          destroy_tree(leaf->left);
          destroy_tree(leaf->right);
          delete leaf;
     }
}

void binarytree::insert(int key, node *leaf){
     if(key < leaf->data){
          if(leaf->left!=NULL)
               insert(key, leaf->left);
          else{
               leaf->left = new node;
               leaf->left->data=key;
               leaf->left->left=NULL;
               leaf->left->right=NULL;
          }
     }
     else if(key>=leaf->data){
          if(leaf->right!= NULL)
               insert(key, leaf->right);
          else{
               leaf->right = new node;
               leaf->right->data=key;
               leaf->right->left=NULL;
               leaf->right->right=NULL;
          }
     }
}

node *binarytree::search(int key, node *leaf){
     if(leaf!=NULL){
          if(key==leaf->data)
               return leaf;
          if(key<leaf->data)
               return search(key, leaf->left);
          else
               return search(key, leaf->right);
     }
     else return NULL;
}

我之前的问题是我在实施方面需要帮助。现在,如果我的二叉树实现是正确的(如果不是,请随时告诉我),有人能帮我找到计算扩展二叉树内部路径长度的算法吗?我认为我的实现应该涵盖扩展二叉树,但我不确定如何找到内部路径长度。我在整个互联网上都看过,但似乎没有人能够解释它或有一种算法来找到它。

再次提前感谢您的帮助!我真的很感激!

【问题讨论】:

    标签: java c++ algorithm binary-tree graph-algorithm


    【解决方案1】:

    语言:C/C++:

    创建如下结构:

    int count = 0;             //treat count,count1, count2 as global variable
    int count1 = 0;            // so define these outside main ()
    int count2 = 0;
    int count3 = 0;
    struct node{
    int data;                  //data or value at that particular node
    struct node* left;         //left pointer
    struct node* right;
    }Node;                     //Node is a type of node
    
    Node node1 = (Node)((malloc) sizeof(Node))
      //to create a space in memory (if available) and 99.99% times it's available
    

    现在,您可以使用任何您想要的功能。 就像如果你想找到长度:

    int findLength(Node* head){
    
    node* temp = head;              //initialize temp to head to use it further
    
       if(temp->left != NULL || temp->right!=NULL){
       count1 += findLength(temp->left);
       count2 += findLength(temp->right);
       //next line: if(count1>count2, make count3=count1 , else count3=count2)
    
       count3 = (count1>count2)? count1 : count2;  
       count += count3;                      //add count3 to previous value of count
       return count+1;
       }
    
       if(temp->left == NULL && temp->right == NULL){
       return 1;
       }
    
       else if(head == NULL)
          return 0;
       return count++;
    }
    

    【讨论】:

    • 好的,我已经创建了一个包含插入、删除和搜索的基本二叉树类。您对利用它来回答上述问题有什么建议吗?
    • 所以基本上你将根节点传递给函数,然后函数在每次运行到内部节点时缩小左侧和右侧并增加计数 - 因此长度是内部节点的数量它找到的节点?
    • 这个函数找到树的长度并且它是递归的。它的作用是:先向左侧走,然后再向右侧走,把你脚下的树的长度带给我,然后再来。所以,root 会说“给我左子树的长度,然后是右子树”,然后左子树会说“给我左子树的长度,然后是右子树的长度,无论哪个更大,让它成为我的长度”。我知道这很令人困惑,但事实就是这样。
    • 你明白我的解释了吗?你能做出这个最佳答案吗:P
    猜你喜欢
    • 2016-07-14
    • 2020-08-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-06-24
    • 2021-06-18
    相关资源
    最近更新 更多