Last active
April 1, 2019 08:38
-
-
Save invisy/e47e0c93a9c0b6c114cde4072aba8a45 to your computer and use it in GitHub Desktop.
Binary Tree
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
using System.Text; | |
using System.Threading.Tasks; | |
namespace SearchTree2 | |
{ | |
public class Node | |
{ | |
public Node parent { get; set; } | |
public Node left { get; set; } | |
public Node right { get; set; } | |
public int data { get; set; } | |
} | |
public class BinaryTree | |
{ | |
private Node node = new Node(); | |
private int count = 0; | |
public void add(int item) | |
{ | |
if (count == 0) | |
{ | |
count++; | |
node.data = item; | |
node.parent = node; | |
return; | |
} | |
Node curr_node = this.node; | |
while (true) | |
{ | |
if (item < curr_node.data) | |
{ | |
if (curr_node.left == null) | |
{ | |
curr_node.left = new Node(); | |
curr_node.left.data = item; | |
curr_node.left.parent = curr_node; | |
count++; | |
break; | |
} | |
else | |
curr_node = curr_node.left; | |
} | |
else if (item > curr_node.data) | |
{ | |
if (curr_node.right == null) | |
{ | |
curr_node.right = new Node(); | |
curr_node.right.data = item; | |
curr_node.right.parent = curr_node; | |
count++; | |
break; | |
} | |
else | |
curr_node = curr_node.right; | |
} | |
else break; | |
} | |
} | |
public bool search(int item) | |
{ | |
Node curr_node = node; | |
while (true) | |
{ | |
if (curr_node == null) | |
{ | |
return false; | |
} | |
else if (curr_node.data == item) | |
{ | |
return true; | |
} | |
else if (item < curr_node.data) | |
{ | |
curr_node = curr_node.left; | |
} | |
else if (item > curr_node.data) | |
{ | |
curr_node = curr_node.right; | |
} | |
} | |
} | |
public void remove(int item) | |
{ | |
Node curr_node = node; | |
while (true) | |
{ | |
if (curr_node.data == item) | |
{ | |
bool isLeftChild = true; | |
if (Object.ReferenceEquals(curr_node, curr_node.parent.right)) | |
isLeftChild = false; | |
if (curr_node.left == null && curr_node.right == null) | |
{ | |
if (isLeftChild) | |
curr_node.parent.left = null; | |
else | |
curr_node.parent.right = null; | |
count--; | |
return; | |
} | |
else if (curr_node.left == null && curr_node.right != null) | |
{ | |
if (isLeftChild) | |
curr_node.parent.left = curr_node.right; | |
else | |
curr_node.parent.right = curr_node.right; | |
count--; | |
return; | |
} | |
else if (curr_node.left != null && curr_node.right == null) | |
{ | |
if (isLeftChild) | |
curr_node.parent.left = curr_node.left; | |
else | |
curr_node.parent.right = curr_node.left; | |
count--; | |
return; | |
} | |
else if (curr_node.left != null && curr_node.right != null) | |
{ | |
Node temp = curr_node.right; | |
while (temp.left != null) | |
{ | |
temp = temp.left; | |
} | |
curr_node.data = temp.data; | |
if (temp.right != null) | |
if (Object.ReferenceEquals(temp, temp.parent.left)) | |
temp.parent.left = temp.right; | |
else | |
temp.parent.right = temp.right; | |
else | |
if (Object.ReferenceEquals(temp, temp.parent.left)) | |
temp.parent.left = null; | |
else | |
temp.parent.right = null; | |
count--; | |
return; | |
} | |
} | |
else if (curr_node == null) | |
{ | |
return; | |
} | |
else if (item < curr_node.data) | |
{ | |
curr_node = curr_node.left; | |
} | |
else if (item > curr_node.data) | |
curr_node = curr_node.right; | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment