Skip to content
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

Added B-Tree Data Structure #95

Merged
merged 26 commits into from
Jan 7, 2022

Conversation

AzuxirenLeadGuy
Copy link
Contributor

Description: Added the following files

  • "Sources/Towel/DataStructures/BTree.cs": B-Tree Data Structure implementation
  • "Tools/Towel_Testing/DataStructures/BTree.cs": 7 unit tests for BTree, testing the Iterator, Add(), Remove() and Search() methods. All are passing.

Also changed the file setting.json to not force/override the users' current color scheme

Related Issue: #34

@ZacharyPatten
Copy link
Owner

Thanks again for being willing to contribute! I will review this PR as soon as I am available. Happy New Year :)

@AzuxirenLeadGuy
Copy link
Contributor Author

Hey, I noticed a bug in the Remove() method. I'll try to fix it and send some commits your way

@AzuxirenLeadGuy
Copy link
Contributor Author

Okay, looks like the bug is resolved. I also made the tests a bit more random and extreme, and its all passing now.

@ZacharyPatten
Copy link
Owner

The base case on the GetEnumerator method looks like it needs to be if (Count > 0) instead of if (Top != null) because Top is assigned a non-null value during construction. This unit test is failing:

[TestMethod]
public void EnumeratorTest()
{
	BTree<int> tree = new(8);
	foreach (int item in tree)
	{
		Assert.Fail();
	}
}

I can fix that myself but if you want to fix it before I have time go for it.

Also, I would like to add an entry on the readme for BTree. That can be added in a future PR, but if you want to include it here that is fine too.

@AzuxirenLeadGuy
Copy link
Contributor Author

Yes, you are right it should be Count > 0. I initially planned to make an empty BTree to have Root node as null... Looks like I forgot to change it

@AzuxirenLeadGuy
Copy link
Contributor Author

Alright, I made mention of B-Tree in the Readme. Feel free to correct my grammar or sentences if needed.

style changes
interfacing
global usings
@ZacharyPatten
Copy link
Owner

ZacharyPatten commented Jan 6, 2022

I made some changes:

  • renamed BTree -> BTreeLinked
    • Why? It is possible to make a flattened version of a BTree. Will I ever add a flattened BTree to Towel? I don't know... maybe... but that is why I wanted to name if "BTreeLinked" for now.
  • renamed BTreeNode -> Node and made nested class
  • added more interfaces to BTree (and renamed some methods to match interfaces)
  • added global usings and file-scoped namespaces (will update the rest of Towel in future)
  • it doesn't look like they are going to implement a way to properly do settings in VSC anytime soon, so I just commented out the theme settings for now in the settings.json
  • added TCompare generic parameter rather than IComparable (this is more efficient and allows multiple comparisons per T)
  • Stepper had the same bug as GetEnumerator (base case), so fixed that
  • used the existing FillArray struct rather than a new StepperToArray struct
  • various minor style changes

So only one actual bug fix. The rest of the changes was mainly to have it more closely follow the patterns/styles of the other code in Towel. A BTree is a 1-Dimensonal sorted tree like Red-Black trees or AVL Trees, so I was trying to get it to look more like those.

I still want to review it a bit more before merging it in.

@AzuxirenLeadGuy
Copy link
Contributor Author

No issues so far. I just think that the tree could have an Add(T item) method instead of TryAdd(T item). The exception thrown within it is guaranteed to never occur given the tree is working fine. I only added the exception statement to assist me in debugging (and also to remove a "null reference" warning).

Apart from that, it looks good enough to me.

@ZacharyPatten
Copy link
Owner

ZacharyPatten commented Jan 6, 2022

My plan was to have BTreeLinked<T, TCompare> implement the IAddable<T> and IRemovabled<T> interfaces. IAddable<T> has an extension method Add. I put the interfaces on BTreeLinked<T, TCompare> but I comment them out as I haven't had time to implement them yet.

Even though the current code does not throw an exception, there could be more exceptions added to detect a corrupted tree (such as due to multi-threading). The Red-Black tree in Towel throws a CorruptedDataStructureException for example.

Copy link
Contributor Author

@AzuxirenLeadGuy AzuxirenLeadGuy left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The comment should mention that "..50 is no longer in the tree" instead of 20 in the readme for the last remove statement

@ZacharyPatten
Copy link
Owner

Readme fixed.

I haven't had a lot of time to review the actual algorithm of the B-Tree, but the unit tests you made are passing, so it looks like its in a pretty good spot. This pull request looks ready to complete to me. I wouldn't be surprised if I introduced a bug in my changes, but any bugs can be ironed out in future changes/pull-requests. :)

@ZacharyPatten ZacharyPatten merged commit c85e741 into ZacharyPatten:main Jan 7, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants