ref not working - Does not change property - c#

I am using ref so that it changes the root of the Binary Search Tree I am trying to create, however, it's not working the way I intend for it to.
public BinaryNode<T> Root { get; set; }
public BinaryTree() : base()
{
Root = null;
public T Insert(ref BinaryNode<T> root, T val)
{
// Insert here
if (root == null)
{
BinaryNode<T> newNode = new BinaryNode<T>(val);
root = newNode;
Size++;
return val;
}
if (val.CompareTo(root.Data) < 0)
{
BinaryNode<T> left = root.LeftChild;
return Insert(ref left, val);
}
else if (val.CompareTo(root.Data) > 0)
{
BinaryNode<T> right = root.RightChild;
return Insert(ref right, val);
}
return val;
}
public override T Insert(T val)
{
BinaryNode<T> root = Root;
return Insert(ref root, val);
}
I was expecting that when I do root = newNode that for example Root would change during the first insert. However, this is not the case. Root stays null even after. I am suspecting that this is something more related to properties and how it interacts with ref instead of ref itself?

You are modifying the local root variable since that's the reference you are passing in. If you want this to work you need to then assign it back to the Root property like so:
public T Insert(T val)
{
BinaryNode<T> root = Root;
var result = Insert(ref root, val);
Root = root;
return result;
}
Perhaps a cleaner option would be to directly use a backing field for the property like so:
BinaryNode<T> _root;
public BinaryNode<T> Root
{
get { return _root; }
set { _root = value; }
}
public T Insert(T val)
{
return Insert(ref _root, val);
}

Related

Why do we need Parent Node in Binary Search Tree - C#

I have only started learning Data Structures so please bear my stupidity, I am trying to develop my own version of BST, I can't get why there is a need of a parent Node? Shouldn't this work just fine.
class BST
{
private Node root;
public BST()
{
root = null;
}
public void insert(int value)
{
Node temp = new Node();
temp.value = value;
if (root == null)
{
root = temp;
return;
}
Node current = root;
while (current != null)
{
if (value <= current.value)
{
current = current.lc;
}
else
{
current = current.rc;
}
}
current = temp;
}
}
class Node
{
public Node lc;
public int value;
public Node rc;
}
There is definitely something that I am missing and I can't grasp or get what it is, when current is null, we are already onto where we need to insert the node, why then do we need a parent node.
This may work
class BST
{
private Node root = null;
public void insert(int value)
{
Node temp = new Node { value = value };
if (root == null)
{
root = temp;
return;
}
var current = root;
while (current != null)
{
if (value <= current.value)
{
if (current.lc == null)
{
current.lc = temp;
break;
}
current = current.lc;
}
else
{
if (current.rc == null)
{
current.rc = temp;
break;
}
current = current.rc;
}
}
}
}
class Node
{
public Node lc;
public int value;
public Node rc;
}
You are mixing variables with references to fields/variables. The current variable holds the value of the lc or rc field (the copy of the field). Setting the variable does not set the corresponding field, just assigns another value to the variable.
Hence the line
current = temp;
does not insert the node in the BST.
What you are trying to do is possible with C#7.0 introduced ref locals and returns and C#7.3 introduced improvements which allow to reassign ref local variables.
The ref local variables are exactly what is your intention - they contain the location (reference, address) of some other field / variable. So the following works (requires C#7.3!):
public void insert(int value)
{
ref Node nodeRef = ref root;
while (nodeRef != null)
{
if (value <= nodeRef.value)
nodeRef = ref nodeRef.lc;
else
nodeRef = ref nodeRef.rc;
}
nodeRef = new Node { value = value };
}
Note the usage of ref keyword. You use nodeRef = ref … to assign a reference (address) of a variable (in this case either root or some node lc or rc field), and nodeRef = … to assign a value to the variable pointed by the nodeRef.
You are setting "null" to some instance.
In your while loop current eventually becomes null, and you are missing the connection between nodes.
To fix this issue you should keep the last node of your tree.
You can try the below :
class BST
{
private Node root;
public BST()
{
root = null;
}
public void insert(int value)
{
root = insert(root, value);
}
private Node insert(Node node, int value) {
// if the given node is null it should be new node
if (node == null) {
node = new Node();
node.value = value;
return node;
}
if (value < node.value)
// if our value lower then current value then we will insert left node recursively
node.lc = insert(node.lc, value);
else if (value > node.value)
// if our value higher then current value then we will insert right node recursively
node.rc = insert(node.rc, value);
return node;
}
public void print() {
print(root);
}
private void print(Node node) {
if (node != null) {
print(node.lc);
Console.WriteLine(node.value);
print(node.rc);
}
return;
}
}
public static void main(String[] args) {
BST bst = new BST();
bst.insert(5);
bst.insert(25);
bst.insert(15);
bst.insert(4);
bst.print();
}
The output is :
4
5
15
25

C# - Binary Search Tree Contains/Is Present Method

I'm really struggling to get this method to work, I was wondering if you could help me out. I've been using the ref keyword, so I'll keep using it. I've been searching the web and it's been some help, but I've tried everything I can possibly think of. Both my count and height methods work, however, I'm just really struggling to get this Contain method to work. Many examples on the web have showed both a public and private method of contains (I understand as to why) but I'm sure it could be done within one method? Surely, right? Also, please ignore the RemoveItem method, unless you wish to give me a head-start, that's at your discretion. I know it's tricky as I've looked up on it earlier during the week.
Node class-
class Node<T> where T : IComparable
{
private T data;
public Node<T> Left, Right;
public Node(T item)
{
data = item;
Left = null;
Right = null;
}
public T Data
{
set { data = value; }
get { return data; }
}
}
BinTree Class-
class BinTree<T> where T : IComparable
{
protected Node<T> root;
public BinTree() //creates an empty tree
{
root = null;
}
public BinTree(Node<T> node) //creates a tree with node as the root
{
root = node;
}
//I've deleted my preOrder, inOrder and postOrder methods just to save you some time
}
BSTree Class-
class BSTree<T> : BinTree<T> where T : IComparable
{
public BSTree()
{
root = null;
}
private void insertItem(T item, ref Node<T> tree)
{
if (tree == null)
{
tree = new Node<T>(item);
}
else if (item.CompareTo(tree.Data) < 0)
{
insertItem(item, ref tree.Left);
}
else if (item.CompareTo(tree.Data) > 0)
insertItem(item, ref tree.Right);
}
public void InsertItem(T item)
{
insertItem(item, ref root);
}
public int Height(ref Node<T> tree)
//Return the max level of the tree
{
if (tree == null)
return 0;
return (1 + Math.Max(Height(ref tree.Left), Height(ref tree.Right)));
}
public int Count(ref Node<T> tree)
//Return the number of nodes in the tree
{
int counter = 0;
if (tree == null)
{
return 0;
}
else if (tree.Left != null)
{
counter += Count(ref tree.Left);
counter++;
}
if (tree.Right != null)
{
counter += Count(ref tree.Right);
counter++;
}
return counter;
}
public Boolean Contains(T item, ref Node<T> tree)
//Return true if the item is contained in the BSTree, false //otherwise.
{
if (tree == null)
{
return false;
}
if (item.CompareTo(tree.Data) < 0)
{
return Contains(ref tree.Left);
if (item.CompareTo(tree.Data) > 0)
{
return Contains(ref tree.Right);
return true;
}
}
}
public void RemoveItem(T item)
{
}
}
Thank you in advance.
To check if a node is in the tree, you have a few options:
The node you want is in the left sub-tree
The node you want is in the right sub-tree
The node you want is where you are right now
So your Contains method should look more like this:
public Boolean Contains(T item, ref Node<T> tree)
{
if (tree == null)
{
return false;
}
if (tree.data == item)
{
return true;
}
if (item.CompareTo(tree.Data) < 0)
{
return Contains(item, ref tree.Left);
}
if (item.CompareTo(tree.Data) > 0)
{
return Contains(item, ref tree.Right);
}
}
public static bool Contains(Node root, int value)
{
if(root == null)
return false;
if(root.Value == value)
return true;
if(value < root.Value)
return Contains( root.Left,value);
else
return Contains( root.Right,value);
}

How to properly return an object that is equal to null? (simplified bst)

I've tried to implement a binary-search-tree(BST) insert using the following method but i really don't get why i get the value instead of the actual "reference" to the node.
I've done other implementations that worked fine but I really want to understand why instead of getting the pointer to let's say node.m_left I get just it's value (null).
How can I get that without resorting to unsafe code ?
The reason i'm choosing this way is because searching and inserting are closely related and i don't want to implement the same thing twice.
public void Add(int value)
{
if (root == null)
{
root = new Node(value);
return;
}
Node parent = null ,
res;
//finding the correct place
res = Find(value, root, ref parent);
if (res == null) //probably redunant
{
//once found create a node and asign it's parent
res = new Node(value);
res.m_parent = parent;
}
//EvaluateAVT(res);
}
private Node Find(int value, Node node, ref Node parent,bool justfind = false)
{
if (node.Data == value && justfind)
{
return node;
}
if (node.Data >= value)
{
if (node.m_left == null)
{
parent = node;
return node.m_left;
}
return Find(value,node.m_left,ref parent ,justfind);
}
else
{
if (node.m_right == null)
{
parent = node;
return node.m_right;
}
return Find(value, node.m_right, ref parent, justfind);
}
}

how to find a NodeData in tree of Nodes?

I have a Node structure as below,this Node has stringdata and list of nodes as it's children. I wanted to search a data in this tree
I wrote a recursive function FindNode(Node intree,string target)
public class Node
{
public string Data;
public List<Node> Children = new List<Node>();
//some code
public Node(string r)
{
this.Data = r;
}
public string getData()
{
return this.Data;
}
//some code
public List<Node> getchildren()
{
return this.Children;
}
//some code
}
target is the string which I want to find and the intree is the begining of the tree(ROOT)
I have problem after while loop what should I return after that?
If I am wrong how should I write it?
public Node FindNode(Node intree,string target)
{
if(intree.getData()==target)
return intree;
else
{
while(intree.getchildren()!=null)
{
foreach(Node n in intree.getchildren())
{
FindNode(n,target);
}
}
}
}
Use this one:
public Node FindNode(Node intree,string target)
{
if(intree.getData()==target)
return intree;
else
{
foreach(Node n in intree.getchildren())
{
Node node = FindNode(n,target) ; //CHECK FOR RETURN
if(node != null)
return node;
}
}
return null;
}
The difference is that I checked for return of FindNode method, and if it's not null, return result.
Just note that in case of dupplicated nodes in the tree (nodes with the same string) it will return first occuarance.
Given that there could be more than one match in the tree you would be better off returning an IEnumerable<Node>. Also you don't need to put those odd get methods in there. And finally did you mean to only search leaf nodes or did you want to search all nodes (the else statement)?
public IEnumerable<Node> FindNode(Node intree,string target)
{
if(intree.Data ==target)
yield return intree;
foreach (var node in intree.Children.SelectMany(c => FindNode(c, target))
yield return node;
}
If you want the first matching node, just call First() on the result. If you want to make sure there is only one, call Single() on it.
I would recommend you to return null and apply check where you are calling this method that if null is returned then it means no node found. Code is as follow
public static Node FindNode(Node intree, string target)
{
if (intree.getData() == target)
return intree;
foreach (Node node in intree.getchildren())
{
Node toReturn = FindNode(node, target);
if (toReturn != null) return toReturn;
}
return null;
}
public Node FindNodeRecursively(Node parentNode, string keyword)
{
if(parentNode.getData() == keyword)
{
return parentNode;
}
else
{
if(parentNode.Children != null)
{
foreach(var node in parentNode.Children)
{
var temp = FindNodeRecursively(node, keyword);
if(temp != null)
return temp;
}
}
return null;
}
}

What is the reason behind this huge Performance difference in .Net 4

I was just doing some research on RedBlack Tree. I knew that SortedSet class in .Net 4.0 uses RedBlack tree. So I took that part out as is using Reflector and created a RedBlackTree class. Now I am running some perf test on this RedBlackTree and SortedSet inserting 40000 sequential integral values (starting from 0 to 39999), I am astonished to see that there is huge perf difference as follows:
RBTree took 9.27208 sec to insert 40000 values
SortedSet took 0.0253097 sec to insert 40000 values
What is the reason behind it? BTW I ran the test in Release configuration only and here is the small test code
var stopWatch = new Stopwatch();
var rbT = new RedBlackTree<int>();
stopWatch = new Stopwatch();
stopWatch.Start();
for (int i = 0; i < 40000; i++) {
rbT.Add(i);
}
stopWatch.Stop();
Console.WriteLine(stopWatch.Elapsed);
var ss = new SortedSet<int>();
stopWatch = new Stopwatch();
stopWatch.Start();
for (int i = 0; i < 40000; i++) {
ss.Add(i);
}
stopWatch.Stop();
Console.WriteLine(stopWatch.Elapsed);
Edit
I would like to share the code also for RBTree what I've extracted so that you also can run the diagnostics
public class Node<T>
{
public Node(){}
public Node(T value)
{
Item = value;
}
public Node(T value, bool isRed)
{
Item = value;
IsRed = isRed;
}
public T Item;
public Node<T> Left;
public Node<T> Right;
public Node<T> Parent;
public bool IsRed;
}
public class RedBlackTree<T>
{
public RedBlackTree(){}
public Node<T> root;
int count, version;
Comparer<T> comparer = Comparer<T>.Default;
public void Add(T item)
{
if (this.root == null)
{
this.root = new Node<T>(item, false);
this.count = 1;
this.version++;
return;
}
Node<T> root = this.root;
Node<T> node = null;
Node<T> grandParent = null;
Node<T> greatGrandParent = null;
this.version++;
int num = 0;
while (root != null)
{
num = this.comparer.Compare(item, root.Item);
if (num == 0)
{
this.root.IsRed = false;
return;
}
if (Is4Node(root))
{
Split4Node(root);
if (IsRed(node))
{
this.InsertionBalance(root, ref node, grandParent, greatGrandParent);
}
}
greatGrandParent = grandParent;
grandParent = node;
node = root;
root = (num < 0) ? root.Left : root.Right;
}
Node<T> current = new Node<T>(item);
if (num > 0)
{
node.Right = current;
}
else
{
node.Left = current;
}
if (node.IsRed)
{
this.InsertionBalance(current, ref node, grandParent, greatGrandParent);
}
this.root.IsRed = false;
this.count++;
}
private static bool IsRed(Node<T> node)
{
return ((node != null) && node.IsRed);
}
private static bool Is4Node(Node<T> node)
{
return (IsRed(node.Left) && IsRed(node.Right));
}
private static void Split4Node(Node<T> node)
{
node.IsRed = true;
node.Left.IsRed = false;
node.Right.IsRed = false;
}
private void InsertionBalance(Node<T> current, ref Node<T> parent, Node<T> grandParent, Node<T> greatGrandParent)
{
Node<T> node;
bool flag = grandParent.Right == parent;
bool flag2 = parent.Right == current;
if (flag == flag2)
{
node = flag2 ? RotateLeft(grandParent) : RotateRight(grandParent);
}
else
{
node = flag2 ? RotateLeftRight(grandParent) : RotateRightLeft(grandParent);
parent = greatGrandParent;
}
grandParent.IsRed = true;
node.IsRed = false;
ReplaceChildOfNodeOrRoot(greatGrandParent, grandParent, node);
}
private static Node<T> RotateLeft(Node<T> node)
{
Node<T> right = node.Right;
node.Right = right.Left;
right.Left = node;
return right;
}
private static Node<T> RotateRight(Node<T> node)
{
Node<T> left = node.Left;
node.Left = left.Right;
left.Right = node;
return left;
}
private static Node<T> RotateLeftRight(Node<T> node)
{
Node<T> left = node.Left;
Node<T> right = left.Right;
node.Left = right.Right;
right.Right = node;
left.Right = right.Left;
right.Left = left;
return right;
}
private static Node<T> RotateRightLeft(Node<T> node)
{
Node<T> right = node.Right;
Node<T> left = right.Left;
node.Right = left.Left;
left.Left = node;
right.Left = left.Right;
left.Right = right;
return left;
}
private void ReplaceChildOfNodeOrRoot(Node<T> parent, Node<T> child, Node<T> newChild)
{
if (parent != null)
{
if (parent.Left == child)
{
parent.Left = newChild;
}
else
{
parent.Right = newChild;
}
}
else
{
this.root = newChild;
}
}
}
Edit
I ran the same diagnostic on some other data structure (some created by me*, some from .net framework**) and here is the interesting results
*AATree 00:00:00.0309294
*AVLTree 00:00:00.0129743
**SortedDictionary 00:00:00.0313571
*RBTree 00:00:09.2414156
**SortedSet 00:00:00.0241973
RBTree is the same as above (stripped out from SortedSet class). I tried with 400000 values also, but RBTree seems taking FOREVER, I really don't know why.
You have a bug in your Node<T> class. When you call the constructor that only takes a single value argument you should be setting IsRed to true.
I suppose that the fixed Node<T> class should look something like this:
public sealed class Node<T>
{
public T Item { get; private set; }
public bool IsRed { get; set; }
public Node<T> Left { get; set; }
public Node<T> Right { get; set; }
public Node(T value)
{
Item = value;
IsRed = true;
}
public Node(T value, bool isRed)
{
Item = value;
IsRed = isRed;
}
}
Another option -- my preference -- would be to omit that constructor altogether and always require IsRed to be set explicitly when you instantiate a new node:
public sealed class Node<T>
{
public T Item { get; private set; }
public bool IsRed { get; set; }
public Node<T> Left { get; set; }
public Node<T> Right { get; set; }
public Node(T value, bool isRed)
{
Item = value;
IsRed = isRed;
}
}
And then replace this line in your Add method...
Node<T> current = new Node<T>(item);
...with this...
Node<T> current = new Node<T>(item, true);
reverse the order of the tests and repeat the measurement.
randomize your data. Sorted sets behave strangely when you insert pre-sorted data.
SortedSet includes a TargetedPatchingOptOut attribute, did your copied version include that?
[TargetedPatchingOptOut("Performance critical to inline this type of method across NGen image boundaries")]
public bool Add(T item)
{
return this.AddIfNotPresent(item);
}
If the difference wasn't that big I would suggest that the cause is that the .NET assemblies are NGen-ed and so they are already translated to native code. In the case of your class the time to compile the IL code into native code is amortized over the time of your test. How does increasing the number of loop iterations affect the times?

Categories