Skip to content

Commit

Permalink
add KMP cpp and build BST
Browse files Browse the repository at this point in the history
  • Loading branch information
Yingyi Zhang committed Oct 3, 2017
1 parent 3e9067e commit 49e78b9
Show file tree
Hide file tree
Showing 2 changed files with 145 additions and 0 deletions.
47 changes: 47 additions & 0 deletions KMP/KMP.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
#include<iostream>
#include<vector>
using namespace std;
void kmp(string T, string P);
vector<int> preprocess(string P);
int main()
{
string T = "sagertehg";
string P = "ge";
kmp(T, P);
return 0;
}

void kmp(string T, string P){
T = " " + T;
P = " " + P;
vector<int> prefix = preprocess(P);
int k = 0;
for(int i = 1; i < T.length(); ++i){
while(k > 0 && P[k + 1] != T[i]){
k = prefix[k];
}
if(P[k + 1] == T[i]){
k += 1;
}
if(k == P.length() - 1){
cout << "find match at "<<i <<endl;
k = prefix[k];
}
}
}

vector<int> preprocess(string P){
int k = 0;
vector<int> res(P.length());
res[1] = 0;
for(int i = 2; i < P.length();++i){
while(k >0 && P[k + 1] != P[i]){
k = res[k];
}

if(P[k + 1] == P[i])
k += 1;
res[i] = k;
}
return res;
}
98 changes: 98 additions & 0 deletions RebuildBST.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
#include <iostream>
#include <vector>

using namespace std;

class Node{
public:
int val;
Node *left;
Node *right;
Node(int v): val(v){
left = NULL;
right = NULL;
}
};

Node* convert(vector<int> &vec, int start, int end);
void preOrderTraverse(Node* root);
void inOrderTraverse(Node* root);
Node* convert2(vector<int>&vec, int parentVal);
Node* newConvert(vector<int>&vec);

int main(){
int arr[] ={10, 7, 5, 9, 8, 15, 12};
int len = sizeof(arr) / sizeof(int);
vector<int> vec1(&arr[0], &arr[len]);
Node* res1 = convert(vec1, 0, len - 1); // O(N^2)
vector<int> vec2(&arr[0], &arr[len]); // O(N)
Node* res2 = newConvert(vec2);
cout << "Preorder traverse of BST generated by two methods: " << endl;
preOrderTraverse(res1);
cout << endl;
preOrderTraverse(res2);
cout << endl << endl;
cout << "Inorder traverse of BST generated by two methods:" << endl;
inOrderTraverse(res1);
cout << endl;
inOrderTraverse(res2);
cout << endl << endl;
return 0;
}

Node* convert(vector<int> &vec, int start, int end){ // O(N^2)
if(start > end) return NULL;
int rootVal = vec[start];
Node* root = new Node(rootVal);
int index = end + 1;
for(int i = start + 1; i <= end; ++i){
if(vec[i] > rootVal){
index = i;
break;
}
}
root->left = convert(vec, start + 1, index - 1);
root->right = convert(vec, index, end);
return root;
}

Node* newConvert(vector<int>& vec){
int rootVal = vec[0];
Node* root = new Node(rootVal);
vec.erase(vec.begin());
if(rootVal > vec[0]){
root -> left = convert2(vec, rootVal); // call convert2 method
}
if(rootVal < vec[0]){
root -> right = convert2(vec, rootVal); // call convert2 method
}
return root;
}

Node* convert2(vector<int> &vec, int parentVal){ // O(N)
if(vec.size() == 0) return NULL;
int rootVal = vec[0];
Node* root = new Node(rootVal);
vec.erase(vec.begin()); // erase the first element
if(rootVal > vec[0]){
root -> left = convert2(vec, rootVal);
}
if(vec[0] < parentVal && rootVal < vec[0]){ // compare with parent value
root -> right = convert2(vec, rootVal);
}
return root;
}

void preOrderTraverse(Node* root){
if(root == NULL) return;
cout << root->val << ", ";
preOrderTraverse(root->left);
preOrderTraverse(root->right);
}

void inOrderTraverse(Node* root){
if(root == NULL) return;
inOrderTraverse(root->left);
cout << root->val << ", ";
inOrderTraverse(root->right);
}

0 comments on commit 49e78b9

Please sign in to comment.