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(); Console.Clear();
tree.Print(tree.Root); tree.Print(tree.Root);
Console.WriteLine("-------------------------------------"); Console.WriteLine("-------------------------------------");
tree.InOrderTraversal(tree.Root);
Console.WriteLine("Delete node: "); Console.WriteLine("Delete node: ");
tree.Delete(Convert.ToInt32(Console.ReadLine())); 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 int EndPos { get { return StartPos + Size; } set { StartPos = value - Size; } }
public node(dynamic _Data = null, public node(dynamic data = null,
node _Parent = null, node parent = null,
node _Left = null, node left = null,
node _Right = null){ node right = null){
//New node for a Binary Tree. Values can be set to null if they are not present //New node for a Binary Tree. Values can be set to null if they are not present
Data = _Data; Data = data;
Parent = _Parent; Parent = parent;
Left = _Left; Left = left;
Right = _Right; Right = right;
} }
public node GetParent(){ public node GetParent(){
return Parent; return Parent;
} }
public void SetParent(node _Parent){ public void SetParent(node parent){
Parent = _Parent; Parent = parent;
} }
public node GetLeft(){ public node GetLeft(){
return Left; return Left;
} }
public void SetLeft(node _Left){ public void SetLeft(node left){
Left = _Left; Left = left;
} }
public node GetRight(){ public node GetRight(){
return Right; return Right;
} }
public void SetRight(node _Right){ public void SetRight(node right){
Right = _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 //print Method functions
@ -264,6 +274,7 @@ namespace BinaryTree
private void Print(string s, int top, int left, int right = -1) private void Print(string s, int top, int left, int right = -1)
{ {
Console.SetCursorPosition(left, top); Console.SetCursorPosition(left, top);
if (right < 0) right = left + s.Length; if (right < 0) right = left + s.Length;
while (Console.CursorLeft < right) Console.Write(s); while (Console.CursorLeft < right) Console.Write(s);