๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
์•Œ๊ณ ๋ฆฌ์ฆ˜/์ž๋ฃŒ๊ตฌ์กฐ ์ด๋ก 

ํŠธ๋ผ์ด (Trie) - ๋น„์„ ํ˜• ์ž๋ฃŒ๊ตฌ์กฐ

by sh119 2024. 7. 17.

๐Ÿ‘ฉ‍๐Ÿ’ป  ํŠธ๋ผ์ด (Trie)

๋ฌธ์ž์—ด์„ ์ €์žฅํ•˜๊ณ  ๋น ๋ฅด๊ฒŒ ํƒ์ƒ‰ํ•˜๊ธฐ ์œ„ํ•œ ์ •๋ ฌ๋œ ํŠธ๋ฆฌ ํ˜•ํƒœ์˜ ์ž๋ฃŒ๊ตฌ์กฐ

๋ฌธ์ž์—ด ์ €์žฅ์„ ์œ„ํ•œ ๋ฉ”๋ชจ๋ฆฌ๊ฐ€ ํ•„์š”ํ•˜์ง€๋งŒ, ํƒ์ƒ‰์ด ๋น ๋ฆ„ ***

 

โœ๏ธ ์‹œ๊ฐ„ ๋ณต์žก๋„ ๋ฐ ์ƒ์„ฑ ๋ณต์žก๋„

๊ธธ์ด๊ฐ€ N์ธ ๋ฌธ์ž์—ด ํƒ์ƒ‰์˜ ์‹œ๊ฐ„ ๋ณต์žก๋„ :O(N)

์ƒ์„ฑ ๋ณต์žก๋„ : O(MN) (M: ๋ฌธ์ž์—ด ๊ฐœ์ˆ˜)

 

โœ๏ธ ํŠธ๋ผ์ด ํ˜•ํƒœ

๋ฌธ์ž์—ด ๊ตฌ์„ฑ ex) apple, april, ace, bear, best

 

โœ๏ธ ํŠธ๋ผ์ด์˜ ์‚ฝ์ž…

์•ž์„œ ์žˆ๋Š” ๊ฒƒ๊ณผ ๋น„๊ตํ•˜์—ฌ ์žˆ๋Š” ๋ถ€๋ถ„์€ ๊ทธ๋Œ€๋กœ ๋”ฐ๋ผ๊ฐ€์„œ ์—†๋Š” ๋ถ€๋ถ„๋ถ€ํ„ฐ ๋…ธ๋“œ๋ฅผ ์ƒ์„ฑ, ๋ฌธ์ž์˜ ๋ ๋…ธ๋“œ๋Š” ๋”ฐ๋กœ ํ‘œ๊ธฐ๋ฅผ ํ•ด๋‘”๋‹ค.

 

โœ๏ธ ํŠธ๋ผ์ด์˜ ์‚ญ์ œ

๋ฌธ์ž๋ฅผ ๋”ฐ๋ผ๊ฐ€์„œ ๋์ชฝ์— ๋‹ค๋‹ค๋ฅด๋ฉด, (<ex> apple์—์„œ์˜ e, app์—์„œ์˜ p ) 

  • ํ•ด๋‹น ์น ํ•ด์ ธ์žˆ๋Š” ๋…ธ๋“œ์˜ (๋ฌธ์ž์—ด ๋ ๋…ธ๋“œ์˜) ์ž์‹๋…ธ๋“œ๊ฐ€ ์—†์œผ๋ฉด ์ƒ‰์น ํ•œ ๊ฒƒ์„ ์—†์•ค ํ›„ ์œ„๋กœ ์ด๋™,
    ๊ทธ ๋‹ค์Œ ๋…ธ๋“œ๊ฐ€ ์ž์‹๋…ธ๋“œ๊ฐ€ ์—†๊ณ , ํฐ์ƒ‰ ๋…ธ๋“œ (๋ ๋…ธ๋“œ๊ฐ€ ์•„๋‹ˆ๋ผ๋ฉด) ์ง€์šด ํ›„ ์œ„๋กœ ์˜ฌ๋ผ๊ฐ€ ์ž์‹๋…ธ๋“œ๊ฐ€ ์žˆ๊ฑฐ๋‚˜ ๋ ๋…ธ๋“œ ์ผ๋•Œ๊นŒ์ง€ ๋ฐ˜๋ณตํ•œ๋‹ค.
  • ๋งŒ์•ฝ ๋ฌธ์ž์—ด์˜ ๋ ๋…ธ๋“œ ์•„๋ž˜ ์ž์‹๋…ธ๋“œ๊ฐ€ ๋” ์กด์žฌํ•œ๋‹ค๋ฉด ํ•ด๋‹น ๋…ธ๋“œ์˜ ์ƒ‰๋งŒ ํฐ์ƒ‰์œผ๋กœ ๋‹ค์‹œ ๋ฐ”๊ฟ”์ฃผ๋ฉด ๋œ๋‹ค.

 

