Jelajahi Sumber

bugfixes for features in last commit

tarfeef101 7 tahun lalu
induk
melakukan
b511b76b44
3 mengubah file dengan 65 tambahan dan 47 penghapusan
  1. 48 33
      source1.h
  2. 17 14
      test.cc
  3. TEMPAT SAMPAH
      testfile

+ 48 - 33
source1.h

@@ -144,6 +144,22 @@ class Triehard // compressed binary trie
 				{
 					right = node;
 				}
+				
+				int sumMag()
+				{
+					if (left && right) return magnitude + left->sumMag() + right->sumMag();
+					if (left) return magnitude + left->sumMag();
+					if (right) return magnitude + right->sumMag();
+					return magnitude;
+				}
+    
+    			int sumCount()
+    			{
+    				if (left && right) return 1 + left->sumCount() + right->sumCount();
+    				if (left) return 1 + left->sumCount();
+    				if (right) return 1 + right->sumCount();
+    				return 1;
+    			}
 		};
 		
 		Trienode * left;
@@ -224,7 +240,7 @@ class Triehard // compressed binary trie
 		{
 			Trienode * curnode;
 			bool side; // represents if you are on the left or right (right being true)
-			if (*val[0])
+			if ((*val)[0])
 			{
 				curnode = right;
 				side = true;
@@ -239,7 +255,7 @@ class Triehard // compressed binary trie
 			
 			for (int i = 0; i < val->size(); i++) // each iteration checks the current character for accuracy. it does not prepare for the next character like the preamble
 			{
-				if (*val[i]) // if next digit is 1
+				if ((*val)[i]) // if next digit is 1
 				{
 					if (side) // if you're on the right
 					{
@@ -332,11 +348,11 @@ class Triehard // compressed binary trie
 			return 0;
 		}
 		
-		void insert(vector<int> * val, int len) // assumes valid input
+		void insert(vector<int> * val) // assumes valid input
 		{
 			Trienode * curnode; // the node we are checking against our current value
 			bool side; // represents if you are on the left or right (right being true)
-			if (*val[0])
+			if ((*val)[0])
 			{
 				curnode = right;
 				side = true;
@@ -351,7 +367,7 @@ class Triehard // compressed binary trie
 			
 			for (int i = 0; i < val->size(); i++)
 			{
-				if (*val[i]) // if current digit is 1
+				if ((*val)[i]) // if current digit is 1
 				{
 					if (side) // if you're on the right
 					{
@@ -520,13 +536,13 @@ class Triehard // compressed binary trie
 			}
 		}
 		
-		void cut(vector<int> * val, int len) // this is delete because i can't use delete :(
+		void cut(vector<int> * val) // this is delete because i can't use delete :(
 		{
 			Trienode * curnode;
 			Trienode * prevnode = nullptr;
 			bool side; // represents if you are on the left or right (right being true)
 			bool side2; // previous node's side
-			if (*val[0])
+			if ((*val)[0])
 			{
 				curnode = right;
 				side = true;
@@ -543,7 +559,7 @@ class Triehard // compressed binary trie
 			
 			for (int i = 0; i < val->size(); i++) // each iteration checks the current character for accuracy. it does not prepare for the next character like the preamble
 			{
-				if (*val[i]) // if next digit is 1
+				if ((*val)[i]) // if next digit is 1
 				{
 					if (side) // if you're on the right
 					{
@@ -737,37 +753,36 @@ class Triehard // compressed binary trie
 		}
     
     // update counter with children recursively
-		void mainCount(Trienode * curnode, int len, int right, int * counter)
-		{
-			if (!curnode) return;
-      len += curnode->getMag();		
-			*counter += (len * curnode->getCount());
-			
-			mainCount(curnode->getLeft(), len, 0, counter);
-			mainCount(curnode->getRight(), len, 1, counter);
-		}
+	void mainCount(Trienode * curnode, int len, int right, int * counter)
+	{
+		if (!curnode) return;
+		len += curnode->getMag();
+		*counter += (len * curnode->getCount());
+		mainCount(curnode->getLeft(), len, 0, counter);
+		mainCount(curnode->getRight(), len, 1, counter);
+	}
 		
-		int countChars() // returns total word length of trie
-		{
-      int counter = 0;
-			mainCount(left, 0, 0, &counter);
-			mainCount(right, 0, 1, &counter);
-      return counter;
-		}
+	int countChars() // returns total word length of trie
+	{
+		int counter = 0;
+		if (left) mainCount(left, 0, 0, &counter);
+		if (right) mainCount(right, 0, 1, &counter);
+		return counter;
+	}
     
     float compressionovertrie() // returns nodes / nodes in a normal trie
-		{
-      float total = left->sumMag() + right->sumMag();
-      float compressed = left->sumCount() + right->sumCount();
-      return roundf(compressed/total * 100) / 100;
-		}
+	{
+		float total = left->sumMag() + right->sumMag();
+		float compressed = left->sumCount() + right->sumCount();
+		return roundf(compressed/total * 100) / 100;
+	}
     
     float compressionoverdict() // returns nodes / sum of all word length
     {
-      float compressed = left->sumCount() + right->sumCount();
-      float total = countChars();
-      return roundf(compressed/total * 100) / 100;
-      
+    	float compressed = left->sumCount() + right->sumCount();
+    	float total = countChars();
+    	return roundf(compressed/total * 100) / 100;
+    }
 };
 
 #endif

+ 17 - 14
test.cc

@@ -1,34 +1,37 @@
 #include "source1.h"
 #include <iostream>
+#include <vector>
 using namespace std;
 
 int main()
 {
 	Triehard * test = new Triehard();
 	cout << "success! Created test" << endl;
-	int x[3] = {0,1,0};
-	int y[5] = {1,0,1,1,0};
-	int z[1] = {0};
-	int a[7] = {1,0,1,1,0,0,0};
-	int b[7] = {0,1,1,0,0,1,1};
-	cout << "Search result for searching 010: " << test->search(x, 3) << endl;
-	test->insert(x, 3);
+	vector<int> x = {0,1,0};
+	vector<int> y = {1,0,1,1,0};
+	vector<int> z = {0};
+	vector<int> a = {1,0,1,1,0,0,0};
+	vector<int> b = {0,1,1,0,0,1,1};
+	cout << "Search result for searching 010: " << test->search(&x) << endl;
+	test->insert(&x);
 	cout << "success! inserted x" << endl;
-	test->insert(a, 7);
+	test->insert(&a);
 	cout << "success! inserted a" << endl;
-	test->insert(y, 5);
+	test->insert(&y);
 	cout << "success! inserted y" << endl;
-	test->insert(z, 1);
+	test->insert(&z);
 	cout << "success! inserted z" << endl;
-	test->insert(b, 7);
+	test->insert(&b);
 	cout << "success! inserted b" << endl;
 	test->myPrintIsBetterThanYoursLogan();
 	cout << "Print Done!" << endl;
-	cout << "Search result for 10110: " << test->search(y, 5) << endl;
-	test->cut(y, 5);
+	cout << "Search result for 10110: " << test->search(&y) << endl;
+	cout << "Compression rate compared to a standard trie: " << test->compressionovertrie() << endl;
+	cout << "Compression rate compared to a list of lists: " << test->compressionoverdict() << endl;
+	test->cut(&y);
 	cout << "success! cut y" << endl;
 	test->myPrintIsBetterThanYoursLogan();
-	cout << "Search result for 10110: " << test->search(y, 5) << endl;
+	cout << "Search result for 10110: " << test->search(&y) << endl;
 	delete test;
 	cout << "success! Completed test" << endl;
 	//while(1);

TEMPAT SAMPAH
testfile