binary search and inorder traversal in Java - java

i'm studying a Binary Search Tree.
I use method inOrder kind of traversals but in main method i get an error. The question is why? What do i miss?
Thank you
inOrder(S11.Tree<java.lang.Integer>.Node<java.lang.Integer>)
in Tree cannot be applied to(S11.Node<java.lang.Integer>)
public class Tree<T extends Comparable<T>> {
private Node<T> root;
public void add(T data) {
root = doInsert(root, data);
}
public Node<T> doInsert(Node<T> root, T data) {
if (root == null) {
return new Node<T>(data);
} else if (data.compareTo(root.data) > 0) {
root.right = doInsert(root.right, data);
} else if (data.compareTo(root.data) < 0) {
root.left = doInsert(root.left, data);
}
return root;
}
public void inOrder(Node<T> root) {
if (root != null) {
inOrder(root.left);
System.out.print(root.data + " ");
inOrder(root.right);
}
}
class Node<T> {
Node<T> left;
Node<T> right;
T data;
public Node(T data) {
this.data = data;
}
}
}
public static void main(String[] args) {
Tree<Integer> tree = new Tree<Integer>();
Node<Integer> root1 = new Node<>(8);
tree.add(3);
tree.add(10);
tree.add(1);
tree.add(6);
tree.add(14);
tree.add(4);
tree.add(7);
tree.add(13);
tree.add(18);
tree.inOrder(**root1)**;//here I get an error
}
}

You have a design problem here as well as incorrect implementation of add(T data) method.
public final class Tree<T extends Comparable<T>> {
private Node<T> root;
private int size;
public void add(T val) {
Node<T> node = new Node<>(val);
if (root == null)
root = node;
else
add(root, node);
size++;
}
private void add(Node<T> parent, Node<T> node) {
if (node.val.compareTo(parent.val) < 0) {
if (parent.left == null)
parent.left = node;
else
add(parent.left, node);
} else if (parent.right == null)
parent.right = node;
else
add(parent.right, node);
}
public List<T> inOrder() {
return size == 0 ? Collections.emptyList() : Collections.unmodifiableList(inOrder(root, new ArrayList<>(size)));
}
private static <T> List<T> inOrder(Node<T> node, List<T> res) {
while (true) {
if (node == null)
return res;
inOrder(node.left, res);
res.add(node.val);
node = node.right;
}
}
// This is an internal implementation of Tree and should be hidden
// Node should not be linked with Tree instance
private static final class Node<T> {
private final T val;
private Node<T> left;
private Node<T> right;
public Node(T val) {
this.val = val;
}
}
}
Output:
Tree<Integer> tree = new Tree<>();
tree.add(8);
tree.add(3);
tree.add(10);
tree.add(1);
tree.add(6);
tree.add(14);
tree.add(4);
tree.add(7);
tree.add(13);
tree.add(18);
System.out.println(tree.inOrder()); // [1, 3, 4, 6, 7, 8, 10, 13, 14, 18]

Related

Binary tree insertion problem.....insert() only putting new Nodes in the child of root nodes

When I use insert() to inset new nodes into the binary tree it only inserts the new nodes on the place of child nodes even when root node already have left and right child. It is not visiting the child nodes to make deeper levels of binary tree.
Sorry for the bad English.
class Node
{
int key;
String value;
Node lc = null;
Node rc = null;
Node(int k,String v)
{
key = k;
value = v;
}
public String toString()
{
return value + "is" + key;
}
}
class BT
{
Node root;
public void insert(int k,String v)
{
Node newnode = new Node(k,v);
if(root == null)
{
System.out.println("root");
root = newnode;
return;
}
Node n = root;
while(n != null)
{
if(newnode.key <= n.key)
{
n = n.lc;
System.out.println("left");
if(n==null){n = newnode; break;}
}
else
{
n = n.rc;
System.out.println("right");
if(n==null){n = newnode; break;}
}
}
System.out.println("loop ended");
return;
}
}
public class test
{
public static void main(String arg[])
{
BT list = new BT();
list.insert(19,"one");
list.insert(67,"sixtyseven");
list.insert(5,"five");
list.insert(12,"twelve");
list.insert(67,"sixtyseven");
}
}
You never change the lc and rc links. Try something like this:
if(newnode.key <= n.key)
{
if(n.lc==null){n.lc = newnode; break;}
n = n.lc;
System.out.println("left");
}
else
{
if(n.rc==null){n.rc = newnode; break;}
n = n.rc;
System.out.println("right");
}
What about using recursion? It's much more clear to realise.
public final class BinaryTree {
private static final boolean ADD_TO_PARENT = true;
private static final boolean ALREADY_ADDED = false;
private Node root;
public void add(int key, String value) {
Node node = new Node(key, value);
if (add(node, root) == ADD_TO_PARENT)
root = node;
}
private static boolean add(Node node, Node parent) {
if (parent == null)
return ADD_TO_PARENT;
if (node.key <= parent.key) {
if (add(node, parent.left) == ADD_TO_PARENT)
parent.left = node;
} else if (add(node, parent.right) == ADD_TO_PARENT)
parent.right = node;
return ALREADY_ADDED;
}
public static final class Node {
private final int key;
private final String value;
private Node left;
private Node right;
public Node(int key, String value) {
this.key = key;
this.value = value;
}
#Override
public String toString() {
return value + " is " + key;
}
}
}

