Skip to content

Commit

Permalink
finish part 1
Browse files Browse the repository at this point in the history
  • Loading branch information
tecty committed Oct 13, 2017
1 parent 4cce13a commit 7c02eb7
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 13 deletions.
60 changes: 47 additions & 13 deletions week11/Tree.c
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,8 @@ Link insert(Link t, Item it)
{
if (t == NULL) return newNode(it);
int diff = cmp(key(it),key(t->value));
// increment the conting of comparison
thisTree->ncompares++;
if (diff == 0)
t->value = it;
else if (diff < 0)
Expand All @@ -167,9 +169,11 @@ Link insert(Link t, Item it)
}

Link insertAtRoot(Link t, Item it)
{
{
if (t == NULL) return newNode(it);
int diff = cmp(key(it), key(t->value));
// increment the conting of comparison
thisTree->ncompares++;
if (diff == 0)
t->value = it;
else if (diff < 0) {
Expand Down Expand Up @@ -209,16 +213,20 @@ Link insertSplay(Link t, Item it)
Key v = key(it);
if (t == NULL) return newNode(it);
int diff = cmp(v,key(t->value));
// increment the conting of comparison
thisTree->ncompares++;
if (diff == 0)
t->value = it;
else if (diff < 0) {
else if (diff < 0) {
if (t->left == NULL) {
t->left = newNode(it);
return t;
}
if (less(v,key(t->left->value))) {
// record one more compares
thisTree->ncompares++;
t->left->left = insertSplay(t->left->left, it);
t = rotateR(t);
t = rotateR(t);
} else {
t->left->right = insertSplay(t->left->right, it);
t->left = rotateL(t->left);
Expand All @@ -231,6 +239,8 @@ Link insertSplay(Link t, Item it)
return t;
}
if (less(key(t->right->value),v)) {
// record one more compares
thisTree->ncompares++;
t->right->right = insertSplay(t->right->right, it);
t = rotateL(t);
} else {
Expand All @@ -246,6 +256,8 @@ static Link insertAVL(Link t, Item it)
{
if (t == NULL) return newNode(it);
int diff = cmp(key(it), key(t->value));
// increment the conting of comparison
thisTree->ncompares++;
if (diff == 0)
t->value = it;
else if (diff < 0)
Expand Down Expand Up @@ -278,6 +290,8 @@ static Link search(Link t, Key k)
if (t == NULL) return NULL;
Link res = NULL;
int diff = cmp(k,t->value);
// increment the conting of comparison
thisTree->ncompares++;
if (diff == 0)
res = t;
else if (diff < 0)
Expand All @@ -293,24 +307,32 @@ static Link searchSplay(Link t, Key k, int *found)
if (t == NULL) {
// item not found
*found = 0;
res = NULL;
res = NULL;
}
else if (eq(key(t->value),k)) {
*found = 1; // item found, store true
res = t;
// record one more comparison
thisTree->ncompares++;
*found = 1; // item found, store true
res = t;
}
else if (less(k,key(t->value))) {
// record one more compares
thisTree->ncompares++;
if (t->left == NULL){
*found = 0;// item not found
//res = rotateRight(t);
//res = rotateRight(t);
res = t;
}
else if (eq(key(t->left->value),k)) {
// record one more comparison
thisTree->ncompares++;
*found = 1;
res = rotateR(t);
}
else {
if (less(k,key(t->left->value))) {
// record one more compares
thisTree->ncompares++;
// left-left
t->left->left = searchSplay(t->left->left, k, found);
t = rotateR(t);
Expand All @@ -320,24 +342,28 @@ static Link searchSplay(Link t, Key k, int *found)
t->left->right = searchSplay(t->left->right, k, found);
t->left = rotateL(t->left);
}
res = rotateR(t);
res = rotateR(t);
}
}
else { // k > key(t->value)
if (t->right == NULL) {
*found = 0;// item not found
//res = rotateLeft(t);
res = t;
}
}
else if (eq(key(t->right->value),k)) {
// record one more comparison
thisTree->ncompares++;
*found = 1;
res = rotateL(t);
}
else{
if (less(key(t->right->value),k)) {
// record one more compares
thisTree->ncompares++;
/* right-right */
t->right->right = searchSplay(t->right->right, k, found);
t = rotateL(t);
t = rotateL(t);
}
else {
/* right-left */
Expand All @@ -356,11 +382,13 @@ void TreeDelete(Tree t, Key k)
t->root = delete(t->root,k);
}

// Helper: recursive delete
// Helper: recursive delete
static Link delete(Link t, Key k)
{
if (t == NULL) return NULL;
int diff = cmp(k,t->value);
// increment the conting of comparison
thisTree->ncompares++;
if (diff == 0)
t = deleteRoot(t);
else if (diff < 0)
Expand Down Expand Up @@ -409,6 +437,9 @@ static Link deleteRoot(Link t)
// Helper: rotate tree right around root
Link rotateR(Link n1)
{
// record one more rotate
thisTree->nrotates ++;

if (n1 == NULL) return NULL;
Link n2 = n1->left;
if (n2 == NULL) return n1;
Expand All @@ -420,6 +451,9 @@ Link rotateR(Link n1)
// Helper: rotate tree left around root
Link rotateL(Link n2)
{
// record one more rotate
thisTree->nrotates ++;

if (n2 == NULL) return NULL;
Link n1 = n2->right;
if (n1 == NULL) return n2;
Expand Down Expand Up @@ -496,9 +530,9 @@ int rprofile[MAX_HEIGHT];

int gap = 3; // gap between left and right nodes

//used for printing next node in the same level,
//used for printing next node in the same level,
//this is the x coordinate of the next char printed
int print_next;
int print_next;

//prints ascii tree for given Tree structure
static void doShowTree(Link t)
Expand Down
Binary file modified week11/tlab
Binary file not shown.

0 comments on commit 7c02eb7

Please sign in to comment.