Java 内部路径长度
我有一个问题需要帮助: 编写一个程序来计算扩展二叉树的内部路径长度。使用它来调查随机生成的二叉搜索树中搜索的平均键比较数 编辑:Java 内部路径长度,java,c++,algorithm,binary-tree,graph-algorithm,Java,C++,Algorithm,Binary Tree,Graph Algorithm,我有一个问题需要帮助: 编写一个程序来计算扩展二叉树的内部路径长度。使用它来调查随机生成的二叉搜索树中搜索的平均键比较数 编辑: 所以我已经提出了一个C++类的二叉树< /p> #include <iostream> /*Binary tree class based on the struct. Includes basic functions insert, delete, search */ struct node { int data; node *l
所以我已经提出了一个C++类的二叉树< /p>
#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;
}
#包括
/*基于结构的二叉树类。包括插入、删除、搜索等基本功能*/
结构节点
{
int数据;
节点*左;
节点*右;
};
类二叉树{
公众:
二叉树();
~binarytree();
无效插入(int键);
节点*搜索(int键);
void destroy_tree();
私人:
void destroy_树(节点*叶);
无效插入(int键,节点*叶);
节点*搜索(int键,节点*叶);
节点*根;
};
binarytree::binarytree(){
root=NULL;
}
binarytree::~binarytree(){
摧毁_树();
}
void binarytree::销毁_树(节点*叶)
{
如果(叶!=NULL)
{
摧毁_树(叶->左);
摧毁_树(叶->右);
删除叶子;
}
}
void binarytree::insert(int键,节点*leaf){
如果(键data){
如果(叶->左!=NULL)
插入(键,叶->左);
否则{
叶->左=新节点;
叶->左->数据=键;
叶->左->左=空;
叶->左->右=空;
}
}
否则如果(键>=叶->数据){
如果(叶->右!=NULL)
插入(键,叶->右);
否则{
叶->右=新节点;
叶->右->数据=键;
叶->右->左=空;
叶->右->右=空;
}
}
}
node*binarytree::搜索(int键,node*leaf){
如果(叶!=NULL){
如果(键==叶->数据)
回叶;
如果(关键数据)
返回搜索(键,叶->左);
其他的
返回搜索(键,叶->右);
}
否则返回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++;
}
好的,我已经创建了一个包含插入、删除和搜索的基本二叉树类。您有什么建议可以利用它来回答上述问题吗?因此,本质上,您将根节点传递到函数中,然后函数在每次运行到内部节点时,都会按左右两侧的比例增加计数-因此,长度是它找到的内部节点数?此函数会找到树是递归的。它所做的是:先从左边走,然后再从右边走,给我你下面那棵树的长度,然后再来。所以,root会说“给我左子树的长度,然后是右子树”,然后左子树会说“给我左子树的长度,然后是右子树的长度,以较大者为准,使其成为我的长度”。我知道这让人困惑,但事实就是这样。你明白我的解释了吗?你能做出最好的回答吗