โœ๏ธ ํŠธ๋ผ์ด์˜ ๊ตฌํ˜„

โœจ Key, Value๋กœ ์ด๋ฃจ์–ด์ง„ ๋…ธ๋“œ๋กœ ๊ตฌ์„ฑ

  • Key : ์•ŒํŒŒ๋ฒณ
  • Value : ์ž์‹ ๋…ธ๋“œ 
Class Noe{
    HashMap<Character, Node> child;
    boolean isTerminal; // ํ˜„์žฌ Node๊ฐ€ ๋‹จ์–ด์˜ ๋ ๋…ธ๋“œ์ธ์ง€ 
}

 

โœจ Java์—์„œ Trie ๊ตฌํ˜„


Class Node{
	HashMap<Character, Node> child;
    boolean isTerminal;
    
    public Node(){
    	this.child = new HashMap<>();
        this.isTerminal = false;
    }
}

Class Trie{
	Node root;
    public Trie(){
    	this.root = new Node();
    }
    
    // ์‚ฝ์ž…
    public void insert(String str){
    	Node cur = this.root;
        
        for(int i=0; i < str.length(); i++){
        	char c = str.charAt(i);
            
            // *** HashMap<>()์— ์ €์žฅ๋œ ๋ฉ”์†Œ๋“œ putIfAbsent() ์‚ฌ์šฉ *** 
            // c ๊ฐ€ cur๊ธฐ์ค€์œผ๋กœ ์ž์‹๋…ธ๋“œ๋กœ ์—†์œผ๋ฉด ๋„ฃ์–ด์ฃผ๊ณ , ์žˆ๋‹ค๋ฉด ๋„˜์–ด๊ฐ€๊ฒŒ ๋œ๋‹ค.
            cur.child.putIfAbsent(c, new Node()); 
            
            cur = cur.child.get(c);
            
            if(i == str.length() - 1){
            	cur.isTerminal = true;
                return;
            }
        }
    }
    
    // ํƒ์ƒ‰
    public boolean search(String str){
    	Node cur = this.root;
        
        for(int i=0; i<str.length(); i++){
        	char c = str.charAt(i);
            
            if(cur.child.containsKey(c)){
            	cur = cur.child.get(c);
            }else{
            	return false;
            }
            
            if(i == str.length() - 1){
            	if(!cur.isTerminal){
                	return flse;
                }
            }
        }
        return true;
    }
    
    // ์‚ญ์ œ
    public void delete(String str){
    	boolean ret = this.delete(this.root, str, 0);
        if(ret){
        	System.out.println(str + " ์‚ญ์ œ ์™„๋ฃŒ");
        }else{
        	System.out.println(str + " ๋‹จ์–ด๊ฐ€ ์—†์Šต๋‹ˆ๋‹ค.");
        }
    }
    
    public boolean delete(Node node, String str, int idx){
    	char c = str.charAt(idx);
        
        if(!node.child.containsKey(c)){
        	return false;
        }
        
        Node cur = node.child.get(c);
        
        idx++;
       
        if(idx == str.length()){
        	if(!cur.isTerminal){
            	return false;
            }
            // ๋‹จ์–ด์˜ ๋ ๋…ธ๋“œ๊ฐ€ ๋งž๋‹ค๋ฉด
            cur.isTerminal = false;
            
            if(cur.child.isEmpty()){ // ์•„๋ž˜ ๋”ธ๋ฆฐ ์ž์‹๋…ธ๋“œ๋“ค์ด ์—†๋‹ค๋ฉด,
            	node.child.remove(c);
            }
        }
        else{
        	if(!this.delete(cur, str, idx)){ // ์žฌ๊ท€ํ•จ์ˆ˜ ์ง„ํ–‰ 
            	return false;
            }
            if(!cur.isTerminal && cur.child.isEmpty()){ // ๋ ๋ฌธ์ž๋„ ์•„๋‹ˆ๊ณ , ํ•ด๋‹น ๋…ธ๋“œ์—์„œ ํŒŒ์ƒ๋˜๋Š” ๋ฌธ์ž๋„ ์—†๋‹ค๋ฉด ๊ฐ™์ด ์‚ญ์ œ
            	node.child.remove(c);
            }
        }
        return true;
    }
}