parent
7d135de61c
commit
677b668c60
@ -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> |
||||
{ |
@ -1,4 +1,4 @@ |
||||
namespace treeStructure |
||||
namespace treeStructure.BT |
||||
{ |
||||
class BinaryTreeNode<T> |
||||
{ |
@ -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…
Reference in new issue