binary tree, bst

main
syneffort 2 years ago
parent 7d135de61c
commit 677b668c60
  1. 63
      DataStructure/treeStructure/BST/BST.cs
  2. 17
      DataStructure/treeStructure/BST/BSTClient.cs
  3. 16
      DataStructure/treeStructure/BT/BTClient.cs
  4. 2
      DataStructure/treeStructure/BT/BinaryTree.cs
  5. 2
      DataStructure/treeStructure/BT/BinaryTreeNode.cs
  6. 16
      DataStructure/treeStructure/Program.cs
  7. 20
      DataStructure/treeStructure/SortedDict/SortedDictClient.cs

@ -0,0 +1,63 @@
using treeStructure.BT;
namespace treeStructure.BST
{
class BST<T>
{
private BinaryTreeNode<T> _root = null;
private Comparer<T> _comparer = Comparer<T>.Default;
public void Insert(T value)
{
BinaryTreeNode<T> node = _root;
if (node == null)
{
_root = new BinaryTreeNode<T>(value);
return;
}
while (node != null)
{
int result = _comparer.Compare(node.Data, value);
if (result == 0)
return;
if (result > 0)
{
if (node.Left == null)
{
node.Left = new BinaryTreeNode<T>(value);
return;
}
node = node.Left;
}
else
{
if (node.Right == null)
{
node.Right = new BinaryTreeNode<T>(value);
return;
}
node = node.Right;
}
}
}
public void PreOrderTraversal()
{
PreOrderRecursive(_root);
}
private void PreOrderRecursive(BinaryTreeNode<T> node)
{
if (node == null)
return;
System.Console.WriteLine(node.Data);
PreOrderRecursive(node.Left);
PreOrderRecursive(node.Right);
}
}
}

@ -0,0 +1,17 @@
namespace treeStructure.BST
{
class BSTClient
{
public static void DoWork()
{
BST<int> bst = new BST<int>();
bst.Insert(4);
bst.Insert(16);
bst.Insert(7);
bst.Insert(13);
bst.Insert(2);
bst.PreOrderTraversal();
}
}
}

@ -0,0 +1,16 @@
namespace treeStructure.BT
{
class BTClient
{
public static void DoWork()
{
BinaryTree<int> btree = new BinaryTree<int>();
btree.Root = new BinaryTreeNode<int>(1);
btree.Root.Left = new BinaryTreeNode<int>(2);
btree.Root.Right = new BinaryTreeNode<int>(3);
btree.Root.Left.Left = new BinaryTreeNode<int>(4);
btree.PreOrderTraversal(btree.Root);
}
}
}

@ -1,4 +1,4 @@
namespace treeStructure namespace treeStructure.BT
{ {
class BinaryTree<T> class BinaryTree<T>
{ {

@ -1,4 +1,4 @@
namespace treeStructure namespace treeStructure.BT
{ {
class BinaryTreeNode<T> class BinaryTreeNode<T>
{ {

@ -1,16 +1,18 @@
namespace treeStructure using treeStructure.BST;
using treeStructure.BT;
using treeStructure.SortedDict;
namespace treeStructure
{ {
static class Program static class Program
{ {
static void Main(string[] args) static void Main(string[] args)
{ {
BinaryTree<int> btree = new BinaryTree<int>(); // BTClient.DoWork();
btree.Root = new BinaryTreeNode<int>(1);
btree.Root.Left = new BinaryTreeNode<int>(2); // BSTClient.DoWork();
btree.Root.Right = new BinaryTreeNode<int>(3);
btree.Root.Left.Left = new BinaryTreeNode<int>(4);
btree.PreOrderTraversal(btree.Root); SortedDictClient.DoWork();
} }
} }
} }

@ -0,0 +1,20 @@
namespace treeStructure.SortedDict
{
class SortedDictClient
{
public static void DoWork()
{
SortedDictionary<int, string> tmap = new SortedDictionary<int, string>();
tmap.Add(1001, "Tom");
tmap.Add(1003, "John");
tmap.Add(1010, "Irina");
tmap.Add(1002, "Jack");
tmap.Add(1008, "Richard");
foreach (KeyValuePair<int, string> kv in tmap)
{
System.Console.WriteLine($"{kv.Key}:{kv.Value}");
}
}
}
}
Loading…
Cancel
Save