内部路径长度
Internal Path Length
我有一个问题需要帮助:
编写一个程序来计算扩展二叉树的内部路径长度。使用它来实证研究在随机生成的二进制搜索树中搜索的关键字比较的平均数量。
编辑:
所以我为二叉树设计了一个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;
}
我之前的问题是,我在实施方面需要帮助。现在,如果我对二叉树的实现是正确的(如果不是,请随时告诉我),有人能帮我找到计算扩展二叉树内部路径长度的算法吗?我认为我的实现应该涵盖扩展的二进制树,但我不确定如何找到内部路径长度。我在互联网上找了很多,但似乎没有人能解释它,也没有人能找到它的算法
再次提前感谢您的帮助!我真的很感激!
语言: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++;
}
相关文章:
- 将函数类成员映射到类本身内部
- 如何将更多文件夹添加到c++include路径
- 带有特殊路径部分的"std::filesystem::weakly_canonical"失败
- C++A*算法并不总是在路径中具有目标节点
- Boost Spirit,获取迭代器内部语义动作
- 我不明白为什么我声明一个空的内部结构并将其传递给构造函数
- 内联函数中具有内部链接的全局变量
- 在函数内部的声明中初始化数组,并在外部使用它
- 如何在不知道向量大小的情况下输入向量内部的向量?
- 卷曲bracers内部结构的声明
- 从函数角度看ID到文件路径的内部与外部映射
- spdlog标头仅与外部fmt一起使用.spdlog错误:'内部':不是'fmt'
- 如何在pugixml中获取节点的内部XML
- 使用C链接在函数内部创建C++模板
- boost xml parsingl将xml的路径作为变量发送
- 指针没有更新它在void函数内部指向的值
- 方法内部但循环仍得到预期的不合格id错误C++
- 对于MacOS上的G++,如何添加默认的include目录/usr/local/include和默认的库搜索路径/usr
- 红黑树的内部路径长度
- 内部路径长度