HashTable을 Java의 Generic을 이용하여 구현해봤다.


Chain (LinkedList) 를 사용하여 메모리를 필요한 만큼만 생성한다.


Hash 함수는 아주 기본적인 모듈라(%) 연산을 사용하여 작성한다.


모든 자료형을 담을 수 있다.


참고 : Cracking the Coding Interview




class Hash<K, V>
{
	private final int MAX_SIZE = 10;
	
	private LinkedList<Cell<K, V>>[] items;
	
	
	public Hash()
	{
		items = (LinkedList<Cell<K, V>>[])new LinkedList[MAX_SIZE];
	}
	
	//simplified hash function
	public int hashCodeOfKey(K key)
	{
		return key.toString().length() % MAX_SIZE;
	}
	
	public void put(K key, V value)
	{
		int x = hashCodeOfKey(key);
		
		if(items[x] == null)
		{
			items[x] = new LinkedList<Cell<K, V>>();
		}
		
		LinkedList<Cell<K, V>> collided = items[x];
		
		/* if item of same key is existed already
		remove existing key and value for new ones */
		
		for(Cell<K, V> c : collided)
		{
			if(c.equivalent(key))
			{
				collided.remove(c);
				break;
			}
		}
		
		Cell<K, V> cell = new Cell<K, V>(key, value);
		
		collided.add(cell);
	}
	
	public V get(K key)
	{
		int x = hashCodeOfKey(key);
		
		if(items[x] == null)
		{
			return null;
		}
		
		LinkedList<Cell<K, V>> collided = items[x];
		
		for(Cell<K, V> c : collided)
		{
			if(c.equivalent(key))
			{
				return c.getValue();
			}
		}
		
		return null;
	}
}

class Cell<K,V>
{
	private K key;
	private V value;
	
	public Cell(K k, V v)
	{
		key = k;
		value = v;
	}
	
	public boolean equivalent(Cell<K,V> c)
	{
		return equivalent(c.getKey());
	}
	
	public boolean equivalent(K k)
	{
		return key.equals(k);
	}
	
	public K getKey()
	{
		return key;
	}
	
	public V getValue()
	{
		return value;
	}
}

'컴퓨터공학 > Data structure' 카테고리의 다른 글

Heap  (0) 2013.01.08
Traversing Tree  (0) 2013.01.07
Balance? - Tree  (0) 2013.01.07

Heap Definition 


• Completebinarytree

Partial order : for every node v, the key stored in v is greater(smaller) than or equal to its parent’s key. 


• A max tree is a tree in which the key value in each node is no smaller than the key values in its children (if any).

• A max heap is a complete binary tree that is also a max tree.



• A min tree is a tree in which the key value in each node is no larger than the key values in its children (if any).

• A min heap is a complete binary tree that isalso a min tree 



Insert into a Min Heap 


• Put into the next available leaf node

• (this is simple since it is array)

– Look at the parent of this element and swap if the parent is larger (note that the partial ordering is preserved)

– Repeat with the parent (sifting up


• Array representation of heap

Since the heap is a complete binary tree, we can easily locate

the parent of any element, i.e., parent(i) is at |_i/2_| 


• Analysis of insert_max_heap

The algorithm follows a path from the new leaf of the max heap to the root until it either reaches the root or reaches a position i such that the value in the parent of i is no smaller than that of i.


Since the height of the heap is log (n + 1) , O(log n) 




Delete from a Min Heap 

• Remove the root from the tree and return its value

• Remove the right most leaf and place it at the root

• Perform sifting down operations(if the key is larger than the smaller of its two children, then swap these two. Repeat this)

• Insertion / deletion : O(n) 

Application of Heap 

• Priority Queue 

• Heap Sort 


Heap Sorting : Storing n keys in O(n), and then repetitively applying DeleteMin() operations in O(n logn). The most basic algorithm.

• Graph algorithms
– Shortest path finding algorithm
– Minimum spanning tree finding algorithm

• Scheduling algorithms 


Heap Sort 

• Ascending
– Using the Min Heap

• Descending
– Uisng the Max Heap 



Implement Heap As Array 


• Easy to use array to implement heap array[i]’s parent is array[i/2]

array[i]’s left child is array[2*i]
array[i]’s right child is array[2*i+1]

• Easy to swap child and parent 


'컴퓨터공학 > Data structure' 카테고리의 다른 글

HashTable by Java  (0) 2016.01.26
Traversing Tree  (0) 2013.01.07
Balance? - Tree  (0) 2013.01.07
  • Preorder

  • Postorder

  • Level order

• NOTE :The inorder traversal is the special case for binary trees.

(another definition possible) 


- inorder는 binary-tree에서만 존재한다.


 preorder traversal

– To move down the tree to the left until a null node is reached

– The null node’s parent is then “visited”, and the traversal continues with the node that is one to the right

– If there is no move to the right, the traversal continues with the last unvisited node at the next higher level of the tree 




• Vertex -> Left subtree -> Right subtree 

– Root : 1st position in traversal
– Bottom right child: latest position 










 postorder traversal

– To move down the tree to the left until a null node is reached

– The traversal continues with the null node’s parent and then to the right

– If there is no move to the right (null node), the null node’s parent is then “visited”, and the traversal continues with the node at the next higher level of tree 





• Left subtree -> Right subtree -> Vertex 

– Root: latest position in traversal
– Bottom left child: 1
st position 

- EX) % rm -r /user/bit 







inorder traversal of a binary tree

– In an inorder traversal a node is visited after its left subtree and before its right subtree 







• Left subtree -> Vertex -> Right subtree
– Bottom left child : 1st position
– Bottom right child :latest position in traversal 








'컴퓨터공학 > Data structure' 카테고리의 다른 글

HashTable by Java  (0) 2016.01.26
Heap  (0) 2013.01.08
Balance? - Tree  (0) 2013.01.07

1. Root to leaf 가 동일하다


=> leaf는 동일 level 에 위치


2. root는 최소 2개 이상의 Sub-Tree를 가진다.


3. 각 노드는 구조체의 자원을 절반 이상 사용한다.

'컴퓨터공학 > Data structure' 카테고리의 다른 글

HashTable by Java  (0) 2016.01.26
Heap  (0) 2013.01.08
Traversing Tree  (0) 2013.01.07

+ Recent posts