patternjavaMinor
BinarySearch Tree implementation & traversals
Viewed 0 times
implementationtraversalstreebinarysearch
Problem
I am practicing various tree algorithms and that require code review for it's efficiency, clarity and also if it can be made better etc.
Is there a better way to call
TreeNode.java
```
package binarytree;
public class TreeNode
{
TreeNode left;
TreeNode right;
int data;
TreeNode parent;
int size=0;
TreeNode(int data)
{
this.data = data;
size = 1;
}
int size()
{
return size;
}
void setLeftChild(TreeNode left)
{
this.left = left;
if(left == null)
{
left.parent = this;
}
}
void setRightChild(TreeNode right)
{
this.right = right;
if(right == null)
{
right.parent = this;
}
}
void insertInOrder(int d)
{
if(d <= data)
{
if(left == null)
{
setLeftChild(new TreeNode(d));
}
else
{
left.insertInOrder(d);
}
}
else{
if(right == null)
{
setRightChild(new TreeNode(d));
}
else{
right.insertInOrder(d);
}
}
size++;
}
void postOrderTraversal(TreeNode parent)
{
if(parent == null) return;
postOrderTraversal(parent.left);
postOrderTraversal(parent.right);
printVal(parent);
}
void preOrderTraversal(TreeNode parent)
{
if(parent == null) return;
printVal(parent);
preOrderTraversal(parent.left);
preOrderTraversal(parent.right);
}
void inOrderTraversal(TreeNode parent)
{
if(parent == null) return;
inOrderTraversal(parent.left);
printVal(parent);
inOrderTraversal(parent.right);
}
void printVal(TreeNode parent)
{
System.out.print(parent.data + "\t");
}
/* binary tree find
Is there a better way to call
parent.findMin(parent)TreeNode.java
```
package binarytree;
public class TreeNode
{
TreeNode left;
TreeNode right;
int data;
TreeNode parent;
int size=0;
TreeNode(int data)
{
this.data = data;
size = 1;
}
int size()
{
return size;
}
void setLeftChild(TreeNode left)
{
this.left = left;
if(left == null)
{
left.parent = this;
}
}
void setRightChild(TreeNode right)
{
this.right = right;
if(right == null)
{
right.parent = this;
}
}
void insertInOrder(int d)
{
if(d <= data)
{
if(left == null)
{
setLeftChild(new TreeNode(d));
}
else
{
left.insertInOrder(d);
}
}
else{
if(right == null)
{
setRightChild(new TreeNode(d));
}
else{
right.insertInOrder(d);
}
}
size++;
}
void postOrderTraversal(TreeNode parent)
{
if(parent == null) return;
postOrderTraversal(parent.left);
postOrderTraversal(parent.right);
printVal(parent);
}
void preOrderTraversal(TreeNode parent)
{
if(parent == null) return;
printVal(parent);
preOrderTraversal(parent.left);
preOrderTraversal(parent.right);
}
void inOrderTraversal(TreeNode parent)
{
if(parent == null) return;
inOrderTraversal(parent.left);
printVal(parent);
inOrderTraversal(parent.right);
}
void printVal(TreeNode parent)
{
System.out.print(parent.data + "\t");
}
/* binary tree find
Solution
Here are a few things you can do to improve this:
- You don't specify visibility of your methods or variables. Presumably you want your variables to be
private, but I'm not sure which of your methods you wantpublicor not. I imagine you don't wantsetLeftChildorsetRightChildto be public since you can't enforce your ordering constraints if anyone can just change children when they feel like it.
- Both
setLeftChildandsetRightChildwill throwNullPointerExceptions when passed a null value. You want!=instead of==in your check.
- Do your nodes need references to their parents?
- In your
Traversalsclass you have nodes namedparent, but you are actually referring to the root node of your tree. I would rename that.
- You don't need to create the other three
TreeNodeclasses since you only use them to get the value that you passed into them when you callinsertInOrder. Just callinsertInOrderwith the values you want.
- You shouldn't pass in the
parentparameter to your traversal methods. These are instance methods and can just refer to the member variables directly.
- Initialize
sizeonce. Currently you set it to 0 and then 1.
- I'm assuming you have plans to use
sizelater on. Otherwise you can just remove it entirely.
Context
StackExchange Code Review Q#48719, answer score: 6
Revisions (0)
No revisions yet.