본문 바로가기
🧮알고리즘

[20211005] 해시 검색

by 캔 2021. 10. 6.
package Practice;
import java.util.LinkedList;

public class HashSearch {
	class Node{
		String key;
		String value;
		public Node(String key, String value) {
			this.key = key;
			this.value = value;
		}
		String value() {
			return value;
		}
		void value(String value) {
			this.value = value;
		}
	}
	
	// Node형 연결리스트로 선언
	LinkedList<Node>[] data;
	
	// 자신을 호출하면서 크기를 지정하여 선언
	public HashSearch(int size) {
		this.data = new LinkedList[size];
	}
	
	// Key값을 통해 HashCode 생성 - 아스키코드이용
	int gethashCode(String key) {
		int hashcode=0;
		for(char c : key.toCharArray())
			hashcode+=c;
		return hashcode;
	}
	
	// HashCode를 이용해서 Index를 지정
	int convertToIndex(int hashcode) {
		return hashcode%data.length;
	}
	
	Node searchKey(LinkedList<Node> list, String key) {
		if(list == null ) return null;
		for(Node node : list) {
			if(node.key.equals(key)) {
				return node;
			}
		}
		return null;
	}
	
	// Key를 통한 값 저장
	void set(String key,String value) {
		int index = convertToIndex(gethashCode(key));
		LinkedList<Node> list = data[index];
		// 없으면 저장
		if(list == null) {
			list = new LinkedList<Node>();
			data[index] = list;
		}
		
		Node node = searchKey(list, key);

		if(node==null)
			list.addLast(new Node(key,value));
		else
			node.value(value);
		
		System.out.println("hashcode : "+gethashCode(key)+", index : " +index + ", ");
	}
	
	// key를 통한 값 호출
	String get(String key) {
		int index = convertToIndex(gethashCode(key));
		LinkedList<Node> list = data[index];
		if(list == null)
			return "Not Found";
		else {
			for(Node n : list) {
				if(n.key.equals(key))
					return n.value;
			}
		return null;
		}
	}	
}

'🧮알고리즘' 카테고리의 다른 글

[20211007] 버블 정렬  (0) 2021.10.07
[20211006] 선택 정렬  (0) 2021.10.06
[20211001] 이진 트리 검색  (0) 2021.10.01
[20210930] 점프(블록) 검색 알고리즘  (0) 2021.09.30
[20210929] 보간 검색 알고리즘  (0) 2021.09.29