Created
July 5, 2016 23:50
-
-
Save gardyna/1483ab81938a27a43142d2730300fdde to your computer and use it in GitHub Desktop.
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
// BynaryTree.cs | |
using System; | |
using System.Collections; | |
using System.Collections.Generic; | |
using System.Linq; | |
using System.Text; | |
using System.Threading.Tasks; | |
// all code copy pasted in haste | |
namespace ConsoleApplication1 { | |
// inherrit from interface | |
public class BinaryTree : ISet { | |
private class node { | |
public int value; | |
public node left; | |
public node right; | |
public node(int val) { | |
left = null; | |
right = null; | |
value = val; | |
} | |
} | |
private node root; | |
public BinaryTree() { | |
root = null; | |
} | |
public void insert(int item) { | |
root = insert(root, item); | |
} | |
/** | |
* recursively insert an item into tree | |
*/ | |
private static node insert(node curr, int val) { | |
// base case if we are at a leaf | |
if(curr == null) { | |
// create new node | |
curr = new node(val); | |
}else if (curr.value > val) { | |
curr.left = insert(curr.left, val); | |
} else if (curr.value < val) { | |
curr.right = insert(curr.right, val); | |
} | |
return curr; | |
} | |
public bool contains(int item) { | |
return contains(root, item); | |
} | |
private bool contains(node curr, int val) { | |
if(curr == null) { return false; } // reached end without finding anything | |
if(curr.value == val) { return true; } | |
if(curr.value > val) { return contains(curr.left, val); } | |
if(curr.value < val) { return contains(curr.right, val); } | |
return false; | |
} | |
} | |
public class LazyImplement : ISet { | |
private List<int> myList = new List<int>(); | |
public bool contains(int item) { | |
return myList.Contains(item); | |
} | |
public void insert(int item) { | |
myList.Add(item); | |
} | |
} | |
// here is our interface | |
public interface ISet { | |
void insert(int item); | |
bool contains(int item); | |
} | |
} | |
// Program.cs | |
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
using System.Text; | |
using System.Threading.Tasks; | |
namespace ConsoleApplication1 { | |
class Program { | |
public static bool hasItem(ISet container, int value) { | |
return container.contains(value); | |
} | |
static void Main(string[] args) { | |
BinaryTree myTree = new BinaryTree(); | |
LazyImplement anotherSet = new LazyImplement(); | |
myTree.insert(42); | |
myTree.insert(20); | |
myTree.insert(10); | |
myTree.insert(15); | |
myTree.insert(5); | |
myTree.insert(100); | |
myTree.insert(1337); | |
anotherSet.insert(21); | |
Console.WriteLine(myTree.contains(5)); | |
Console.WriteLine(myTree.contains(23)); | |
Console.WriteLine(hasItem(myTree, 5)); | |
Console.WriteLine(hasItem(myTree, 21)); | |
Console.WriteLine(hasItem(anotherSet, 21)); | |
Console.WriteLine(hasItem(anotherSet, 4)); | |
Console.ReadLine(); | |
} | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment