Monday, October 20, 2008

BitArray Set Implementation(C#)

using System.Collections;//
namespace BitArraySet
{
public class CSet
{
private BitArray data;
public CSet()
{
data = new BitArray(5);
}

public void Add(int item)
{
data[item] = true;
}

public bool IsMember(int item)
{
return data[item];
}

public void Remove(int item)
{
data[item] = false;
}

public CSet Union(CSet aSet)
{
CSet tempSet = new CSet();
for (int i = 0; i < data.Count; i++)
{
tempSet.data[i] = (this.data[i] || aSet.data[i]);
}
return tempSet;
}

public CSet Intersection(CSet aSet)
{
CSet tempSet = new CSet();
for (int i = 0; i < data.Count; i++)
{
tempSet.data[i] = (this.data[i] && aSet.data[i]);
}
return tempSet;
}

public CSet Difference(CSet aSet)
{
CSet tempSet = new CSet();
for (int i = 0; i < data.Count; i++)
{
tempSet.data[i] = (this.data[i] && (!(aSet.data[i])));
}
return tempSet;
}

public bool IsSubset(CSet aSet)
{
CSet tempSet = new CSet();
for (int i = 0; i < data.Count; i++)
{
if (this.data[i] && (!(aSet.data[i])))
{
return false;
}
}
return true;
}

public override string ToString()
{
string s = "";
for (int i = 0; i < data.Count; i++)
{
if (data[i])
{
s += i.ToString(); ;
}
}
return s;
}
}

class Program
{
static void Main(string[] args)
{
CSet setA = new CSet();
CSet setB = new CSet();

setA.Add(1);
setA.Add(2);
setA.Add(3);
setB.Add(2);
setB.Add(3);

CSet setC = new CSet();
setC = setA.Union(setB);
Console.WriteLine();
Console.WriteLine(setA.ToString());
Console.WriteLine(setB.ToString());
setC = setA.Intersection(setB);
Console.WriteLine(setC.ToString());
setC = setA.Difference(setB);
Console.WriteLine(setC.ToString());
if (setB.IsSubset(setA))
{
Console.WriteLine("B is a subset of A");
}
else
{
Console.WriteLine("B is not a subset of A");
}
}
}
}

Define the Set Class(C#)

using System.Collections;//
namespace ClassSetPractise
{
public class CSet
{
//the two most important properties of sets are that
//the members of a set are unordered and no member
//can occur in a set more than once
//we only need one data member and one structors members for our CSet
private Hashtable data;
public CSet()
{
data = new Hashtable();
}

public void Add(Object item)
{
if (!(data.ContainsValue(item)))
{
data.Add(Hash(item),item);
}
}
private string Hash(Object item)
{
char[] chars;
int hashValue = 0;
string s = item.ToString();
chars = s.ToCharArray();
for (int i = 0; i <= chars.GetUpperBound(0); i++)
{
hashValue += (int)chars[i];
}
return hashValue.ToString();
}

public void Remove(Object item)
{
data.Remove(Hash(item));
}

public int Size()
{
return data.Count;
}

public CSet Union(CSet aSet)
{
CSet tempSet = new CSet();
foreach (Object hashObject in data.Keys)
{
tempSet.Add(this.data[hashObject]);
}
foreach (Object hashObject in aSet.data.Keys)
{
if (!(this.data.ContainsKey(hashObject)))
{
tempSet.Add(aSet.data[hashObject]);
}
}

return tempSet;
}

public CSet Intersection(CSet aSet)
{
CSet tempSet = new CSet();
foreach (Object hashObject in data.Keys)
{
if (aSet.data.Contains(hashObject))
{
tempSet.Add(aSet.data[hashObject]);//
}
}
return tempSet;
}

public bool Subset(CSet aSet)
{
//if (this.Size > aSet.Size)
//{
// return false;
//}
//else
//{
foreach (Object key in this.data.Keys)
{
if (!(aSet.data.Contains(key)))
{
return false;
}
}
//}
return true;
}

public CSet Different(CSet aSet)
{
CSet tempSet = new CSet();
foreach (Object hashObject in data.Keys)
{
if (!(aSet.data.Contains(hashObject)))
{
tempSet.Add(data[hashObject]);
}
}
return tempSet;
}

public override string ToString()
{
string s = "";
foreach (Object key in data.Keys)
{
s += data[key]+" ";
}
return s;
}
}
class Program
{
static void Main(string[] args)
{
CSet setA = new CSet();
CSet setB = new CSet();

setA.Add("milk");
setA.Add("eggs");
setA.Add("bacon");
setA.Add("cereal");

setB.Add("bacon");
setB.Add("eggs");
setB.Add("bread");

CSet setC = new CSet();
setC = setA.Union(setB);
Console.WriteLine();
Console.WriteLine("A: "+setA.ToString());
Console.WriteLine("B: "+setB.ToString());
Console.WriteLine("A union B: "+setC.ToString());
setC = setA.Intersection(setB);
Console.WriteLine("A intersect B: "+setC.ToString());
setC = setA.Different(setB);
Console.WriteLine("B diff A: "+setC.ToString());
if (setB.Subset(setA))
{
Console.WriteLine("B is a subset of A");
}
else
{
Console.WriteLine("B is not a subset of A");
}
}
}
}

How to delete node from binary search tree(C#)

namespace BinarySearchTreeDelete
{
//define a Node class for tree
public class Node
{
public int Data;
public Node Left;
public Node Right;

public void DisplayNode()
{
Console.Write(Data+" ");
}
}

public class BinarySearchTree
{
public Node root;
public BinarySearchTree()
{
root = null;
}

public void Insert(int i)
{
Node newNode = new Node();
newNode.Data = i;
if (root == null)
{
root = newNode;
}
else
{
Node Current = root;
Node parent;
while (true)
{
parent = Current;
if (i < Current.Data)
{
Current = Current.Left;
if (Current == null)
{
parent.Left = newNode;
break;
}
}
else
{
Current = Current.Right;
if (Current == null)
{
parent.Right = newNode;
break;
}
}
}
}
}

public void InOrder(Node theRoot)
{
if (theRoot != null)
{
InOrder(theRoot.Left);
theRoot.DisplayNode();
InOrder(theRoot.Right);
}
}

public Node GetSuccessor(Node delNode)
{
Node successorParent = delNode;
Node successor = delNode;
Node current = delNode.Right;

while (current != null)
{
successorParent = current;
successor = current;
current = current.Left;
}
if (successor != delNode.Right)
{
successorParent.Left = successor.Right;
successor.Right = delNode.Right;
}
return successor;
}

public bool Delete(int key)
{
Node current = root;
Node parent = root;
bool isLeftChild = true;
while (current.Data != key)
{
parent = current;
if (key < current.Data)
{
isLeftChild = true;
current = current.Left;//
}
else
{
isLeftChild = false;
current = current.Right;
}
if (current == null)
{
return false;
}
}

if (current.Left == null && current.Right == null)
{
if (current == root)
{
root = null;
}
else if (isLeftChild)
{
parent.Left = null;
}
else
{
parent.Right = null;
}
}
else if (current.Right == null)
{
if (current == root)
{
root = current.Left;
}
else if (isLeftChild)
{
parent.Left = current.Left;
}
else
{
parent.Right = current.Right;
}
}
else if (current.Left == null)
{
if (current == root)
{
root = current.Right;
}
else if (isLeftChild)
{
parent.Left = parent.Right;
}
else
{
parent.Right = current.Right;
}
}
else
{
Node successor = GetSuccessor(current);
if (current == root)
{
root = successor;
}
else if (isLeftChild)
{
parent.Left = successor;
}
else
{
parent.Right = successor;
}
successor.Left = current.Left;
}
return true;
}
}

class Program
{
static void Main(string[] args)
{
BinarySearchTree BST = new BinarySearchTree();
BST.Insert(23);
BST.Insert(45);
BST.Insert(16);
BST.Insert(37);
BST.Insert(3);
BST.Insert(99);
BST.Insert(22);
BST.Insert(5);
Console.WriteLine("InOrder traversing...");
BST.InOrder(BST.root);
Console.WriteLine();
if (BST.Delete(45))
{
BST.InOrder(BST.root);
}
else
{
Console.WriteLine("there is no this number...");
}
Console.WriteLine();
}
}
}

Search the Mini value and Max value in Tree(C#)

namespace BinaryTree
{
public class Node
{
public int Data;
public Node Left;
public Node Right;
public void DisplayNode()
{
Console.Write(Data+" ");
}
}

public class BinarySearchTree
{
public Node root;
public BinarySearchTree()
{
root = null;
}
public void Insert(int i)
{
Node newNode = new Node();
newNode.Data = i;
if (root == null)
{
root = newNode;
}
else
{
Node Current = root;
Node Parent;
while (true)
{
Parent = Current;
if (i < Parent.Data)
{
Current = Current.Left;
if (Current == null)
{
Parent.Left = newNode;
break;
}
}
else
{
Current = Current.Right;
if (Current == null)
{
Parent.Right = newNode;
break;
}
}
}
}
}

public void InOrder(Node theRoot)
{
if (theRoot != null)
{
InOrder(theRoot.Left);
theRoot.DisplayNode();
InOrder(theRoot.Right);
}
}
public void FindMini()
{
Node Current = root;
while (Current.Left != null)
{
Current = Current.Left;
}
Console.WriteLine("the Mini data is: "+Current.Data);
}

public void FindMax()
{
Node Current = root;
while (Current.Right != null)
{
Current = Current.Right;
}
Console.WriteLine("the Max data is: " + Current.Data);
}
}

class Program
{
static void Main(string[] args)
{
BinarySearchTree BST = new BinarySearchTree();
BST.Insert(23);
BST.Insert(45);
BST.Insert(16);
BST.Insert(37);
BST.Insert(3);
BST.Insert(99);
BST.Insert(22);
Console.WriteLine("traversing with InOrder...");
BST.InOrder(BST.root);
Console.WriteLine();
Console.WriteLine();
Console.WriteLine("Find the Mini data in this tree...");
BST.FindMini();
Console.WriteLine();
Console.WriteLine("Find the Max data in this tree...");
BST.FindMax();
Console.WriteLine();
}
}
}

How to Use Binary Search Tree(C#)

namespace BinaryTree
{
public class Node
{
public int Data;
public Node Left;
public Node Right;
public void DisplayNode()
{
Console.Write(Data+" ");
}
}

public class BinarySearchTree
{
public Node root;
public BinarySearchTree()
{
root = null;
}
public void Insert(int i)
{
Node newNode = new Node();
newNode.Data = i;
if (root == null)
{
root = newNode;
}
else
{
Node Current = root;
Node Parent;
while (true)
{
Parent = Current;
if (i < Parent.Data)
{
Current = Current.Left;
if (Current == null)
{
Parent.Left = newNode;
break;
}
}
else
{
Current = Current.Right;
if (Current == null)
{
Parent.Right = newNode;
break;
}
}
}
}
}

public void InOrder(Node theRoot)
{
if (theRoot != null)
{
InOrder(theRoot.Left);
theRoot.DisplayNode();
InOrder(theRoot.Right);
}
}
public void PreOrder(Node theRoot)
{
if (theRoot != null)
{
theRoot.DisplayNode();
PreOrder(theRoot.Left);
PreOrder(theRoot.Right);
}
}

public void PostOrder(Node theRoot)
{
if (theRoot != null)
{
PostOrder(theRoot.Left);
PostOrder(theRoot.Right);
theRoot.DisplayNode();
}
}
}

class Program
{
static void Main(string[] args)
{
BinarySearchTree BST = new BinarySearchTree();
BST.Insert(23);
BST.Insert(45);
BST.Insert(16);
BST.Insert(37);
BST.Insert(3);
BST.Insert(99);
BST.Insert(22);
Console.WriteLine("traversing with InOrder...");
BST.InOrder(BST.root);
Console.WriteLine();
Console.WriteLine();
Console.WriteLine("traversing with PreOrder...");
BST.PreOrder(BST.root);
Console.WriteLine();
Console.WriteLine();
Console.WriteLine("traversing with PostOrder...");
BST.PostOrder(BST.root);
Console.WriteLine();
Console.WriteLine();
}
}
}

how to Create a Binary Tree(C#)

namespace BinaryTree
{
public class Node
{
public int Data;
public Node Left;
public Node Right;
public void DisplayNode()
{
Console.WriteLine(Data+" ");
}
}

public class BinarySearchTree
{
public Node root;
public BinarySearchTree()
{
root = null;
}
public void Insert(int i)
{
Node newNode = new Node();
newNode.Data = i;
if (root == null)
{
root = newNode;
}
else
{
Node Current = root;
Node Parent;
while (true)
{
Parent = Current;
if (i < Parent.Data)
{
Current = Current.Left;
if (Current == null)
{
Parent.Left = newNode;
break;
}
}
else
{
Current = Current.Right;
if (Current == null)
{
Parent.Right = newNode;
break;
}
}
}
}
}

}

class Program
{
static void Main(string[] args)
{
BinarySearchTree BST = new BinarySearchTree();
Random rd = new Random();
for (int i = 0; i < 10; i++)
{
BST.Insert(rd.Next(30));
}
}
}
}