平衡树错误求助(有注释)
  • 板块学术版
  • 楼主zhc9426
  • 当前回复8
  • 已保存回复8
  • 发布时间2022/9/20 21:38
  • 上次更新2023/10/27 10:29:13
查看原帖
平衡树错误求助(有注释)
592849
zhc9426楼主2022/9/20 21:38

rt,自己写了个权值平衡树

只有插入的时候没有问题,写了删除就报错了,问题如下

undefined reference to `Insert(int, int, int)'

错误行

cout<<"n组数据"<<endl; cin >> n;
cout<<"权值,数据"<<endl; 
    for(i = 0; i < n; i++){
        cin >> j >> k; // j是节点权值  k是对应数据
        Insert(j, k, 1); // 权值建树
    }

全部代码

#include<iostream>
#include<cstdio>
#include<cmath>
#include<vector>
//权值搜索树模板 
/*
搜索树(平衡树)定义:
左儿子小于当前节点,右儿子大于当前节点 
*/ 
using namespace std;
//宏定义 
int i, j, k;
int maxn=1, n;//n是节点数  
char Char;
struct Tree{ 
    int Fa;//指向父节点 
    int Left;//指向左儿子 
    int Right;//指向右儿子 
    int Data[20];//数据
    int qwq;//权值
    int Count;//权值出现相同次数,也是数据组数
};
Tree tree_qwq[1000];//权值排序

//函数部分
int Insert(int y, int z, int m);//权值数 y->qwq z->Data m是当前节点 
int Delete(int y, int z, int m);
int Serach(int y, int z, int m);

//主程序部分 
int main()
{
	cout<<"n组数据"<<endl; cin >> n; cout<<"权值,数据"<<endl; 
    for(i = 0; i < n; i++){
        cin >> j >> k; // j是节点权值  k是对应数据
        Insert(j, k, 1); // 权值建树
    }
    cout<<"是(t)否(f)搜索?(未完成)"<<endl;
	cout<<"是(t)否(f)删除?"<<endl; cin>>Char;
	if(Char == 't'){
		cout<<"请输入删除组数"<<endl;
		cin>>n;
		cout<<"权值,数据";
		for(i = 0; i < n; i++){
			cin >> j >> k;
			Delete(j, k, 1);
		}
	}
    return 0;
}
//函数编写
insert_a (int y, int z, int now){ // z是数据  y是权值  m是当前节点 

    if(tree_qwq[now].qwq == 0){ // 新建节点 
        tree_qwq[now].qwq = y;
        tree_qwq[now].Data[1] = z;
        tree_qwq[now].Count = 1;
        ++maxn;
        cout<<"EndNew"<<endl;
        return 0;
    }
    
    if(tree_qwq[now].qwq > y){ // 权值比当前节点小
	 
        if(tree_qwq[now].Left == 0){ // 无左节点 ,新增指针
            tree_qwq[maxn].Fa = now;
            tree_qwq[now].Left = maxn;
            cout<<"FindLeft  ";
            Insert (y, z, maxn);
            return 0;
        }
        
        else{ // 有左节点直接递归 
            cout<<"ContinueLeft  ";
            Insert (y, z, tree_qwq[now].Left);
            return 0;
        }
    }
    
    if(tree_qwq[now].qwq < y){ // 权值比当前节点大 
    
        if(tree_qwq[now].Right == 0){ // 没有右节点 , 新建节点 
            tree_qwq[maxn].Fa = now;
            tree_qwq[now].Right = maxn;
            cout<<"FindRight  ";
            Insert ( y, z, maxn);
            return 0;
        }
        
        else{ // 有右节点 , 继续遍历 
            cout<<"ContinueRight  ";
            Insert (y, z, tree_qwq[now].Right);
            return 0;
        }
    }
    
    if(tree_qwq[now].qwq==y) { // 权值与当前节点相等 
		++tree_qwq[now].Count;
		tree_qwq[now].Data[tree_qwq[now].Count] = z;
		cout<<"EndSame"<<endl;
		return 0;
    }
    
    return 0;
}

int Delete( int y, int z, int m) {//y->qwq z->Data m->当前节点 

    if (tree_qwq[ tree_qwq[m].Left ].qwq == 0 && tree_qwq[ tree_qwq[m].Right ].qwq == 0){ // 
        cout<<"Delete  ";
        --maxn;
        
        if (tree_qwq[m].Count > 1){ // 当前节点有多个数据 
    		cout<<"DeleteOneOf  ";
    		for( i=0; i<tree_qwq[m].Count; i++){
				if( z == tree_qwq[m].Data[i]){
					tree_qwq[m].Data[i] = tree_qwq[m].Data[ tree_qwq[m].Count ];	
				} 
			} 
		} 
		else
		{ 
			cout<<"DeleteThis  "; 
	        tree_qwq[m].Fa = 0;
    	    tree_qwq[m].qwq = 0;
    	    tree_qwq[m].Data[0] = 0;
    	} 
        --tree_qwq[m].Count;
        return 0;
    }
    
    if(tree_qwq[m].qwq > y && tree_qwq[m].Left != 0){ // 权值比当前节点小
    	cout<<"ContinueLeft ";
        Delete (y, z, tree_qwq[m].Left);
        return 0;
	}
	
    if(tree_qwq[m].qwq < y && tree_qwq[m].Right != 0){
        cout<<"ContinueRight  ";
        Delete (y, z, tree_qwq[m].Right);
        return 0;	
	}
	
	cout<<"NoPointHere!"<<endl;
	
	return 0;
}


2022/9/20 21:38
加载中...