-
Notifications
You must be signed in to change notification settings - Fork 1.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Added Trie #162
Open
sepehrs1378
wants to merge
11
commits into
emirpasic:master
Choose a base branch
from
sepehrs1378:Trie
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Added Trie #162
Changes from 9 commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
5cb52df
added Reverse, Count, Replace functions to arraylist
sepehrs1378 f295d51
added Trie
sepehrs1378 af058a3
Trie Refactor
AlirezaT99 2d5e6e0
Trees/Trie Modified
AlirezaT99 d00bf01
added Trie
sepehrs1378 6d1b74e
Trie Refactored
AlirezaT99 2896b35
Trie NodesCount and test added
AlirezaT99 30954bc
serialization and iterator
parhamsaremi e325670
merged
parhamsaremi 6115212
fixed bad merge
parhamsaremi 9d4cad0
Merge pull request #1 from sepehrs1378/merged
parhamsaremi File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
// Copyright (c) 2015, Emir Pasic. All rights reserved. | ||
// Use of this source code is governed by a BSD-style | ||
// license that can be found in the LICENSE file. | ||
|
||
package main | ||
|
||
import ( | ||
"fmt" | ||
trie "github.com/emirpasic/gods/trees/trie" | ||
) | ||
|
||
func main() { | ||
fmt.Println("a") | ||
t := trie.New() | ||
words := []string{"sam", "john", "tim", "jose", "rose", | ||
"cat", "dog", "dogg", "roses"} | ||
for i := 0; i < len(words); i++ { | ||
t.Insert(words[i]) | ||
} | ||
wordsToFind := []string{ | ||
"sam", "john", "tim", "jose", "rose", | ||
"cat", "dog", "dogg", "roses", "rosess", "ans", "san", | ||
} | ||
for i := 0; i < len(wordsToFind); i++ { | ||
found := t.Contains(wordsToFind[i]) | ||
if found { | ||
fmt.Printf("Word \"%s\" found in trie\n", wordsToFind[i]) | ||
} else { | ||
fmt.Printf("Word \"%s\" not found in trie\n", wordsToFind[i]) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
package trie | ||
|
||
import "github.com/emirpasic/gods/containers" | ||
|
||
func assertIteratorImplementation() { | ||
var _ containers.ReverseIteratorWithIndex = (*Iterator)(nil) | ||
} | ||
|
||
// Iterator holding the iterator's state | ||
type Iterator struct { | ||
words []string | ||
trie *Trie | ||
index int | ||
} | ||
|
||
// Iterator returns a stateful iterator whose values can be fetched by an index. | ||
func (trie *Trie) Iterator() Iterator { | ||
s := make([]string, 10) | ||
findWords(trie.root, "", s) | ||
return Iterator{words:s, trie: trie, index:-1} | ||
} | ||
|
||
// Next moves the iterator to the next element and returns true if there was a next element in the container. | ||
// If Next() returns true, then next element's index and value can be retrieved by Index() and Value(). | ||
// If Next() was called for the first time, then it will point the iterator to the first element if it exists. | ||
// Modifies the state of the iterator. | ||
func (iterator *Iterator) Next() bool { | ||
if iterator.index < iterator.heap.Size() { | ||
iterator.index++ | ||
} | ||
return iterator.words.withinRange(iterator.index) | ||
} | ||
|
||
// Prev moves the iterator to the previous element and returns true if there was a previous element in the container. | ||
// If Prev() returns true, then previous element's index and value can be retrieved by Index() and Value(). | ||
// Modifies the state of the iterator. | ||
func (iterator *Iterator) Prev() bool { | ||
if iterator.index >= 0 { | ||
iterator.index-- | ||
} | ||
return iterator.words.withinRange(iterator.index) | ||
} | ||
|
||
// Value returns the current element's value. | ||
// Does not modify the state of the iterator. | ||
func (iterator *Iterator) Value() interface{} { | ||
value, _ := iterator.words.list.Get(iterator.index) | ||
return value | ||
} | ||
|
||
// Index returns the current element's index. | ||
// Does not modify the state of the iterator. | ||
func (iterator *Iterator) Index() int { | ||
return iterator.index | ||
} | ||
|
||
// Begin resets the iterator to its initial state (one-before-first) | ||
// Call Next() to fetch the first element if any. | ||
func (iterator *Iterator) Begin() { | ||
iterator.index = -1 | ||
} | ||
|
||
// End moves the iterator past the last element (one-past-the-end). | ||
// Call Prev() to fetch the last element if any. | ||
func (iterator *Iterator) End() { | ||
iterator.index = iterator.words.Size() | ||
} | ||
|
||
// First moves the iterator to the first element and returns true if there was a first element in the container. | ||
// If First() returns true, then first element's index and value can be retrieved by Index() and Value(). | ||
// Modifies the state of the iterator. | ||
func (iterator *Iterator) First() bool { | ||
iterator.Begin() | ||
return iterator.Next() | ||
} | ||
|
||
// Last moves the iterator to the last element and returns true if there was a last element in the container. | ||
// If Last() returns true, then last element's index and value can be retrieved by Index() and Value(). | ||
// Modifies the state of the iterator. | ||
func (iterator *Iterator) Last() bool { | ||
iterator.End() | ||
return iterator.Prev() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
package trie | ||
|
||
import "github.com/emirpasic/gods/containers" | ||
|
||
func assertSerializationImplementation() { | ||
var _ containers.JSONSerializer = (*Trie)(nil) | ||
var _ containers.JSONDeserializer = (*Trie)(nil) | ||
} | ||
|
||
// ToJSON outputs the JSON representation of the heap. | ||
func (t *Trie) ToJSON() ([]byte, error) { | ||
elements := make([]interface{}) | ||
it := trie.Iterator(t) | ||
for it.Next() { | ||
elements.append(it.Value()) | ||
} | ||
return json.Marshal(&elements) | ||
} | ||
|
||
// FromJSON populates the tree from the input JSON representation. | ||
func (t *Trie) FromJSON(data []byte) error { | ||
elements := make([]interface{}) | ||
err := json.Unmarshal(data, &elements) | ||
if err == nil { | ||
for _, value := range elements { | ||
t.insert(value) | ||
} | ||
} | ||
return err | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
package trie | ||
|
||
const ( | ||
// AlphabetSize total characters in english alphabet | ||
AlphabetSize = 26 | ||
) | ||
|
||
<<<<<<< HEAD | ||
type TrieNode struct { | ||
children [AlphabetSize]*TrieNode | ||
isWordEnd bool | ||
} | ||
|
||
type Trie struct { | ||
root *TrieNode | ||
} | ||
|
||
// New returns a pointer pointing to a new Trie | ||
func New() *Trie { | ||
return &Trie{ | ||
root: &TrieNode{}, | ||
} | ||
} | ||
|
||
// Insert inserts a word into the Trie | ||
func (t *Trie) Insert(word string) { | ||
======= | ||
type trieNode struct { | ||
children [AlphabetSize]*trieNode | ||
isWordEnd bool | ||
} | ||
|
||
type trie struct { | ||
root *trieNode | ||
nodesCount int | ||
} | ||
|
||
// New returns a pointer pointing to a new trie | ||
func New() *trie { | ||
return &trie{ | ||
root: &trieNode{}, | ||
} | ||
} | ||
|
||
// Insert inserts a word into the trie | ||
func (t *trie) Insert(word string) { | ||
>>>>>>> 2896b350131353759aacf42da7047972425068eb | ||
wordLength := len(word) | ||
current := t.root | ||
for i := 0; i < wordLength; i++ { | ||
index := word[i] - 'a' | ||
if current.children[index] == nil { | ||
<<<<<<< HEAD | ||
current.children[index] = &TrieNode{} | ||
======= | ||
current.children[index] = &trieNode{} | ||
t.nodesCount++ | ||
>>>>>>> 2896b350131353759aacf42da7047972425068eb | ||
} | ||
current = current.children[index] | ||
} | ||
current.isWordEnd = true | ||
} | ||
|
||
<<<<<<< HEAD | ||
// Contains checks whether the Trie has the given word or not | ||
func (t *Trie) Contains(word string) bool { | ||
======= | ||
// Contains checks whether the trie has the given word or not | ||
func (t *trie) Contains(word string) bool { | ||
>>>>>>> 2896b350131353759aacf42da7047972425068eb | ||
wordLength := len(word) | ||
current := t.root | ||
for i := 0; i < wordLength; i++ { | ||
index := word[i] - 'a' | ||
if current.children[index] == nil { | ||
return false | ||
} | ||
current = current.children[index] | ||
} | ||
|
||
return current.isWordEnd | ||
<<<<<<< HEAD | ||
} | ||
|
||
func (trie *TrieNode, value string, values []string) findWords(){ | ||
for i := 0; i< 26 ; i++ { | ||
if trie.isWordEnd { | ||
values.append(value+string(i+'a')) | ||
} | ||
if trie.children[i] != nil { | ||
findWords(trie.children[i], value+string(i+'a'), values) | ||
} | ||
} | ||
======= | ||
} | ||
|
||
// NodesCount returns number of nodes in the tree. | ||
func (t *trie) NodesCount() int { | ||
return t.nodesCount | ||
} | ||
|
||
// Empty returns true if tree does not contain any nodes except for root | ||
func (t *trie) Empty() bool { | ||
return t.nodesCount == 0 | ||
>>>>>>> 2896b350131353759aacf42da7047972425068eb | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@sepehrs1378 Conflicts?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sorry about that. My friend fixed it.