diff --git a/C#/Datastructures/BinaryTree/Tree.cs b/C#/Datastructures/BinaryTree/Tree.cs index 13803a8..d9a2c9a 100644 --- a/C#/Datastructures/BinaryTree/Tree.cs +++ b/C#/Datastructures/BinaryTree/Tree.cs @@ -1,5 +1,5 @@ -using C_.Datastructures.BinaryTree; using C_.Datastructures.Stack; +using C_.Datastructures.LinkedList; using System; namespace C_.Datastructures.BinaryTree @@ -54,7 +54,7 @@ namespace C_.Datastructures.BinaryTree if (Count == 0) return false; - //Check if value is the root + //Check if the only value is the root if (Count == 1) { if (Root!.Value!.Equals(value)) @@ -108,6 +108,45 @@ namespace C_.Datastructures.BinaryTree return true; } + public LinkedList? Inorder(TreeNode? node) + { + LinkedList list = LinkedList.Create(); + + if (Count == 0) + return default; + + if (node == null) + node = Root; + + Inorder(list, node!); + return list; + } + private void Inorder(LinkedList list, TreeNode node) + { + if (node.Left != default) + Inorder(list, node.Left); + + list.Append(node.Value!); + + if (node.Right != default) + Inorder(list, node.Right); + } + + + + + private TreeNode? Traverse(T value, TreeNode? node) + {//T is comparable so use methods to determine which way to traverse + if(node == default) + return default; + + if (value.CompareTo(node.Value) < 0) + {//Traverse Left + return node.Left; + } + //Traverse Right + return node.Right; + } private Stack>? Find(T value) {//Return true if the item can be found within the tree @@ -162,19 +201,6 @@ namespace C_.Datastructures.BinaryTree } } - private TreeNode? Traverse(T value, TreeNode? node) - {//T is comparable so use methods to determine which way to traverse - if(node == default) - return default; - - if (value.CompareTo(node.Value) < 0) - {//Traverse Left - return node.Left; - } - //Traverse Right - return node.Right; - } - private TreeNode? Descend(T value, TreeNode? current) {//Keep trying to determine whether to go left or right until null node is found that can be appended to if (current == default) diff --git a/C#/Program.cs b/C#/Program.cs index 82a9763..0dc8ca5 100644 --- a/C#/Program.cs +++ b/C#/Program.cs @@ -122,7 +122,9 @@ tree.Add(1); tree.Add(8); tree.Add(7); -var x = tree.Delete(4); +var x = tree.Delete(1); + +var l = tree.Inorder(null); Console.ReadLine();