수다닷컴

  • 해외여행
    • 괌
    • 태국
    • 유럽
    • 일본
    • 필리핀
    • 미국
    • 중국
    • 기타여행
    • 싱가폴
  • 건강
    • 다이어트
    • 당뇨
    • 헬스
    • 건강음식
    • 건강기타
  • 컴퓨터
    • 프로그램 개발일반
    • C언어
    • 비주얼베이직
  • 결혼생활
    • 출산/육아
    • 결혼준비
    • 엄마이야기방
  • 일상생활
    • 면접
    • 취업
    • 진로선택
  • 교육
    • 교육일반
    • 아이교육
    • 토익
    • 해외연수
    • 영어
  • 취미생활
    • 음악
    • 자전거
    • 수영
    • 바이크
    • 축구
  • 기타
    • 강아지
    • 제주도여행
    • 국내여행
    • 기타일상
    • 애플
    • 휴대폰관련
  • 프로그램 개발일반
  • C언어
  • 비주얼베이직

해쉬 2차탐색법 (quadratic probing ) 질문 있습니다

참이삭

2023.04.01



제가 보고있는 책에서
해쉬 2차 탐색법 (quadratic probing ) 에 관련된 문제풀다가 질문이 생겼어요
총 세가지 질문이 있습니다...

현재 해쉬 공부 중인데요

첫번째 질문으로는,,
제가 보고 있는 자료구조 책에서
해쉬 2차탐색법 (quadratic probing)은
isempty() 메소드에서는 단순히 현재 차지되어 있는 slot 싸이즈를 0으로 설정하는,, 즉

public boolean isempty()
{
return currentsize== 0;
}

이라고 쓸수 없다고 하는데요.. 그럼 어떻게 써야하나요?..
이해가 안가는걸요..

길어서 읽기 힘드시겠지만..
현재 다음과 같은 소스를 기반으로 문제를 풀고 있어서요..
이게있어야 제 질문이 이해가 가실거예요

// quadraticprobing hash table class
//
// construction: an approximate initial size or default of 101
//
// ******************public operations*********************
// bool insert( x ) -- insert x
// bool remove( x ) -- remove x
// bool contains( x ) -- return true if x is present
// void makeempty( ) -- remove all items
//boolean isempty( ) -- return true if hash table is empty
// anytype find( anytype x ) -- return reference to x, if present; null otherwise
// object[] toarray() -- return array of active items in hash table

빨간 부분이 제가 질문 드리려는 메소드들이예요
이 소스안에는 없고요.. 제가 쓴게 맞는지 확인하고싶어요

public class quadraticprobinghashtableanytype
{
private static final int default_table_size = 101;
private hashentryanytype [ ] array; // the array of elements
private int currentsize; // the number of occupied cells

public quadraticprobinghashtable( )
{
this( default_table_size );
}
public quadraticprobinghashtable( int size )
{
allocatearray( size );
makeempty( );
}

private void rehash( )
{
hashentryanytype [ ] oldarray = array;
allocatearray( nextprime( 2 * oldarray.length ) );
currentsize = 0;

for( int i = 0; i oldarray.length; i++ )
if( oldarray[ i ] != null && oldarray[ i ].isactive )
insert( oldarray[ i ].element );
}

public void remove( anytype x )
{
int currentpos = findpos( x );
if( isactive( currentpos ) ) {
array[ currentpos ].isactive = false;
}
}

public boolean contains( anytype x )
{
int currentpos = findpos( x );
return isactive( currentpos );
}

private boolean isactive( int currentpos )
{
return array[ currentpos ] != null && array[ currentpos ].isactive;
}

public void makeempty( )
{
currentsize = 0;
for( int i = 0; i array.length; i++ )
array[ i ] = null;
}

private int myhash( anytype x )
{
int hashval = x.hashcode( );
hashval %= array.length;
if( hashval 0 )
hashval += array.length;
return hashval;
}

private static class hashentryanytype
{
public anytype element; // the element
public boolean isactive; // false if marked deleted
public hashentry( anytype e )
{
this( e, true );
}
public hashentry( anytype e, boolean i )
{
element = e;
isactive = i;
}
}

private void allocatearray( int arraysize )
{
array = new hashentry[ arraysize ];
}

private static int nextprime( int n )
{
if( n % 2 == 0 )
n++;
for( ; !isprime( n ); n += 2 )
;
return n;
&nbsbsp; }

private static boolean isprime( int n )
{
if( n == 2 || n == 3 )
return true;
if( n == 1 || n % 2 == 0 )
return false;
for( int i = 3; i * i = n; i += 2 )
if( n % i == 0 )
return false;
return true;
}

private int findpos( anytype x )
{
int offset = 1;
int currentpos = myhash( x );

while( array[ currentpos ] != null &&
!array[ currentpos ].element.equals( x ) )
{
currentpos += offset; // compute ith probe
offset += 2;
if( currentpos = array.length )
currentpos -= array.length;
}

return currentpos;
}

public void insert( anytype x )
{
// insert x as active
int currentpos = findpos( x );
if( isactive( currentpos ) )
return;
array[ currentpos ] = new hashentryanytype( x, true );
// rehash; see section 5.5
if( ++currentsize array.length / 2 )
rehash( );
}그리고 또 다음 두개 문제들도 위의 소스 기반인 문제인데요
책에 답이 제공이 안되서요.. 제가 맞게 한건지..확인해 주셨으면 해요

첫번째 문제는 해쉬테이블 내용을 배열로 바꾸는거예요 2차탐색법 기준으로요..
implement method public object[] toarray(), which returns an array of the elements stored in the hash table.
if there are 3 active elements stored in the table, the length of the returned array should be 3.제가 쓴건..
public object[] toarray() {
object any [] = new object[this.currentsize];

int i = 0;
for(int j = 0; j array.length ; j++){
if( array[i] != null && array[i].element != null ){
any[i] = array[i].element;
i++;
}
}
return any;
}

이거 맞나요?... 그리고 두번째 문제는 해쉬테이블에서 아이템 찾으면 반환하는.. 문제예요
implement method public anytype find(anytype x), which returns a reference to element x if x is stored in the hash table, and returns null otherwise.

제가 풀기로는...
public anytype find(anytype x)
{
int currentpos = findpos(x);
if( array[ currentpos ] == null || !array[ currentpos ].isactive )
return null;
else
return array[currentpos].element;
}

맞는지.. 확인하고 싶어요..

답변 부탁드려요 감사합니다..

신청하기





COMMENT

댓글을 입력해주세요. 비속어와 욕설은 삼가해주세요.

