查找

1.学习总结

1.1思维导图

 

1.2查找学习体会

理解查找的基本概念,我觉得重点要掌握线性表各种查找算法,包括顺序查找,折半查找和分块查找的基本思路,算法实现和查找效率等,今后更要灵活运用各种查找算法解决一些综合应用问题。

2.PTA实验作业

2.1 题目一:是否二叉搜索树

(1)设计思路:

伪代码:

先遍历二叉树,max保存左子树中的最大值,min保存右子树中的最小值,为空树返回false

(2)代码截图

3)提交列表:

无法解决部分正确的问题

2.2 题目二:二叉搜索树中的最近祖先

(1)伪代码

 若树为空或者u或v不在该树中  ERROR;  若u或者v就是根节点的值  ;  return T->Key;   若u或者v在同一棵子树上     return T->Key;  若u大于T->key  最近共同祖先在左子树上; 
    若u大于T->key  最近共同祖先在右子树上; 然后查看u是否在树中 , 若树为空:则返回0即不在;查找该树,若找到等值的key,则u在该树中;否则不在;

 

(2)代码截图

 

 (3)提交情况

 

2.3题目三

QQ账户的申请与登录

(1)设计思路:无(此题询问同学和参考百度的)

(2)代码截图:

虽然目前不会,但是我会争取以后弄懂的

3.最终排名

 

我的总分:108

排名:80

 4.阅读代码:

题目:利用哈希表实现数据查找

现在有一个用来存放整数的Hash表,Hash表的存储单位称为桶,每个桶能放3个整数,当一个桶中要放的元素超过3个时,则要将新的元素存放在溢出桶中,每个溢出桶也能放3个元素,多个溢出桶使用链表串起来。此Hash表的基桶数目为素数P,Hash表的hash函数对P取模。  

  1. #include<iostream>  
  2. using namespace std;  
  3.   
  4.   
  5. #define P 7  
  6. #define NULL_DATA -1  
  7. #define BUCKET_NODE_SIZE 3  
  8.   
  9.   
  10. struct bucket_node    
  11. {  
  12.     int data[BUCKET_NODE_SIZE];  
  13.     struct bucket_node *next;  
  14. };  
  15. bucket_node hash_table[P];  
  16.   
  17.   
  18. void InitHashTable(bucket_node(*ht)[P])  
  19. {  
  20.     for(int i=0; i<P; ++i)  
  21.     {  
  22.         for(int j=0; j<BUCKET_NODE_SIZE; ++j)  
  23.         {  
  24.             (*ht)[i].data[j] = NULL_DATA;  
  25.         }  
  26.         (*ht)[i].next = NULL;  
  27.     }  
  28. }  
  29.   
  30.   
  31. int Hash(int X)  
  32. {  
  33.     return X % P;  
  34. }  
  35. void insert_new_element(bucket_node(*ht)[P], int x)  
  36. {  
  37.       
  38.     int index = Hash(x);      
  39.     for (int i = 0; i < BUCKET_NODE_SIZE; i++)  
  40.     {  
  41.         if ((*ht)[index].data[i] == NULL_DATA)  
  42.         {  
  43.             (*ht)[index].data[i] = x;  
  44.             return;  
  45.         }  
  46.     }  
  47.     bucket_node *s = new bucket_node;  
  48.     s = (*ht)[index].next;  
  49.     bucket_node *p = &(*ht)[index];  
  50.     while(s != NULL)  
  51.     {  
  52.         for(int i = 0; i < BUCKET_NODE_SIZE; i++)  
  53.         {  
  54.             if (s->data[i] == NULL_DATA)  
  55.             {  
  56.                 s->data[i] = x;  
  57.                 //(*ht)[index] = *s;  
  58.                 return;  
  59.             }  
  60.         }  
  61.         p = s;  
  62.         s = s->next;  
  63.      }  
  64.     s=new bucket_node;  
  65.     p->next= s;  
  66.     for(int j = 0; j<BUCKET_NODE_SIZE; ++j)  
  67.     {  
  68.         s->data[j] = NULL_DATA;  
  69.     }  
  70.     s->next = NULL;  
  71.     for (int i = 0; i < BUCKET_NODE_SIZE;i++)  
  72.     if (s->data[i] == NULL_DATA)  
  73.     {  
  74.         s->data[i] = x;  
  75.         return;  
  76.     }  
  77. }  
  78.   
  79.   
  80. void main()  
  81. {  
  82.     InitHashTable(&hash_table);  
  83.     int ar[] = {8,15,22,29,36,43,50,7,14,21,28,35,42,49,56,63,70,25,30};  
  84.     for(int i=0; i<sizeof(ar)/sizeof(int); ++i)  
  85.     {  
  86.         insert_new_element(&hash_table,ar[i]);  
  87.     }  
  88. }  

 

posted @ 2018-05-27 12:17  里昂科科  阅读(160)  评论(0编辑  收藏  举报