二叉树与哈希表

14 篇文章 0 订阅
11 篇文章 0 订阅

树和二叉树

[1]二叉树

  1. 满二叉树

    • 结点个数2^k-1(k为深度)
  2. 完全二叉树

    • 2^k-1 -1 <结点个数<2^k -1
  3. 先序、中序、后序遍历代码实现

    /*先序遍历*/
    void prev_printf(Node *root){
    	if(root){
    		printf("%d ",root->data);
    		prev_printf(root->left);
    		prev_printf(root->right);
    	}
    }
    /*中序遍历*/
    void mid_printf(Node *root){
    	if(root){
    		mid_printf(root->left);
    		printf("%d ",root->data);
    		mid_printf(root->right);
    	}
    }
    /*后序遍历*/
    void end_printf(Node *root){
    	if(root){
    		end_printf(root->left);
    		end_printf(root->right);
    		printf("%d ",root->data);
    	}
    }
    

哈希表

[1]线性探测法解决冲突

  1. 此方法定义哈希表是一个数组,根据关键字除留取余所得到的的下标,将关键字放到表中对应下标的位置;
  2. 若该位置已有数据,则发生冲突,通过线性探测法,遍历其余下标(实现方法代码可见),挨个检测当前位置中是否有元素,没有则直接插入。
/*除留余数法*/
int hash_fun(int key){
	return key%LEN;
}
/*数据插入*/
int hash_insert(int hash_table[], int key){
	int  index;
	index = hash_fun(key);
	
	if(hash_table[index] == 0){
		hash_table[index] = key;
		return 0;
	}
	/*解决冲突---->线性探测法解决冲突*/
	for(int i=1; i<LEN; i++){
        /*线性遍历其余下标*/
		int index1 = hash_fun(index+i);
		if(hash_table[index1] == 0){
			hash_table[index1] = key;
			return 0;
		}
	}
	return -1;
}
/*查找*/
void hash_search(int hash_table[]){
	int key;
	printf("输入查找的关键字:");
	scanf("%d",&key);
	while(getchar()!='\n');
	int index = hash_fun(key);
	if(hash_table[index] == key){
		printf("hash_table[%d] = %d\n",index,hash_table[index]);
	}
	else{
		int index1;
		for(int i = 1;i < LEN;i++){
			index1 = hash_fun(index+i);		
			if(hash_table[index1] == key){
				break;
			}
		}
		printf("hash_table[%d] = %d\n",index1,hash_table[index1]);
	}	
}
/*打印哈希表中数据*/
void hash_show(int hash_table[]){
	for(int i=0; i<LEN; i++){
		printf("hash_table[%d] = %d\n", i, hash_table[i]);
	}
}

[2]链地址法解决冲突

  1. 此方法的定义的哈希表是一个结构体指针数组,保存每一条链表头指针的地址;
  2. 还是使用除留余数法,通过得到下标确定放入哪条链表;
  3. 解决冲突的方法是将关键字直接插到哈希表对应下标所保存的链表中。
/*数据插入*/
void hash_insert(struct node* hash_table[], int key){
	int index;
	index = hash_fun(key);

	struct node *pnew = NULL;
	pnew = (struct node *)malloc(sizeof(struct node));
	assert(pnew!=NULL);
	pnew->data = key;
	pnew->next = NULL;

	if(hash_table[index] == NULL){
		hash_table[index] = pnew;
	}
	else{
		struct node *p = hash_table[index];
		while(p->next != NULL){
			p = p->next;
		}
		p->next = pnew;
	}
	return ;
}
/*查找数据*/
void hash_search(struct node *hash_table[]){
	int key;
	printf("输入查找的关键字:");
	scanf("%d",&key);
	while(getchar()!='\n');
	int index = hash_fun(key);
	struct node *p = hash_table[index];
	while(p != NULL){
		if(p->data == key){
			printf("在第%d条链表:%d\n",index,p->data);
			break;
		}
		p = p->next;
	}
}
/*打印哈希表中数据*/
void hash_show(struct node* hash_table[]){
	for(int i=0; i<LEN; i++){
		if(hash_table[i] == NULL){
			printf("第%d条链表暂无数据!\n", i);
		}
		else{
			struct node *p  = hash_table[i];
			printf("第%d条链表的数据如下:", i);
			while(p != NULL){
				printf("%d ", p->data);
				p = p->next;
			}
			printf("\n");
		}
	}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值