手动字典🌲?!

24次阅读

共计 1575 个字符,预计需要花费 4 分钟才能阅读完成。

  • 我最开始是这样尝试理解字典树的结构的。。。。。。🐶

class ManualTrie{Node root;
	ManualTrie(){root = new Node<>();}
static	class Node{
		E value;
		Node parent;
		Node a;
		Node e;
		Node r;
		Node s;
		Node t;
	
	   	Node(){
			this.value = null;
			this.parent = null;
			this.a = null;
			this.e = null;
			this.r = null;
			this.s = null;
			this.t = null;
		}
	}
	void find(E value){
		Node current = root;
		char[] chars = value.toString().toCharArray();
		for(char c:chars){if(c == 'a'&¤t.a!=null) current = current.a;
			if(c == 'e'&¤t.e!=null) current = current.e;
			if(c == 'r'&¤t.r!=null) current = current.r;
			if(c == 's'&¤t.s!=null) current = current.s;
			if(c == 't'&¤t.t!=null) current = current.t;
		}
		System.out.println(current.value);
	}
		
	public static void main(String[] args){ManualTrie Trie = new ManualTrie();
		Trie.root.value = "eat";

		//Node nodea = new Node<>();
		Trie.root.a = new Node<>();
		Trie.root.a.parent = Trie.root;
		Trie.root.a.value = "a";

		Node nodeat = new Node<>();
		Trie.root.a.t = nodeat;
		Trie.root.a.t.parent = Trie.root.a;
		Trie.root.a.t.value = "at";

		Node nodeate = new Node<>();
		Trie.root.a.t.e = nodeate;
		Trie.root.a.t.e.parent = Trie.root.a.t;
		Trie.root.a.t.e.value = "ate";
		

		Node emptye = new Node<>();
		Trie.root.e = emptye;
		Trie.root.e.parent = Trie.root;		

		Node emptya = new Node<>();
		Trie.root.e.a = emptya;
		Trie.root.e.a.parent = Trie.root.e;

		Node ear = new Node<>();
		Trie.root.e.a.r = ear;
		Trie.root.e.a.r.parent = Trie.root.e.a;
		Trie.root.e.a.r.value = "ear";
		
		Node emptys = new Node<>();
		Trie.root.e.a.s = emptys;
		Trie.root.e.a.s.parent = Trie.root.e.a;
		
	
		Node nodeeast = new Node<>();
		Trie.root.e.a.s.t = nodeeast;
		Trie.root.e.a.s.t.parent = Trie.root.e.a.s;
		Trie.root.e.a.s.t.value = "east";
		
		Node nodeeat = new Node<>();
		Trie.root.e.a.t = nodeeat;
		Trie.root.e.a.t.parent = Trie.root.a.t;
		Trie.root.e.a.t.value = "eat";
	
		Trie.find("east");
	}

}

正文完
 0