diff --git a/DataStructure/treeStructure/BST/BST.cs b/DataStructure/treeStructure/BST/BST.cs new file mode 100644 index 0000000..9b75f07 --- /dev/null +++ b/DataStructure/treeStructure/BST/BST.cs @@ -0,0 +1,63 @@ +using treeStructure.BT; + +namespace treeStructure.BST +{ + class BST + { + private BinaryTreeNode _root = null; + private Comparer _comparer = Comparer.Default; + + public void Insert(T value) + { + BinaryTreeNode node = _root; + if (node == null) + { + _root = new BinaryTreeNode(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(value); + return; + } + + node = node.Left; + } + else + { + if (node.Right == null) + { + node.Right = new BinaryTreeNode(value); + return; + } + + node = node.Right; + } + } + } + + public void PreOrderTraversal() + { + PreOrderRecursive(_root); + } + + private void PreOrderRecursive(BinaryTreeNode node) + { + if (node == null) + return; + + System.Console.WriteLine(node.Data); + PreOrderRecursive(node.Left); + PreOrderRecursive(node.Right); + } + } +} \ No newline at end of file diff --git a/DataStructure/treeStructure/BST/BSTClient.cs b/DataStructure/treeStructure/BST/BSTClient.cs new file mode 100644 index 0000000..f428c32 --- /dev/null +++ b/DataStructure/treeStructure/BST/BSTClient.cs @@ -0,0 +1,17 @@ +namespace treeStructure.BST +{ + class BSTClient + { + public static void DoWork() + { + BST bst = new BST(); + bst.Insert(4); + bst.Insert(16); + bst.Insert(7); + bst.Insert(13); + bst.Insert(2); + + bst.PreOrderTraversal(); + } + } +} \ No newline at end of file diff --git a/DataStructure/treeStructure/BT/BTClient.cs b/DataStructure/treeStructure/BT/BTClient.cs new file mode 100644 index 0000000..36c891e --- /dev/null +++ b/DataStructure/treeStructure/BT/BTClient.cs @@ -0,0 +1,16 @@ +namespace treeStructure.BT +{ + class BTClient + { + public static void DoWork() + { + BinaryTree btree = new BinaryTree(); + btree.Root = new BinaryTreeNode(1); + btree.Root.Left = new BinaryTreeNode(2); + btree.Root.Right = new BinaryTreeNode(3); + btree.Root.Left.Left = new BinaryTreeNode(4); + + btree.PreOrderTraversal(btree.Root); + } + } +} \ No newline at end of file diff --git a/DataStructure/treeStructure/BinaryTree.cs b/DataStructure/treeStructure/BT/BinaryTree.cs similarity index 92% rename from DataStructure/treeStructure/BinaryTree.cs rename to DataStructure/treeStructure/BT/BinaryTree.cs index 5d5274d..63182a1 100644 --- a/DataStructure/treeStructure/BinaryTree.cs +++ b/DataStructure/treeStructure/BT/BinaryTree.cs @@ -1,4 +1,4 @@ -namespace treeStructure +namespace treeStructure.BT { class BinaryTree { diff --git a/DataStructure/treeStructure/BinaryTreeNode.cs b/DataStructure/treeStructure/BT/BinaryTreeNode.cs similarity index 90% rename from DataStructure/treeStructure/BinaryTreeNode.cs rename to DataStructure/treeStructure/BT/BinaryTreeNode.cs index f5f36c4..1cd0f0e 100644 --- a/DataStructure/treeStructure/BinaryTreeNode.cs +++ b/DataStructure/treeStructure/BT/BinaryTreeNode.cs @@ -1,4 +1,4 @@ -namespace treeStructure +namespace treeStructure.BT { class BinaryTreeNode { diff --git a/DataStructure/treeStructure/Program.cs b/DataStructure/treeStructure/Program.cs index 9da4b45..db117b5 100644 --- a/DataStructure/treeStructure/Program.cs +++ b/DataStructure/treeStructure/Program.cs @@ -1,16 +1,18 @@ -namespace treeStructure +using treeStructure.BST; +using treeStructure.BT; +using treeStructure.SortedDict; + +namespace treeStructure { static class Program { static void Main(string[] args) { - BinaryTree btree = new BinaryTree(); - btree.Root = new BinaryTreeNode(1); - btree.Root.Left = new BinaryTreeNode(2); - btree.Root.Right = new BinaryTreeNode(3); - btree.Root.Left.Left = new BinaryTreeNode(4); + // BTClient.DoWork(); + + // BSTClient.DoWork(); - btree.PreOrderTraversal(btree.Root); + SortedDictClient.DoWork(); } } } \ No newline at end of file diff --git a/DataStructure/treeStructure/SortedDict/SortedDictClient.cs b/DataStructure/treeStructure/SortedDict/SortedDictClient.cs new file mode 100644 index 0000000..04fac03 --- /dev/null +++ b/DataStructure/treeStructure/SortedDict/SortedDictClient.cs @@ -0,0 +1,20 @@ +namespace treeStructure.SortedDict +{ + class SortedDictClient + { + public static void DoWork() + { + SortedDictionary tmap = new SortedDictionary(); + tmap.Add(1001, "Tom"); + tmap.Add(1003, "John"); + tmap.Add(1010, "Irina"); + tmap.Add(1002, "Jack"); + tmap.Add(1008, "Richard"); + + foreach (KeyValuePair kv in tmap) + { + System.Console.WriteLine($"{kv.Key}:{kv.Value}"); + } + } + } +} \ No newline at end of file