patterncsharpMinor
A* path finding algorithm for a red-black tree
Viewed 0 times
pathredalgorithmforfindingtreeblack
Problem
So far I've implemented a way to add to my
I want to add a remove function to this tree, but can't find anything online that suits my style of tree. Any help? I only need to delete the first node. Also, what could I improve in my current code?
```
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace RedBlackTree
{
//From Wiki:
//The performance of an AA tree is equivalent to the performance of a red-black tree.
//While an AA tree makes more rotations than a red-black tree, the simpler algorithms tend to be faster, and all of this balances out to result in similar performance.
//A red-black tree is more consistent in its performance than an AA tree, but an AA tree tends to be flatter, which results in slightly faster search times.
class RedBlackTree
{
private const bool red = false;
private const bool black = true;
private rbtNode root;
private int _count = 0;
public int count { get { return _count; } }
//public bool Contains(PNode Key)
//{
// Node current = root;
// while (current != null)
// {
// if (current.Key == Key)
// {
// return true;
// }
// else if (value current.F)
{
if (current.right == null)
{
current.right = n;
break;
}
else
{
current = current.right;
}
}
}
n.parent = current;
}
_count++;
InsertCase1(n);
}
//Root node test, make it black
private void InsertCase1(rbtNode n)
{
if (n.parent == null)
{
n.colour = black;
}
RedBlackTree (will be used with A* pathfinding - hence the F value). I'm not entirely sure how to improve it further.I want to add a remove function to this tree, but can't find anything online that suits my style of tree. Any help? I only need to delete the first node. Also, what could I improve in my current code?
```
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace RedBlackTree
{
//From Wiki:
//The performance of an AA tree is equivalent to the performance of a red-black tree.
//While an AA tree makes more rotations than a red-black tree, the simpler algorithms tend to be faster, and all of this balances out to result in similar performance.
//A red-black tree is more consistent in its performance than an AA tree, but an AA tree tends to be flatter, which results in slightly faster search times.
class RedBlackTree
{
private const bool red = false;
private const bool black = true;
private rbtNode root;
private int _count = 0;
public int count { get { return _count; } }
//public bool Contains(PNode Key)
//{
// Node current = root;
// while (current != null)
// {
// if (current.Key == Key)
// {
// return true;
// }
// else if (value current.F)
{
if (current.right == null)
{
current.right = n;
break;
}
else
{
current = current.right;
}
}
}
n.parent = current;
}
_count++;
InsertCase1(n);
}
//Root node test, make it black
private void InsertCase1(rbtNode n)
{
if (n.parent == null)
{
n.colour = black;
}
Solution
Edit / Update:
I noticed that you have a misplaced curly bracket (
For some reason I don't like
I also noticed that there were a couple of
I would probably rewrite it like
Notice that I changed
your
As far as the
I noticed that you have a misplaced curly bracket (
}) that makes it so that your last class is inside of your first class, meaning that this code shouldn't compile. For some reason I don't like
Else if at the end of an if..then..else block, that might just be personal style/opinion but I figured that I would throw that out there. I also noticed that there were a couple of
If...then...else blocks that end with an Else if like this oneif (current.F == value.F)
{
return;
}
else if (value.F current.F)
{
if (current.right == null)
{
current.right = n;
break;
}
else
{
current = current.right;
}
}I would probably rewrite it like
if (current.F == value.F)
{
return;
}
else if (value.F < current.F)
{
if (n.left == null)
{
current.left = n;
break;
}
else
{
current = current.left;
}
}
else
{
if (current.right == null)
{
current.right = n;
break;
}
else
{
current = current.right;
}
}Notice that I changed
else if (value.F > current.F) to else, I didn't see how there could be another case after that, so showing that anything other then the first two cases would fall into this category.your
if...then...else blocks can really screw with an application, you can have all the right code in them but have them in totally the wrong order, I would be specific in the way that you form them and have a set pattern that you use, so that when you look back at it you can tell what is priority one, because the first one it hits is the only one it hits and then it ducks out of the if statement.As far as the
Delete() Method, add it and see if it works the way you intend it to.Code Snippets
if (current.F == value.F)
{
return;
}
else if (value.F < current.F)
{
if (n.left == null)
{
current.left = n;
break;
}
else
{
current = current.left;
}
}
else if (value.F > current.F)
{
if (current.right == null)
{
current.right = n;
break;
}
else
{
current = current.right;
}
}if (current.F == value.F)
{
return;
}
else if (value.F < current.F)
{
if (n.left == null)
{
current.left = n;
break;
}
else
{
current = current.left;
}
}
else
{
if (current.right == null)
{
current.right = n;
break;
}
else
{
current = current.right;
}
}Context
StackExchange Code Review Q#30442, answer score: 3
Revisions (0)
No revisions yet.