Hi, I have only been using C# for a short time now and am learning about binary trees and trying to develop one for an application.
The one that I have seems to work fine except for the moveTo method. I get an error message for each of the switch cases. The message is "The name leftChild does not exist in the current context" I get the same message for
rightChild, parent, and rootNode.
Does anyone have any ideas as to why I am getting this message and how I might fix it?
The code is as follows:
using System;<br/>
using System.Collections;<br/>
using System.Collections.Generic;<br/>
using System.Collections.ObjectModel;<br/>
using System.Linq;<br/>
using System.Text;<br/>
<br/>
namespace _CSharpTestingGrounds<br/>
{<br/>
public class BinaryTree<T><br/>
{<br/>
<br/>
private Node<T> root;<br/>
private Node<T> current;<br/>
private int size;<br/>
<br/>
public enum Relative<br/>
{ leftChild = 0, rightChild = 1, parent = 2, rootNode = 3 };<br/>
<br/>
public BinaryTree()<br/>
{<br/>
root = null;<br/>
current = null;<br/>
size = 0;<br/>
}<br/>
<br/>
public void Destroy(Node<T> n)<br/>
{<br/>
if (n != null)<br/>
{<br/>
Destroy(n.getLeft());<br/>
Destroy(n.getRight());<br/>
n = null;<br/>
size--;<br/>
}<br/>
}<br/>
<br/>
public bool isEmpty()<br/>
{<br/>
return root == null;<br/>
}<br/>
<br/>
public bool isLeaf()<br/>
{<br/>
bool isLeaf = false;<br/>
<br/>
isLeaf = (current.getLeft() == null && current.getRight() == null);<br/>
<br/>
return isLeaf;<br/>
}<br/>
<br/>
public int getSize()<br/>
{<br/>
return size;<br/>
}<br/>
<br/>
public T getCurrent()<br/>
{<br/>
return current.getElement();<br/>
}<br/>
<br/>
private Node<T> findParent(Node<T> n)<br/>
{<br/>
Stack<Node<T>> s = new Stack<Node<T>>();<br/>
n = root;<br/>
while (n.getLeft() != current && n.getRight() != current)<br/>
{<br/>
if (n.getRight() != null)<br/>
s.Push(n.getRight());<br/>
<br/>
if (n.getLeft() != null)<br/>
n = n.getLeft();<br/>
else<br/>
n = s.Pop();<br/>
}<br/>
s.Clear();<br/>
return n;<br/>
}<br/>
<br/>
public bool moveTo(Relative rel)<br/>
{<br/>
bool found = false;<br/>
<br/>
switch (rel)<br/>
{<br/>
case leftChild:<br/>
if (current.getLeft() != null)<br/>
{<br/>
current = current.getLeft();<br/>
found = true;<br/>
}<br/>
break;<br/>
case rightChild:<br/>
if (current.getRight() != null)<br/>
{<br/>
current = current.getRight();<br/>
found = true;<br/>
}<br/>
break;<br/>
case parent:<br/>
if (current != root)<br/>
{<br/>
current = findParent(current);<br/>
found = true;<br/>
}<br/>
break;<br/>
case rootNode:<br/>
if (root != null)<br/>
{<br/>
current = root;<br/>
found = true;<br/>
}<br/>
break;<br/>
} // end Switch relative<br/>
<br/>
return found;<br/>
}<br/>
<br/>
public Node<T> getRoot()<br/>
{<br/>
return root;<br/>
}<br/>
<br/>
public void Update(T el)<br/>
{<br/>
current.setElement(el);<br/>
}<br/>
<br/>
public bool insert(T value, Relative rel)<br/>
{<br/>
bool inserted = true;<br/>
<br/>
Node<T> node = null;<br/>
<br/>
if ((rel == BinaryTree<T>.Relative.leftChild && current.getLeft() != null)<br/>
|| (rel == BinaryTree<T>.Relative.rightChild && current.getRight() != null))<br/>
{<br/>
inserted = false;<br/>
}<br/>
else<br/>
{<br/>
<br/>
node = new Node<T>(value);<br/>
<br/>
switch (rel)<br/>
{<br/>
case leftChild:<br/>
current.setLeft(node);<br/>
break;<br/>
case rightChild:<br/>
current.setRight(node);<br/>
break;<br/>
case rootNode:<br/>
if (root == null)<br/>
root = node;<br/>
current = root;<br/>
break;<br/>
}<br/>
}<br/>
<br/>
size++;<br/>
<br/>
return inserted;<br/>
}<br/>
<br/>
public void inOrder(Node<T> p)<br/>
{<br/>
if (p != null)<br/>
{<br/>
inOrder(p.getLeft());<br/>
Console.Write(p.getElement().ToString());<br/>
inOrder(p.getRight());<br/>
}<br/>
}<br/>
<br/>
public void preOrder(Node<T> p)<br/>
{<br/>
if (p != null)<br/>
{<br/>
Console.Write(p.getElement().ToString());<br/>
preOrder(p.getLeft());<br/>
preOrder(p.getRight());<br/>
}<br/>
}<br/>
<br/>
public void postOrder(Node<T> p)<br/>
{<br/>
if (p != null)<br/>
{<br/>
postOrder(p.getLeft());<br/>
postOrder(p.getRight());<br/>
Console.Write(p.getElement().ToString());<br/>
}<br/>
}<br/>
<br/>
}<br/>
}<br/>
View the full article
The one that I have seems to work fine except for the moveTo method. I get an error message for each of the switch cases. The message is "The name leftChild does not exist in the current context" I get the same message for
rightChild, parent, and rootNode.
Does anyone have any ideas as to why I am getting this message and how I might fix it?
The code is as follows:
using System;<br/>
using System.Collections;<br/>
using System.Collections.Generic;<br/>
using System.Collections.ObjectModel;<br/>
using System.Linq;<br/>
using System.Text;<br/>
<br/>
namespace _CSharpTestingGrounds<br/>
{<br/>
public class BinaryTree<T><br/>
{<br/>
<br/>
private Node<T> root;<br/>
private Node<T> current;<br/>
private int size;<br/>
<br/>
public enum Relative<br/>
{ leftChild = 0, rightChild = 1, parent = 2, rootNode = 3 };<br/>
<br/>
public BinaryTree()<br/>
{<br/>
root = null;<br/>
current = null;<br/>
size = 0;<br/>
}<br/>
<br/>
public void Destroy(Node<T> n)<br/>
{<br/>
if (n != null)<br/>
{<br/>
Destroy(n.getLeft());<br/>
Destroy(n.getRight());<br/>
n = null;<br/>
size--;<br/>
}<br/>
}<br/>
<br/>
public bool isEmpty()<br/>
{<br/>
return root == null;<br/>
}<br/>
<br/>
public bool isLeaf()<br/>
{<br/>
bool isLeaf = false;<br/>
<br/>
isLeaf = (current.getLeft() == null && current.getRight() == null);<br/>
<br/>
return isLeaf;<br/>
}<br/>
<br/>
public int getSize()<br/>
{<br/>
return size;<br/>
}<br/>
<br/>
public T getCurrent()<br/>
{<br/>
return current.getElement();<br/>
}<br/>
<br/>
private Node<T> findParent(Node<T> n)<br/>
{<br/>
Stack<Node<T>> s = new Stack<Node<T>>();<br/>
n = root;<br/>
while (n.getLeft() != current && n.getRight() != current)<br/>
{<br/>
if (n.getRight() != null)<br/>
s.Push(n.getRight());<br/>
<br/>
if (n.getLeft() != null)<br/>
n = n.getLeft();<br/>
else<br/>
n = s.Pop();<br/>
}<br/>
s.Clear();<br/>
return n;<br/>
}<br/>
<br/>
public bool moveTo(Relative rel)<br/>
{<br/>
bool found = false;<br/>
<br/>
switch (rel)<br/>
{<br/>
case leftChild:<br/>
if (current.getLeft() != null)<br/>
{<br/>
current = current.getLeft();<br/>
found = true;<br/>
}<br/>
break;<br/>
case rightChild:<br/>
if (current.getRight() != null)<br/>
{<br/>
current = current.getRight();<br/>
found = true;<br/>
}<br/>
break;<br/>
case parent:<br/>
if (current != root)<br/>
{<br/>
current = findParent(current);<br/>
found = true;<br/>
}<br/>
break;<br/>
case rootNode:<br/>
if (root != null)<br/>
{<br/>
current = root;<br/>
found = true;<br/>
}<br/>
break;<br/>
} // end Switch relative<br/>
<br/>
return found;<br/>
}<br/>
<br/>
public Node<T> getRoot()<br/>
{<br/>
return root;<br/>
}<br/>
<br/>
public void Update(T el)<br/>
{<br/>
current.setElement(el);<br/>
}<br/>
<br/>
public bool insert(T value, Relative rel)<br/>
{<br/>
bool inserted = true;<br/>
<br/>
Node<T> node = null;<br/>
<br/>
if ((rel == BinaryTree<T>.Relative.leftChild && current.getLeft() != null)<br/>
|| (rel == BinaryTree<T>.Relative.rightChild && current.getRight() != null))<br/>
{<br/>
inserted = false;<br/>
}<br/>
else<br/>
{<br/>
<br/>
node = new Node<T>(value);<br/>
<br/>
switch (rel)<br/>
{<br/>
case leftChild:<br/>
current.setLeft(node);<br/>
break;<br/>
case rightChild:<br/>
current.setRight(node);<br/>
break;<br/>
case rootNode:<br/>
if (root == null)<br/>
root = node;<br/>
current = root;<br/>
break;<br/>
}<br/>
}<br/>
<br/>
size++;<br/>
<br/>
return inserted;<br/>
}<br/>
<br/>
public void inOrder(Node<T> p)<br/>
{<br/>
if (p != null)<br/>
{<br/>
inOrder(p.getLeft());<br/>
Console.Write(p.getElement().ToString());<br/>
inOrder(p.getRight());<br/>
}<br/>
}<br/>
<br/>
public void preOrder(Node<T> p)<br/>
{<br/>
if (p != null)<br/>
{<br/>
Console.Write(p.getElement().ToString());<br/>
preOrder(p.getLeft());<br/>
preOrder(p.getRight());<br/>
}<br/>
}<br/>
<br/>
public void postOrder(Node<T> p)<br/>
{<br/>
if (p != null)<br/>
{<br/>
postOrder(p.getLeft());<br/>
postOrder(p.getRight());<br/>
Console.Write(p.getElement().ToString());<br/>
}<br/>
}<br/>
<br/>
}<br/>
}<br/>
View the full article