-
Notifications
You must be signed in to change notification settings - Fork 0
Trie #3
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
Open
DmKazakov
wants to merge
1
commit into
master
Choose a base branch
from
hw-3
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
Trie #3
Changes from all commits
Commits
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 hidden or 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,49 @@ | ||
| # Covers JetBrains IDEs: IntelliJ, RubyMine, PhpStorm, AppCode, PyCharm, CLion, Android Studio and Webstorm | ||
| # Reference: https://intellij-support.jetbrains.com/hc/en-us/articles/206544839 | ||
|
|
||
| # User-specific stuff: | ||
| .idea/**/workspace.xml | ||
| .idea/**/tasks.xml | ||
| .idea/dictionaries | ||
|
|
||
| # Sensitive or high-churn files: | ||
| .idea/**/dataSources/ | ||
| .idea/**/dataSources.ids | ||
| .idea/**/dataSources.xml | ||
| .idea/**/dataSources.local.xml | ||
| .idea/**/sqlDataSources.xml | ||
| .idea/**/dynamic.xml | ||
| .idea/**/uiDesigner.xml | ||
|
|
||
| # Gradle: | ||
| .idea/**/gradle.xml | ||
| .idea/**/libraries | ||
|
|
||
| # CMake | ||
| cmake-build-debug/ | ||
|
|
||
| # Mongo Explorer plugin: | ||
| .idea/**/mongoSettings.xml | ||
|
|
||
| ## File-based project format: | ||
| *.iws | ||
|
|
||
| ## Plugin-specific files: | ||
|
|
||
| # IntelliJ | ||
| out/ | ||
|
|
||
| # mpeltonen/sbt-idea plugin | ||
| .idea_modules/ | ||
|
|
||
| # JIRA plugin | ||
| atlassian-ide-plugin.xml | ||
|
|
||
| # Cursive Clojure plugin | ||
| .idea/replstate.xml | ||
|
|
||
| # Crashlytics plugin (for Android Studio and IntelliJ) | ||
| com_crashlytics_export_strings.xml | ||
| crashlytics.properties | ||
| crashlytics-build.properties | ||
| fabric.properties |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or 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,16 @@ | ||
| <?xml version="1.0" encoding="UTF-8"?> | ||
| <module org.jetbrains.idea.maven.project.MavenProjectsManager.isMavenModule="true" type="JAVA_MODULE" version="4"> | ||
| <component name="NewModuleRootManager" LANGUAGE_LEVEL="JDK_1_5"> | ||
| <output url="file://$MODULE_DIR$/target/classes" /> | ||
| <output-test url="file://$MODULE_DIR$/target/test-classes" /> | ||
| <content url="file://$MODULE_DIR$"> | ||
| <sourceFolder url="file://$MODULE_DIR$/src/main/java" isTestSource="false" /> | ||
| <sourceFolder url="file://$MODULE_DIR$/src/main/resources" type="java-resource" /> | ||
| <sourceFolder url="file://$MODULE_DIR$/src/test/java" isTestSource="true" /> | ||
| <excludeFolder url="file://$MODULE_DIR$/target" /> | ||
| </content> | ||
| <orderEntry type="inheritedJdk" /> | ||
| <orderEntry type="sourceFolder" forTests="false" /> | ||
| <orderEntry type="library" name="Maven: junit:junit:4.0" level="project" /> | ||
| </component> | ||
| </module> |
This file contains hidden or 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,20 @@ | ||
| <?xml version="1.0" encoding="UTF-8"?> | ||
| <project xmlns="http://maven.apache.org/POM/4.0.0" | ||
| xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
| xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> | ||
| <modelVersion>4.0.0</modelVersion> | ||
|
|
||
| <groupId>ru.spbau.mit.kazakov</groupId> | ||
| <artifactId>Trie</artifactId> | ||
| <version>1.0-SNAPSHOT</version> | ||
| <packaging>jar</packaging> | ||
|
|
||
| <dependencies> | ||
| <dependency> | ||
| <groupId>junit</groupId> | ||
| <artifactId>junit</artifactId> | ||
| <version>4.0</version> | ||
| </dependency> | ||
| </dependencies> | ||
|
|
||
| </project> |
This file contains hidden or 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,133 @@ | ||
| package ru.spbau.mit.kazakov.Trie; | ||
|
|
||
| import java.io.*; | ||
| import java.util.Hashtable; | ||
|
|
||
| /** | ||
| * Prefix tree for storing strings. | ||
| */ | ||
| public class Trie implements Serializable { | ||
| private Node root = new Node(); | ||
|
|
||
| /** | ||
| * Checks if there is specified string in trie. | ||
| * | ||
| * @param element a string to check | ||
| * @return true if there is specified string in trie, and false otherwise | ||
| */ | ||
| public boolean contains(String element) { | ||
| Node current = root; | ||
|
|
||
| for (int i = 0; i < element.length(); i++) { | ||
| if (!current.edges.containsKey(element.charAt(i))) { | ||
| return false; | ||
| } | ||
| current = current.edges.get(element.charAt(i)); | ||
| } | ||
|
|
||
| return current.isTerminal; | ||
| } | ||
|
|
||
| /** | ||
| * Adds specified string to trie. | ||
| * | ||
| * @param element a string to add | ||
| * @return true if there was no specified string in trie, and false otherwise | ||
| */ | ||
| public boolean add(String element) { | ||
| if (contains(element)) { | ||
| return false; | ||
| } | ||
|
|
||
| Node current = root; | ||
| for (int i = 0; i < element.length(); i++) { | ||
| current.numOfContinuations++; | ||
| if (!current.edges.containsKey(element.charAt(i))) { | ||
| current.edges.put(element.charAt(i), new Node()); | ||
| } | ||
| current = current.edges.get(element.charAt(i)); | ||
| } | ||
| current.numOfContinuations++; | ||
| current.isTerminal = true; | ||
|
|
||
| return true; | ||
| } | ||
|
|
||
| /** | ||
| * Counts how many strings in trie starts with specified string. | ||
| */ | ||
| public int howManyStartsWithPrefix(String prefix) { | ||
| Node current = root; | ||
|
|
||
| for (int i = 0; i < prefix.length(); i++) { | ||
| if (!current.edges.containsKey(prefix.charAt(i))) { | ||
| return 0; | ||
| } | ||
| current = current.edges.get(prefix.charAt(i)); | ||
| } | ||
|
|
||
| return current.numOfContinuations; | ||
| } | ||
|
|
||
| /** | ||
| * Returns number of added strings in trie. | ||
| */ | ||
| public int size() { | ||
| return root.numOfContinuations; | ||
| } | ||
|
|
||
| /** | ||
| * Removes specified string from trie. | ||
| * | ||
| * @param element a string to remove | ||
| * @return true if there was specified string in trie, and false otherwise | ||
| */ | ||
| public boolean remove(String element) { | ||
| if (!contains(element)) { | ||
| return false; | ||
| } | ||
|
|
||
| Node current = root; | ||
| for (int i = 0; i < element.length(); i++) { | ||
| current.numOfContinuations--; | ||
| Node next = current.edges.get(element.charAt(i)); | ||
| if (next.numOfContinuations == 1) { | ||
| current.edges.remove(element.charAt(i)); | ||
| } | ||
| current = next; | ||
| } | ||
| current.numOfContinuations--; | ||
| current.isTerminal = false; | ||
|
|
||
| return true; | ||
| } | ||
|
|
||
| /** | ||
| * Serializes current trie to given stream. | ||
| * | ||
| * @param out stream for writing | ||
| */ | ||
| public void serialize(OutputStream out) throws IOException { | ||
| ObjectOutputStream objectOutputStream = new ObjectOutputStream(out); | ||
| objectOutputStream.writeObject(this); | ||
| } | ||
|
|
||
| /** | ||
| * Deserializes trie from given stream and replaces current trie with new one. | ||
| * | ||
| * @param in stream for reading | ||
| */ | ||
| public void deserialize(InputStream in) throws IOException, ClassNotFoundException { | ||
| ObjectInputStream objectInputStream = new ObjectInputStream(new BufferedInputStream(in)); | ||
| root = ((Trie) objectInputStream.readObject()).root; | ||
| } | ||
|
|
||
| /** | ||
| * Vertex in trie. Stores outgoing edges with corresponding characters. | ||
| */ | ||
| private class Node implements Serializable { | ||
| private Hashtable<Character, Node> edges = new Hashtable<Character, Node>(); | ||
| private boolean isTerminal = false; | ||
| private int numOfContinuations = 0; | ||
| } | ||
| } | ||
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.
Он мог бы быть static