Added InOrderTraversal

This commit is contained in:
luke-else 2021-11-18 18:07:13 +00:00
parent c942ce57ce
commit 5c2e7110f8
3 changed files with 27 additions and 15 deletions

View File

@ -20,6 +20,7 @@ namespace BinaryTree
Console.Clear();
tree.Print(tree.Root);
Console.WriteLine("-------------------------------------");
tree.InOrderTraversal(tree.Root);
Console.WriteLine("Delete node: ");
tree.Delete(Convert.ToInt32(Console.ReadLine()));

View File

@ -14,38 +14,38 @@ namespace BinaryTree
public int EndPos { get { return StartPos + Size; } set { StartPos = value - Size; } }
public node(dynamic _Data = null,
node _Parent = null,
node _Left = null,
node _Right = null){
public node(dynamic data = null,
node parent = null,
node left = null,
node right = null){
//New node for a Binary Tree. Values can be set to null if they are not present
Data = _Data;
Parent = _Parent;
Left = _Left;
Right = _Right;
Data = data;
Parent = parent;
Left = left;
Right = right;
}
public node GetParent(){
return Parent;
}
public void SetParent(node _Parent){
Parent = _Parent;
public void SetParent(node parent){
Parent = parent;
}
public node GetLeft(){
return Left;
}
public void SetLeft(node _Left){
Left = _Left;
public void SetLeft(node left){
Left = left;
}
public node GetRight(){
return Right;
}
public void SetRight(node _Right){
Right = _Right;
public void SetRight(node right){
Right = right;
}
}

View File

@ -183,7 +183,17 @@ namespace BinaryTree
}
public void InOrderTraversal(node current){
if (current.GetLeft() != null)
InOrderTraversal(current.GetLeft());
Console.Write($"{current.Data} -> ");
if (current.GetRight() != null)
InOrderTraversal(current.GetRight());
}
//print Method functions
@ -264,6 +274,7 @@ namespace BinaryTree
private void Print(string s, int top, int left, int right = -1)
{
Console.SetCursorPosition(left, top);
if (right < 0) right = left + s.Length;
while (Console.CursorLeft < right) Console.Write(s);