  • 난길

    소스는 자세히 안봤지만..
    특이하게.. Java느낌보다는 C++느낌이 나네요.
    STL에서도 제공되는 iterator와 같이 자바에서도 iteration 알고리즘을 제공합니다.
    Iterator 또는 Enumeration ..-- 이걸 이용해서 해보세요..

    적고보니, 질문에 대한 답변은 아니네요.

번호 제 목 글쓴이 날짜
2695934 tr 속성값 (9) 새 2025-05-25
2695905 ASP로 개발됐을 때 css가 달라져요 ㅠㅠ (4) 슬아라 2025-05-24
2695878 form을 이용한 다른 페이지로 넘기는 방법을 알려주세요 (1) 핫파랑 2025-05-24
2695844 저기 암호화 및 복호화 프로그램.. 만들어볼려는대 (2) 한빛 2025-05-24
2695814 [질문] PDA에서 애플릿이 가능한가요? (1) 봄시내 2025-05-24
2695785 웹 설정 도와줄분 화이트캣 2025-05-23
2695730 갑자기 기억이 안나는데 accesskey 속성.. 빛나라 2025-05-23
2695702 [질문] Java 버전 차이에 의한 오류?!! (2) 검사 2025-05-23
2695672 자바 임베디드 쪽으로 배우고 싶은데요..질문이요.. (1) 뽀그리 2025-05-22
2695647 헉! 이클립스(v3.1)에서 발생되는 널포인트 익셉션? ;;; (3) 아빠몬 2025-05-22
2695586 IFRAME 캐싱 질문 봄나비 2025-05-22
2695498 [질문]실행가능한 jar파일.. 정말 이해가 안가네요... ㅡㅜ;; 터1프한렩 2025-05-21
2695468 자바랑 이클립스에서요.. 스킬 2025-05-21
2695375 Mysql 연동하는 자바 질문있습니다. 아리솔 2025-05-20
2695319 파워포인트 파일을 저장할 수 있을까요? 시윤 2025-05-19
2695289 [질문]Tween 값의 정도를 알고 싶습니다. 타마 2025-05-19
2695238 c 와 c++의 시작 (10) ChocoHoilc 2025-05-18
2695215 탑메뉴의 repeat-x .배경이 두가지에요ㅠ ㅠ 널위해 2025-05-18
2695187 자바스크립트와 자바의 import에 관해서 질문드려요 (1) 무슬 2025-05-18
2695116 테마 문의 (해당 사이트와 같은 테마 혹은 플러그인) Sweet 2025-05-17
<<  이전  1 2 3 4 5 6 7 8 9 10  다음  >>

수다닷컴 | 여러분과 함께하는 수다토크 커뮤니티 수다닷컴에 오신것을 환영합니다.
사업자등록번호 : 117-07-92748 상호 : 진달래여행사 대표자 : 명현재 서울시 강서구 방화동 890번지 푸르지오 107동 306호
copyright 2011 게시글 삭제 및 기타 문의 : clairacademy@naver.com