Skip to content
Snippets Groups Projects
Commit 089ae6f2 authored by efaal85565's avatar efaal85565
Browse files

AVL insert

parents bdcd2b1b 1d181306
No related branches found
No related tags found
No related merge requests found
......@@ -36,6 +36,7 @@ namespace Dictionaries.src.Array.Base
Console.WriteLine(this);
}
// Returns position after last element based on insert position (unsorted arrays)
protected virtual int getElementBorder()
{
return insertPosition;
......
......@@ -17,15 +17,17 @@ namespace Dictionaries.src.Array
public override bool insert(int elem)
{
if (!hasSpace())
// No more space
if (!(elementCount < elements.Length))
return false;
if (elementCount > 0)
{
search(elem);
if (!allowDuplicates && lastSearchPosition != -1)
bool wasFound = search(elem);
if (!allowDuplicates && wasFound)
return false;
// Shift every element after searched element one position to the right
for (int i = elements.Length - 2; i >= insertPosition; i--)
{
elements[i + 1] = elements[i];
......@@ -38,6 +40,7 @@ namespace Dictionaries.src.Array
return true;
}
// Search by Divide-and-Conquer approach
public override bool search(int elem)
{
if (elementCount == 0)
......@@ -48,13 +51,16 @@ namespace Dictionaries.src.Array
int endIndex = elementCount-1;
do
{
// Define middle position of current (Sub-)Array
medianIndex = (startIndex + endIndex) / 2;
if (elements[medianIndex] < elem)
{
// Check right side (mid to right outer bound)
startIndex = medianIndex + 1;
}
else
{
// Check left side (left outer bound to mid)
endIndex = medianIndex - 1;
}
} while (elements[medianIndex] != elem && startIndex <= endIndex);
......@@ -79,6 +85,7 @@ namespace Dictionaries.src.Array
elements[lastSearchPosition] = 0;
if (lastSearchPosition < elementCount - 1)
{
// Element was not last in array -> Shift everything from right side, one position to the left side
for (int i = lastSearchPosition; i < elementCount-1; i++)
{
elements[i] = elements[i + 1];
......@@ -89,12 +96,8 @@ namespace Dictionaries.src.Array
elementCount--;
return true;
}
private bool hasSpace()
{
return elementCount < elements.Length;
}
// Returns position after last element based on current elementCount (sorted Array)
protected override int getElementBorder()
{
return elementCount;
......
......@@ -12,14 +12,17 @@ namespace Dictionaries.src.Array
get { return elements[i]; }
set
{
elements[i] = value;
if (insertPosition < elements.Length)
{
elements[insertPosition] = value;
insertPosition++;
}
}
}
public override bool insert(int elem)
{
// No more space
if (insertPosition == elements.Length)
return false;
......@@ -30,6 +33,7 @@ namespace Dictionaries.src.Array
public override bool search(int elem)
{
// Define endpoint for search based on current element count
int sentinel = insertPosition == elements.Length ? elements.Length : insertPosition;
for (int i = 0; i < sentinel; i++)
{
......@@ -47,6 +51,7 @@ namespace Dictionaries.src.Array
if (!search(elem))
return false;
// Override found element with last item from array
insertPosition--;
elements[lastSearchPosition] = elements[insertPosition];
elements[insertPosition] = 0;
......
using System;
namespace Dictionaries.src.HashTable.Base
namespace Dictionaries.src.HashTable.Base
{
abstract class AbstractHashTable : ISetUnsorted
public abstract class AbstractHashTable : ISetUnsorted
{
public abstract int createHash(int elem);
public abstract bool search(int elem);
public abstract bool insert(int elem);
public abstract bool delete(int elem);
public bool delete(int elem)
{
throw new NotImplementedException();
}
public bool insert(int elem)
{
throw new NotImplementedException();
}
public void print()
{
throw new NotImplementedException();
}
public abstract void print();
public bool search(int elem)
{
throw new NotImplementedException();
}
protected int createHash(int key)
{
throw new NotImplementedException();
}
}
}
using Dictionaries.src.HashTable.Base;
using System;
namespace Dictionaries.src.HashTable
{
class HashTabQuadProb : AbstractHashTable
{
public int[] HashTable = new int[maxSize];
const int maxSize = 19;
public override int createHash(int elem)
{
return (elem % maxSize);
}
int[] table;
private bool checkOpenSpace()//Sucht nach offenen Plätzen(Nullen)
{
bool isOpen = false;
for (int i = 0; i < maxSize; i++)
{
if (HashTable[i] == 0)
{
isOpen = true;
}
}
return isOpen;
}
public override bool insert(int elem)
{
if (!checkOpenSpace())
return false;//Array ist voll
if (search(elem))
return false; // Eintrag ist bereits vorhanden
int j = 0;
int hash = createHash(elem);
while (HashTable[hash] != 0 && HashTable[hash] != elem)
{
j++;
hash = createHash(hash + j * j);
}
if (HashTable[hash] == 0)
{
HashTable[hash] = elem;
return true;
}
else
{
return false;
}
}
public override bool search(int elem)
{
int hash = createHash(elem);
int j = 0;
int i = 0;
while (HashTable[hash] != 0 && HashTable[hash] != elem && i < maxSize)
{
j++;
i++;
hash = createHash(hash + j * j);
}
if (HashTable[hash] == elem)
{
return true;
}
else
{
return false;
}
}
public override bool delete(int elem)
{
int hash = elem % maxSize;
int j = 0;
while (HashTable[hash] != 0 && HashTable[hash] != elem)
{
j++;
hash = createHash(hash + j * j);
}
if (HashTable[hash] == 0)
{
return false;
}
else
{
HashTable[hash] = 0;
return true;
}
}
public override void print()
{
for (int i = 0; i < HashTable.Length; i++)
{
Console.Write(HashTable[i] + " ");
}
}
}
}
......@@ -8,5 +8,29 @@ namespace Dictionaries.src.HashTable
SetUnsortedLinkedList[] table;
public override int createHash(int elem)
{
throw new System.NotImplementedException();
}
public override bool delete(int elem)
{
throw new System.NotImplementedException();
}
public override bool insert(int elem)
{
throw new System.NotImplementedException();
}
public override void print()
{
throw new System.NotImplementedException();
}
public override bool search(int elem)
{
throw new System.NotImplementedException();
}
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment