patterncsharpModerate
Simple Binary Search Tree
Viewed 0 times
simplebinarytreesearch
Problem
At the moment I am learning algorithms and here I am trying to implement a simple binary search tree. I would like to know your suggestions on whether I am on the right track or not, and how this can be improved.
BinaryTree.cs
Node.cs
```
class Node
{
private int number;
public Node rightLeaf;
public Node leftLeaf;
public Node(int value)
{
number = value;
rightLeaf = null;
leftLeaf = null;
}
public bool isLeaf(ref Node node)
{
return (node.rightLeaf == null && node.leftLeaf == null);
}
public void insertData(ref Node node, int data)
{
if (node == null)
{
node = new Node(data);
}
else if (node.number data)
{
insertData(ref node.leftLeaf, data);
}
}
public bool search(Node node, int s)
{
if (node == null)
return false;
if (node.number == s)
{
return true;
}
else if (node.number s)
{
return search(node.leftLeaf, s);
}
return false;
}
public void display(Node n)
{
if (n == null)
return;
display(
BinaryTree.cs
class BinaryTree
{
private Node root;
private int count;
public BinaryTree()
{
root = null;
count = 0;
}
public bool isEmpty()
{
return root == null;
}
public void insert(int d)
{
if (isEmpty())
{
root = new Node(d);
}
else
{
root.insertData(ref root, d);
}
count++;
}
public bool search(int s)
{
return root.search(root, s);
}
public bool isLeaf()
{
if(!isEmpty())
return root.isLeaf(ref root);
return true;
}
public void display()
{
if(!isEmpty())
root.display(root);
}
public int Count()
{
return count;
}
}Node.cs
```
class Node
{
private int number;
public Node rightLeaf;
public Node leftLeaf;
public Node(int value)
{
number = value;
rightLeaf = null;
leftLeaf = null;
}
public bool isLeaf(ref Node node)
{
return (node.rightLeaf == null && node.leftLeaf == null);
}
public void insertData(ref Node node, int data)
{
if (node == null)
{
node = new Node(data);
}
else if (node.number data)
{
insertData(ref node.leftLeaf, data);
}
}
public bool search(Node node, int s)
{
if (node == null)
return false;
if (node.number == s)
{
return true;
}
else if (node.number s)
{
return search(node.leftLeaf, s);
}
return false;
}
public void display(Node n)
{
if (n == null)
return;
display(
Solution
I don't quite understand why you'd use a
Why not just return the node?
Your capitalization is consistent, but slightly confusing and non-standard. For example, both your private fields and public methods are
This would be perfectly fine if you distinguished the field by using the
Or
But like I said, it's non-standard. The standard is to use PascalCase for methods, camelCase for local vars, and _underscoredCamelCase for private fields.
ref here. public void insertData(ref Node node, int data)
{
if (node == null)
{
node = new Node(data);
}
else if (node.number data)
{
insertData(ref node.leftLeaf, data);
}
}Why not just return the node?
public Node insertData(Node node, int data)
{
if (node == null)
{
return new Node(data);
}
if (node.number data)
{
return insertData(node.leftLeaf, data);
}
}Your capitalization is consistent, but slightly confusing and non-standard. For example, both your private fields and public methods are
camelCase. public void insert(int d)
{
if (isEmpty())
{
root = new Node(d);
}This would be perfectly fine if you distinguished the field by using the
this keyword or an underscore to make it look different from your methods. public void insert(int d)
{
if (isEmpty())
{
_root = new Node(d);
}Or
public void insert(int d)
{
if (isEmpty())
{
this.root = new Node(d);
}But like I said, it's non-standard. The standard is to use PascalCase for methods, camelCase for local vars, and _underscoredCamelCase for private fields.
public void Insert(int d)
{
if (IsEmpty())
{
_root = new Node(d);
}Code Snippets
public void insertData(ref Node node, int data)
{
if (node == null)
{
node = new Node(data);
}
else if (node.number < data)
{
insertData(ref node.rightLeaf, data);
}
else if (node.number > data)
{
insertData(ref node.leftLeaf, data);
}
}public Node insertData(Node node, int data)
{
if (node == null)
{
return new Node(data);
}
if (node.number < data)
{
return insertData(node.rightLeaf, data);
}
if (node.number > data)
{
return insertData(node.leftLeaf, data);
}
}public void insert(int d)
{
if (isEmpty())
{
root = new Node(d);
}public void insert(int d)
{
if (isEmpty())
{
_root = new Node(d);
}public void insert(int d)
{
if (isEmpty())
{
this.root = new Node(d);
}Context
StackExchange Code Review Q#92618, answer score: 16
Revisions (0)
No revisions yet.