Skip to content

Commit

Permalink
Binary Search Tree
Browse files Browse the repository at this point in the history
  • Loading branch information
prateekshyap committed May 7, 2021
1 parent 8da280f commit 218e5c0
Showing 1 changed file with 22 additions and 21 deletions.
43 changes: 22 additions & 21 deletions BinarySearchTreeThreaded.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public void insert(int val)
insertIntoTree(root, val);
}

public void insertIntoTree(Node root, int val)
private void insertIntoTree(Node root, int val)
{
if (val < root.data && root.left == null)
{
Expand All @@ -46,40 +46,41 @@ public void insertIntoTree(Node root, int val)
insertIntoTree(root.right, val);
}

private Node transplant(Node parent, Node curr, Node replace)
{
if (parent.left == curr)
parent.left = replace;
else
parent.right = replace;
return parent;
}

public String delete(int val)
{
Node curr = searchNode(val);
if (curr == null) return "Node unavailable";
if (curr.left == null && curr.right == null) //leaf node
{
if (curr.parent.left == curr) curr.parent.left = null;
else curr.parent.right = null;
curr.parent = transplant(curr.parent,curr,null);
return "Success";
}
else if (curr.left == null) //single child
{
if (curr.parent.right == curr) curr.parent.right = curr.right;
else curr.parent.left = curr.right;
curr.parent = transplant(curr.parent,curr,curr.right);
return "Success";
}
else if (curr.right == null) //single child
{
if (curr.parent.right == curr) curr.parent.right = curr.right;
else curr.parent.left = curr.right;
curr.parent = transplant(curr.parent,curr,curr.left);
return "Success";
}
//double children, replacing with successor strategy
Node successorNode = searchNode(getSuccessor(curr.data));
if (successorNode.parent.left == successorNode)
successorNode.parent.left = successorNode.right;
else
successorNode.parent.right = successorNode.right;
if (curr == root) root = successorNode;
successorNode.parent = transplant(successorNode.parent,successorNode,successorNode.right);
if (curr == root)
root = successorNode;
else
{
if (curr.parent.left == curr) curr.parent.left = successorNode;
else curr.parent.right = successorNode;
}
curr.parent = transplant(curr.parent,curr,successorNode);
successorNode.left = curr.left;
successorNode.right = curr.right;
return "Success";
Expand All @@ -104,7 +105,7 @@ public int getHeight()
return getTreeHeight(root);
}

public int getTreeHeight(Node root)
private int getTreeHeight(Node root)
{
if (root == null)
return -1;
Expand All @@ -128,7 +129,7 @@ public int getNonLeafCount()
return nodeCount("nonleaf",root);
}

public int nodeCount(String type, Node root)
private int nodeCount(String type, Node root)
{
if (root == null)
return 0;
Expand All @@ -146,7 +147,7 @@ public void preOrder()
printPreOrder(root);
}

public void printPreOrder(Node root)
private void printPreOrder(Node root)
{
if (root != null)
{
Expand All @@ -161,7 +162,7 @@ public void inOrder()
printInOrder(root);
}

public void printInOrder(Node root)
private void printInOrder(Node root)
{
if (root != null)
{
Expand All @@ -176,7 +177,7 @@ public void postOrder()
printPostOrder(root);
}

public void printPostOrder(Node root)
private void printPostOrder(Node root)
{
if (root != null)
{
Expand Down

0 comments on commit 218e5c0

Please sign in to comment.