C语言中的双指针AVL树
我正在用指针练习我的AVL树。但如果我连一棵不平衡的树都做不成,我就无法继续平衡它。我认为我使用指针的方式有问题。这是我的密码:C语言中的双指针AVL树,c,data-structures,linked-list,binary-tree,avl-tree,C,Data Structures,Linked List,Binary Tree,Avl Tree,我正在用指针练习我的AVL树。但如果我连一棵不平衡的树都做不成,我就无法继续平衡它。我认为我使用指针的方式有问题。这是我的密码: typedef struct structNode { int data; struct structNode *left; struct structNode *right; int height; } sNode; void createTree(sNode **node); void insertNode(sNode **node, i
typedef struct structNode {
int data;
struct structNode *left;
struct structNode *right;
int height;
} sNode;
void createTree(sNode **node);
void insertNode(sNode **node, int num);
void main(){
sNode * root = NULL;
createTree(&root);
}
void createTree(sNode **root){
int i, num, nodes;
printf("\n\t\t\t\tNumber of nodes?\n");
nodes = numScan(1,6);
for(i=0;i<nodes;i++){
num = numScan(1,100);
insertNode(&*root,num);
}
printf("\n\n\t\t\t\tPress any key to continue.");
getch();
}
void insertNode(sNode **root, int num){
if(*root == NULL){
sNode *node = malloc(sizeof(node));
*root = node;
node->data = num;
node->left = NULL;
node->right = NULL;
node->height = 1;
return;
}
sNode *node = *root;
if(num < node->data){
node->left = insertNode(&node->left, num); //void value not ignored as it ought to be
}
else
node->right = insertNode(&node->right, num); //void value not ignored as it ought to be
}
}
typedef结构节点{
int数据;
struct structNode*左;
结构节点*右;
内部高度;
}斯诺德;
void createTree(sNode**节点);
void insertNode(sNode**node,int num);
void main(){
sNode*root=NULL;
createTree(&root);
}
void createTree(sNode**root){
int i,num,节点;
printf(“\n\t\t\t\t节点数?\n”);
节点=numScan(1,6);
对于(i=0;idata=num;
节点->左=空;
节点->右=空;
节点->高度=1;
返回;
}
sNode*节点=*根;
if(numdata){
node->left=insertNode(&node->left,num);//未按应有的方式忽略void值
}
其他的
node->right=insertNode(&node->right,num);//未按应有的方式忽略void值
}
}
我的代码来自这个网站,但它没有使用双指针,所以我在学习上有困难
地点:
编辑:numScan(a,b)基本上只是扫描一个字符串,然后判断它是否是一个数字数组,这样所有的输入都是数字(因此没有输入字母或字符)只是允许输入的数字范围。正如Daskwuff所说,这是一个简单的非平衡二叉树,而不是AVL树。但是,您的代码并没有那么糟糕:
void createTree(sNode **pRoot){
int i, num, nodes;
printf("\n\t\t\t\tNumber of nodes?\n");
nodes = numScan(1,6);
for(i=0;i<nodes;i++){
num = numScan(1,100);
insertNode(pRoot,num); //Just pass the pRoot as it is
}
printf("\n\n\t\t\t\tPress any key to continue.");
getch();
}
int insertNode(sNode **root, int num)
{ //returns the new height of *root
if(*root == NULL){
sNode *node = malloc(sizeof(node));
*root = node;
node->data = num;
node->left = NULL;
node->right = NULL;
node->height = 1;
return 1;
}
sNode *node = *root;
int SubtreeHeight;
if(num < node->data){
SubtreeHeight = insertNode(&node->left, num); //child enters itself at &node->left if needed
}
else
SubtreeHeight = insertNode(&node->right, num);
}
if (SubtreeHeight>=node->height) //if new subtree is higher..
node->height = SubtreeHeight + 1; //plus node itself
return node->height;
}
void createTree(sNode**pRoot){
int i,num,节点;
printf(“\n\t\t\t\t节点数?\n”);
节点=numScan(1,6);
对于(i=0;idata=num;
节点->左=空;
节点->右=空;
节点->高度=1;
返回1;
}
sNode*节点=*根;
int近树高;
if(numdata){
subtreehight=insertNode(&node->left,num);//如果需要,子节点将在&node->left处输入自身
}
其他的
SubtreeHeight=insertNode(&node->right,num);
}
if(SubtreeHeight>=节点->高度)//如果新子树更高。。
节点->高度=子树高度+1;//加上节点本身
返回节点->高度;
}
numScan()做什么?调用插入节点(&*root,num);
-->插入节点(&root,num)时,&*root
也是错误的
哦,是的,很抱歉我忘了添加它,它基本上只是扫描字符串并判断它是否是数字数组,所以所有输入都是数字。num scan中的数字只是允许输入的范围。您在这里实现的不是AVL。它只是一个不平衡的二叉树。“不运行”还不够具体。到底发生了什么?另外,你应该提供一个最小的、完整的例子来说明这种行为。一个好的起点是@JezT:刚刚注意到,我对高度的计算是错误的。正如duskwuff所说,这是一个简单的不平衡二叉树,而不是AVL oneOh,没问题,伙计,我所需要的是至少做一个tr你到底有没有精神错乱。你已经帮了我很多。剩下的我会想办法的