Binary Search Tree Deletion Issue

The program is running well in for the other method. However in my deletion, espeacially in one case that when i'm going to delete a node which has 2 children i can get to perform the transplant part but no matter what i tried i could not get rid of the right child which i just replaced with the its parents.
i have tried to set it to null and return it right away when it set to null(which it should be deleted)
the print methods are working perfectly fine but not the deletion one.
this is my output:
INSERT 4 E 3 E 6 E 2 H 1 T 5 Y 7 S
POSTORDER
THEYSEE
INORDER
THEEYES
DELETE 6 4 1 2
PREORDER
YESS
EXIT
so the problem now is why the hell the key 7 get duplicated it never get to deleted while i'm trying to move its data from 7 to 6.
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
boolean done = false;
BinarySearchTree myTree = new BinarySearchTree();
while (!done && s.hasNextLine()) {
String line = s.nextLine();
Scanner ls = new Scanner(line);
String token = ls.next();
switch (token.toUpperCase()) {
case "INSERT":
while(ls.hasNext()) {
myTree.insert(Integer.parseInt(ls.next()), ls.next());
}
continue;
case "INORDER":
myTree.inorderSearch();
System.out.println();
continue;
case "DELETE":
while(ls.hasNext()) {
myTree.deleteNode(myTree.root,Integer.parseInt(ls.next()));
}
continue;
case "PREORDER":
myTree.preorder();
System.out.println();
continue;
case "POSTORDER":
myTree.postorder();
System.out.println();
continue;
case "EXIT":
done = true;
}
}
}
}
//BINARY SEARCH TREE CLASS
class BinarySearchTree {
class Node {
int key;
String data;
Node right, left;
public Node(int item, String data) {
this.key = item;
this.data = data;
this.left = this.right = null;
}
public void setKey(int key) {
this.key = key;
}
public void setData(String data) {
this.data = data;
}
public void setRight(Node right) {
this.right = right;
}
public void setLeft(Node left) {
this.left = left;
}
public Node getRight() {
return right;
}
public Node getLeft() {
return left;
}
public int getKey() {
return key;
}
public String getData() {
return data;
}
}
Node root;
BinarySearchTree() {
this.root = null;
}
private Node insertHelper(Node root, int key, String data) {
if (root == null) {
root = new Node( key, data);
return root;
}
if (key < root.key)
root.left = insertHelper(root.left, key, data);
else if (key > root.key)
root.right = insertHelper(root.right, key, data);
return root;
}
void insert(int key, String data) {
root = insertHelper(root, key, data);
}
void inorderSearch() {
inorderSearchHelper(root);
}
private void inorderSearchHelper(Node root) {
if (root != null) {
inorderSearchHelper(root.left);
System.out.print(root.data);
inorderSearchHelper(root.right);
}
}
public void postorder(){
postorderHelper(root);
}
private void postorderHelper(Node root) {
if (root == null)
return;
postorderHelper(root.left);
postorderHelper(root.right);
System.out.print(root.data);
}
public void preorder(){
preorderHelper(root);
}
private void preorderHelper(Node node) {
if (node == null)
return;
System.out.print(node.data);
preorderHelper(node.left);
preorderHelper(node.right);
}
// DELETE FKING NODEs!
/*private void transplant(Node root,Node u, Node v){
if(u. == )
}*/
public static Node minimumElement(Node root) {
if (root.left == null)
return root;
else {
return minimumElement(root.left);
}
}
public static Node deleteNode(Node root, int key) {
if (root == null)
return null;
if (root.key > key) {
root.left = deleteNode(root.left, key);
} else if (root.key < key) {
root.right = deleteNode(root.right, key);
} else {
// if nodeToBeDeleted have both children
if (root.left != null && root.right != null) {
Node temp = root;
// Finding minimum element from right
Node minNodeForRight = minimumElement(temp.right);
// Replacing current node with minimum node from right subtree
root.data = minNodeForRight.data;
// Deleting minimum node from right now
deleteNode(root.right, minNodeForRight.key);
}
// if nodeToBeDeleted has only left child
else if (root.left != null) {
root = root.left;
}
// if nodeToBeDeleted has only right child
else if (root.right != null) {
root = root.right;
}
// if nodeToBeDeleted do not have child (Leaf node)
else
root = null;
}
return root;
}
}
so this is the right output:
INSERT 4 E 3 E 6 E 2 H 1 T 5 Y 7 S
POSTORDER
THEYSEE
INORDER
THEEYES
DELETE 6 4 1 2
PREORDER
YES
EXIT

