C# - Binary Search Tree Contains/Is Present Method

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.





This is too much information. Please boil this down to a Minimal, Complete, and Verifiable example, and define specifically what it is you'd like us to help you with.
– rory.ap
Nov 30 '16 at 21:41





Tree searches usually requires a recursive method or a push/pop method.
– jdweng
Nov 30 '16 at 21:41





There are three possibilities for the result of CompareTo: < 0 , > 0 or = 0. You need to check all three to decide whether to stop the search successfully (on 0) or to search the left or right subtrees. At the moment your test for item > tree root is within the test for item < tree root which it should not be.
– Lee
Nov 30 '16 at 21:43



CompareTo: < 0 , > 0 or = 0


item > tree root


item < tree root





Is your code compiling? If it is, what behavior do you get?
– Kelson Ball
Nov 30 '16 at 21:44





your second if block will never get called because its contained in an if block that precludes it from being true.
– Lucas Kot-Zaniewski
Nov 30 '16 at 21:44





2 Answers
2



To check if a node is in the tree, you have a few options:



So your Contains method should look more like this:


Contains


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);






Item probably needs passed to the recursive calls. That's why I asked OP if his code was compiling :/
– Kelson Ball
Nov 30 '16 at 21:53





Good call @KelsonBall. I have edited the post to reflect these changes.
– Libby
Nov 30 '16 at 21:55





@KelsonBall Sorry for the late reply, I have answered your question.
– JavaScriptGrasshopper
Nov 30 '16 at 21:59





@Libby Thank you for your answer, however, I have stumbled across an error "Operator '==' cannot be applied to operands of type 'T' and 'T'.
– JavaScriptGrasshopper
Nov 30 '16 at 22:01





I upvoted but what's the point of the last return false and the second if statement? why not just add an else to the last if block?
– Lucas Kot-Zaniewski
Nov 30 '16 at 22:01


return false


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);





add description and say how you solution help to solve the problem :)
– janith1024
Aug 25 at 2:59






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

𛂒𛀶,𛀽𛀑𛂀𛃧𛂓𛀙𛃆𛃑𛃷𛂟𛁡𛀢𛀟𛁤𛂽𛁕𛁪𛂟𛂯,𛁞𛂧𛀴𛁄𛁠𛁼𛂿𛀤 𛂘,𛁺𛂾𛃭𛃭𛃵𛀺,𛂣𛃍𛂖𛃶 𛀸𛃀𛂖𛁶𛁏𛁚 𛂢𛂞 𛁰𛂆𛀔,𛁸𛀽𛁓𛃋𛂇𛃧𛀧𛃣𛂐𛃇,𛂂𛃻𛃲𛁬𛃞𛀧𛃃𛀅 𛂭𛁠𛁡𛃇𛀷𛃓𛁥,𛁙𛁘𛁞𛃸𛁸𛃣𛁜,𛂛,𛃿,𛁯𛂘𛂌𛃛𛁱𛃌𛂈𛂇 𛁊𛃲,𛀕𛃴𛀜 𛀶𛂆𛀶𛃟𛂉𛀣,𛂐𛁞𛁾 𛁷𛂑𛁳𛂯𛀬𛃅,𛃶𛁼

Edmonton

Crossroads (UK TV series)