using System;
using System.Collections.Generic;
using System.Xml.Linq;
class Program
{
public static int Counter = 0;
static void Main()
{
BCT btree = new BCT();
btree.Insert(10);
btree.Insert(20);
btree.Insert(5);
btree.Insert(7);
btree.Insert(8);
btree.Insert(0);
btree.Insert(30);
btree.Print(); // 0 3 6 7 9 13 20
}
public static int Fibo(int n)
{
Counter++;
if (n == 0) return 0;
if (n == 1) return 1;
int left = Fibo(n - 1);
int right = Fibo(n - 2);
return left + right;
}
public class TreeNode
{
public TreeNode Left;
public TreeNode Right;
public int Value;
public TreeNode(int value)
{
Value = value;
}
}
public class BTree
{
public TreeNode Root;
public void Insert(int value)
{
InsertNode(Root, value);
}
private void InsertNode(TreeNode node, int value)
{
// 1. Base case (nothing exists)
if (Root == null)
{
Root = new TreeNode(value);
return;
}
// 2. General case (smth exists)
if (value < node.Value)
{
// Exit point
if (node.Left == null)
{
node.Left = new TreeNode(value);
return;
}
else
{
InsertNode(Root.Left, value);
}
}
else if (value > node.Value)
{
// Exit point
if (node.Right == null)
{
node.Right = new TreeNode(value);
return;
}
else
{
InsertNode(Root.Right, value);
}
}
}
public bool Contains(int value)
{
return ContainsNode(Root, value);
}
private bool ContainsNode(TreeNode node, int value)
{
if (node == null) return false;
if (value < node.Value) return ContainsNode(node.Left, value);
if (value > node.Value) return ContainsNode(node.Right, value);
return true;
}
public void Print()
{
PrintTree(Root);
}
private void PrintTree(TreeNode root)
{
if (root == null) return;
PrintTree(root.Left);
Console.WriteLine(root.Value);
PrintTree(root.Right);
}
}
public class MyDictionary<TKey, TValue>
{
private LinkedList<KeyValuePair<TKey, TValue>>[] _buckets;
private const int DefaultCapacity = 10;
public MyDictionary(int defaultCapacity = DefaultCapacity)
{
if (defaultCapacity < 0) defaultCapacity = DefaultCapacity;
_buckets = new LinkedList<KeyValuePair<TKey, TValue>>[defaultCapacity];
}
private int GetIndex(TKey key)
{
int hash = Math.Abs(key.GetHashCode());
return hash % _buckets.Length;
}
public bool AddOrUpdate(TKey key, TValue value)
{
int index = GetIndex(key);
if (_buckets[index] == null)
{
_buckets[index] = new LinkedList<KeyValuePair<TKey, TValue>>();
}
LinkedList<KeyValuePair<TKey, TValue>> list = _buckets[index];
foreach (var pair in list)
{
if (pair.Key.Equals(key))
{
list.Remove(pair);
list.AddLast(new KeyValuePair<TKey, TValue>(key, value));
return false; // no new element was added, but previous was updated
}
}
list.AddLast(new KeyValuePair<TKey, TValue>(key, value));
return true;
}
public bool TryGetValue(TKey key, out TValue value)
{
int index = GetIndex(key);
if (_buckets[index] != null)
{
LinkedList<KeyValuePair<TKey, TValue>> list = _buckets[index];
foreach (var pair in list)
{
if (pair.Key.Equals(key))
{
value = pair.Value;
return true;
}
}
}
value = default;
return false;
}
public bool Remove(TKey key)
{
int index = GetIndex(key);
if (_buckets[index] != null)
{
LinkedList<KeyValuePair<TKey, TValue>> list = _buckets[index];
foreach (var pair in list)
{
if (pair.Key.Equals(key))
{
list.Remove(pair);
return true;
}
}
}
return false;
}
public bool ContainsKey(TKey key)
{
int index = GetIndex(key);
if (_buckets[index] != null)
{
return true;
}
return false;
}
public TValue this[TKey key]
{
get
{
TValue dummy = default;
TryGetValue(key, out dummy);
return dummy;
}
set
{
AddOrUpdate(key, value);
}
}
}
public class BCT
{
TreeNode root;
Queue<TreeNode> queue = new Queue<TreeNode>();
public void Insert(int value)
{
if (root == null)
{
root = new TreeNode(value);
queue.Enqueue(root);
return;
}
TreeNode top = queue.Peek();
if (top.Left == null)
{
top.Left = new TreeNode(value);
queue.Enqueue(top.Left);
}
else if (top.Right == null)
{
top.Right = new TreeNode(value);
queue.Enqueue(top.Right);
}
if (top.Left != null && top.Right != null)
queue.Dequeue();
}
public void Print()
{
Queue<TreeNode> qq = new Queue<TreeNode>();
qq.Enqueue(root);
while (qq.Count > 0)
{
TreeNode current = qq.Dequeue();
Console.Write(current.Value+" ");
if (current.Left != null) qq.Enqueue(current.Left);
if (current.Right != null) qq.Enqueue(current.Right);
}
}
}
}