Insert method for BST

I was able to implement a Boolean insert method for a binary search tree. It takes a generic data and inserts a key into the BST. My code does not allow duplicates, and returns false if a key is already presented in the BST. It returns true if the insertion is successful.
The problem is that, I think its not increment the number of elements present in the BST and is only able to reach 2 elements.
I have tried to debug it and see why my output and expected is off by one.
Here is my constructors and methods
public class BSTree<T extends Comparable<? super T>> implements Iterable {
private int nelems;
private BSTNode root;
private BSTNode curr;
private BSTNode keyHere;
protected class BSTNode {
T key;
LinkedList<T> dataList;
BSTNode left;
BSTNode right;
public BSTNode(BSTNode left, BSTNode right, LinkedList<T> dataList, T key) {
//TODO
this.left = left;
this.right = right;
this.key = key;
setDataList(dataList);
}
public BSTNode(BSTNode left, BSTNode right, T key) {
//TODO
this.left = left;
this.right = right;
this.key = key;
this.dataList = new LinkedList<T>();
}
public T getKey() {
//TODO
return this.key;
}
public BSTNode getLeft() {
//TODO
return this.left;
}
public BSTNode getRight() {
//TODO
return this.right;
}
public LinkedList<T> getDataList() {
//TODO
return this.dataList;
}
public void setleft(BSTNode newleft) {
//TODO
this.left = newleft;
}
public void setright(BSTNode newright) {
//TODO
this.right = newright;
}
public void setDataList(LinkedList<T> newData) {
//TODO
this.dataList = newData;
}
public void addNewInfo(T data) {
//TODO
if (data != null)
{
getDataList().addLast(data);
}
}
public boolean removeInfo(T data) {
return dataList.remove(data);
}
}
public BSTree() {
//TODO
root = null;
nelems = 0;
curr = null;
keyHere = null;
}
Here is my code for insertion
public boolean insert(T key) {
if (key == null)
{
throw new NullPointerException();
}
if (findKey(key)) // this method returns true if key is in BST
{
return false;
}
if (getSize() == 0)
{
LinkedList<T> linked = new LinkedList<T>();
BSTNode node = new BSTNode(null, null, linked,key);
root = node;
curr = root;
nelems = 1;
}
else
{
if (curr.getKey().compareTo(key) < 0)
{
if(curr.getRight() != null)
{
curr = curr.getRight();
insert(key);
}
else
{
LinkedList<T> linked = new LinkedList<T>();
BSTNode node = new BSTNode(null,null,linked,key);
curr.setright(node);
nelems += 1 ;
curr = root;
}
}
else
{
if (curr.getLeft() != null)
{
curr = curr.getLeft();
insert(key);
}
else
{
LinkedList<T> linked = new LinkedList<T>();
BSTNode node = new BSTNode(null,null,linked,key);
curr.setleft(node);
curr = root;
nelems += 1;
}
}
}
return true;
}
public boolean findKey(T key) {
if (key == null)
{
throw new NullPointerException();
}
if (getSize() == 0)
{
return false;
}
else if(curr.getKey().compareTo(key)!= 0)
{
if(curr.getKey().compareTo(key) < 0)
{
if (curr.getRight() != null)
{
curr = curr.getRight();
findKey(key);
}
else
{
this.curr = root;
return false;
}
}
else
{
if (curr.getLeft() != null)
{
curr = curr.getLeft();
findKey(key);
}
else
{
this.curr = root;
return false;
}
}
}
else
{
this.keyHere = curr;
this.curr = root;
return true;
}
return true;
}
When I would insert a value to a tree it keeps track of the first 2 elements but after adding the third one it stops increment element for some reason.
here is my testers
public class BSTreeTester{
BSTree<Integer> tree1;
public void setUp() throws Exception {
tree1 = new BSTree<Integer>();
}
public void testinsert() {
tree1.insert(new Integer(100));
assertEquals("The size of the tree should be 1", 1, tree1.getSize());
assertEquals("The root of the tree should be 100", 100,
(int)tree1.getRoot().getKey());
tree1.insert(new Integer(200));
assertEquals("The size of the tree shoul dbe 2", 2, tree1.getSize());
assertEquals("The root of the tree should still be 100", 100,
(int)tree1.getRoot().getKey());
tree1.insert(new Integer(300));
assertEquals("The size of the tree should be 3", 3, tree1.getSize());
assertEquals("The root of the tree should still be 100", 100,
(int)tree1.getRoot().getKey());
}
When inserting the 3rd new integer the expected should be 3, but its giving me 2.

Equals method in two Trees [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 4 years ago.
Improve this question
Guys i created two trees. I wrote a method but it does not work correctly.
My method does not work recursively and prints only "false".
In general, I need to recurse through the elements of a tree, and If two trees are similar, then it should output - "true". If two rees not similar, then it should output - false. Please help me write code eqauls method in my trees
My code:
public class TreePrint {
public static void main(String[] args) {
Tree<String> rootFolder = new Tree<>("RootFolder");
Node<String> video = rootFolder.addChild("Video");
Node<String> music = rootFolder.addChild("Music");
Node<String> picture = rootFolder.addChild("Picture");
video.addChild("Terminator");
video.addChild("Die Hard");
video.addChild("Rocky");
music.addChild("Eminem");
Node<String> picture01 = picture.addChild("Picasso");
picture01.addChild("Do Vinci");
Node<String> picture02 = picture01.addChild("NN");
picture02.addChild("Cartoon");
picture02.addChild("Comics");
Tree2<String> rootFolder1 = new Tree2<>("RootFolder");
printTree(rootFolder);
printTree(rootFolder1);
boolean b1 = rootFolder.contains("P0");
//System.out.println(b1);
boolean b2 = rootFolder1.contains("Eminem");
//System.out.println(b2);
System.out.println(rootFolder.equals(rootFolder1));
}
private static <T> void printTree(Node<T> node) {
printTree(node, 0);
}
private static <T> void printTree(Node<T> node, int level) {
printNode(node, level);
if (node.getChildren() != null) {
for (Node childNode : node.getChildren()) {
printTree(childNode, level + 1);
}
}
}
private static <T> void printNode(Node<T> kid, int level) {
for (int i = 0; i < level; i++) {
System.out.print(" ");
}
System.out.println(kid.getData());
}
}
public class Tree<T> extends Node<T> {
public Tree(T data) {
super(data, null);
}
public boolean contains(T value) {
return recurse(iterate(), value);
}
private boolean recurse(List<Node<T>> children, T value) {
return children.stream()
.anyMatch(item -> item.getData().equals(value) || item.iterate().size() > 0 && recurse(item.iterate(), value));
}
public boolean equals(Object obj) {
return isEquals(obj);
}
private boolean isEquals(Object obj
if (this == obj) {
return true;
}
if (obj == null) {
return false;
}
if (getClass() != obj.getClass()) {
return false;
}
Node other = (Node) obj;
if (children == null) {
if (other.children != null) {
return false;
}
} else if (!children.equals(other.children)) {
return false;
}
if (data == null) {
if (other.data != null) {
return false;
}
} else if (!data.equals(other.data)) {
return false;
}
return true;
}
}
public class Node<T> {
private T data;
private final List<Node<T>> children = new ArrayList<>();
private final Node<T> parent;
public Node(T data, Node<T> parent) {
this.data = data;
this.parent = parent;
}
public void addChild(Node<T> node) {
children.add(node);
}
public Node<T> addChild(T nodeData) {
Node<T> newNode = new Node<T>(nodeData, this);
children.add(newNode);
return newNode;
}
public List<Node<T>> iterate() {
return children;
}
public void remove(Node<T> node) {
children.remove(node);
}
public List<Node<T>> getChildren() {
return children;
}
public Node getParent() {
return parent;
}
public T getData() {
return data;
}
public void setData(T data) {
this.data = data;
}
}
public class Tree2<T> extends Node<T> {
public Tree2(T data) {
super(data, null);
}
public boolean contains(T value) {
return recurse(iterate(), value);
}
private boolean recurse(List<Node<T>> children, T value) {
return children.stream()
.anyMatch(item -> item.getData().equals(value) || item.iterate().size() > 0 && recurse(item.iterate(), value));
}
}
The problem is is this line
if (getClass() != obj.getClass()) {
return false;
}
As one object is of class Tree and other is of Tree2
I have no idea why do you need two classes Tree and Tree2 but:
TreePoint
public class TreePoint {
public static void main(String[] args) {
Tree<String> rootFolder = new Tree<>("RootFolder");
Node<String> video = rootFolder.addChild("Video");
Node<String> music = rootFolder.addChild("Music");
Node<String> picture = rootFolder.addChild("Picture");
video.addChild("Terminator");
video.addChild("Die Hard");
video.addChild("Rocky");
music.addChild("Eminem");
Node<String> picture01 = picture.addChild("Picasso");
picture01.addChild("Do Vinci");
Node<String> picture02 = picture01.addChild("NN");
picture02.addChild("Cartoon");
picture02.addChild("Comics");
Tree2<String> rootFolder1 = new Tree2<>("RootFolder");
Node<String> video1 = rootFolder1.addChild("Video");
Node<String> music1 = rootFolder1.addChild("Music");
Node<String> picture1 = rootFolder1.addChild("Picture");
video1.addChild("Terminator");
video1.addChild("Die Hard");
video1.addChild("Rocky");
music1.addChild("Eminem");
Node<String> picture011 = picture1.addChild("Picasso");
picture011.addChild("Do Vinci");
Node<String> picture021 = picture011.addChild("NN");
picture021.addChild("Cartoon");
picture021.addChild("Comics");
printTree(rootFolder);
printTree(rootFolder1);
System.out.println(rootFolder.equals(rootFolder1));
}
private static <T> void printTree(Node<T> node) {
printTree(node, 0);
}
private static <T> void printTree(Node<T> node, int level) {
printNode(node, level);
if (node.getChildren() != null) {
for (Node childNode : node.getChildren()) {
printTree(childNode, level + 1);
}
}
}
private static <T> void printNode(Node<T> kid, int level) {
for (int i = 0; i < level; i++) {
System.out.print(" ");
}
System.out.println(kid.getData());
}
}
Tree
import java.util.List;
public class Tree<T> extends Node<T> {
public Tree(T data) {
super(data, null);
}
public boolean contains(T value) {
return recurse(iterate(), value);
}
private boolean recurse(List<Node<T>> children, T value) {
return children.stream()
.anyMatch(item -> item.getData().equals(value) || item.iterate().size() > 0 && recurse(item.iterate(), value));
}
}
Node
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
public class Node<T> {
public T data;
public final List<Node<T>> children = new ArrayList<>();
public final Node<T> parent;
public Node(T data, Node<T> parent) {
this.data = data;
this.parent = parent;
}
public void addChild(Node<T> node) {
children.add(node);
}
public Node<T> addChild(T nodeData) {
Node<T> newNode = new Node<T>(nodeData, this);
children.add(newNode);
return newNode;
}
public List<Node<T>> iterate() {
return children;
}
public void remove(Node<T> node) {
children.remove(node);
}
public List<Node<T>> getChildren() {
return children;
}
public Node getParent() {
return parent;
}
public T getData() {
return data;
}
public void setData(T data) {
this.data = data;
}
#Override
public boolean equals(Object o) {
if (this == o) return true;
Node<?> node = (Node<?>) o;
return Objects.equals(data, node.data) &&
Objects.equals(children, node.children);
}
#Override
public int hashCode() {
return Objects.hash(data, children, parent);
}
}
Tree2
import java.util.List;
public class Tree2<T> extends Node<T> {
public Tree2(T data) {
super(data, null);
}
public boolean contains(T value) {
return recurse(iterate(), value);
}
private boolean recurse(List<Node<T>> children, T value) {
return children.stream()
.anyMatch(item -> item.getData().equals(value) || item.iterate().size() > 0 && recurse(item.iterate(), value));
}
}

Head of BST doesn't update after using Insertion method

I am new on JAVA and if can't understand what is wrong in my Insert method.
The head node never update and nothing is going on display.
public class BinarySearchTree {
private Node head;
/**
* This is a default constructor for the root of the binary search tree
*/
public BinarySearchTree() {
head = null;
}
public Node Insert(Node head, Node node) {
if (head == null)
head = node;
else if (node.data < head.data)
head.left = Insert(head.left, node);
else if (node.data > head.data)
head.right = Insert(head.right, node);
return head;
If I use on the constructor head = new Node(), I get the tree but node with data=0 is add to my tree.
How can I prevent that?
Thank you
EDIT:
public class Node {
int data;
Node right;
Node left;
Node parent;
/**
* Constructor for the root in binary search tree
*/
public Node() {
}
public Node(int data) {
this.data = data;
this.right = null;
this.left = null;
//this.parent = null;
}
public Node(Node obj){
if(obj != null){
this.data = obj.data;
this.left = obj.left;
this.right = obj.right;
}
}
public void setData(int data, Node right, Node left, Node parent) {
this.data = data;
this.right = right;
this.left = left;
//this.parent = parent;
}
public int getData() {
return data;
}
public class BinarySearchTree {
private Node head;
/**
* This is a default constructor for the root of the binary search tree
*/
public BinarySearchTree() {
head = new Node();
}
public Node Insert(Node head, Node node) {
if (head == null)
head = node;
else if (node.data < head.data)
head.left = Insert(head.left, node);
else if (node.data > head.data)
head.right = Insert(head.right, node);
return head;
}
////////////////////////////////////////////////////////////////////////////////
public void printInOrder(Node node)
{
if (node != null)
{
printInOrder(node.left);
System.out.print(node.data + " - ");
printInOrder(node.right);
}
}
public void printPostOrder(Node node)
{
if (node != null)
{
printPostOrder(node.left);
printPostOrder(node.right);
System.out.print(node.data + " - ");
}
}
public void printPreOrder(Node node)
{
if (node != null)
{
System.out.print(node.data + " - ");
printPreOrder(node.left);
printPreOrder(node.right);
}
}
public Node getHead(){
return head;
}
////////////////////////////////////////////////////////////////////////////////
public static void main(String[] args)
{
BinarySearchTree f = new BinarySearchTree();
/**
* Insert
*/
f.Insert(f.head, new Node(20));
f.Insert(f.head, new Node(5));
f.Insert(f.head, new Node(25));
f.Insert(f.head, new Node(3));
f.Insert(f.head, new Node(7));
f.Insert(f.head, new Node(27));
f.Insert(f.head, new Node(27));
/**
* Print
*/
f.printInOrder(f.head);
System.out.println("");
f.printPostOrder(f.head);
System.out.println("");
f.printPreOrder(f.head);
System.out.println("");
}
The problem is that your function Insert has an input named head
This means in the funtion head is not the head of your class, but the passed value. And the java does it's pass-by-value and well...
Try this instead of your Insert:
private Node recursiveInsert(Node head, Node node) {
if (head == null)
head = node;
else if (node.data < head.data)
head.left = recursiveInsert(head.left, node);
else if (node.data > head.data)
head.right = recursiveInsert(head.right, node);
return head;
}
public Node insert(Node node){
if(this.head==null){
this.head=node;
}else{
recursiveInsert(this.head,node);
}
return this.head;
}
And change the calls
f.Insert(f.head, new Node(20));
f.Insert(f.head, new Node(5));
f.Insert(f.head, new Node(25));
f.Insert(f.head, new Node(3));
f.Insert(f.head, new Node(7));
f.Insert(f.head, new Node(27));
f.Insert(f.head, new Node(27));
to
f.insert(new Node(20));
f.insert(new Node(5));
f.insert(new Node(25));
f.insert(new Node(3));
Tell me if it works ;)

Categories