Added Inorder Traversal
This commit is contained in:
parent
8ac85d8a1f
commit
4b45f5d561
@ -1,5 +1,5 @@
|
|||||||
using C_.Datastructures.BinaryTree;
|
|
||||||
using C_.Datastructures.Stack;
|
using C_.Datastructures.Stack;
|
||||||
|
using C_.Datastructures.LinkedList;
|
||||||
using System;
|
using System;
|
||||||
|
|
||||||
namespace C_.Datastructures.BinaryTree
|
namespace C_.Datastructures.BinaryTree
|
||||||
@ -54,7 +54,7 @@ namespace C_.Datastructures.BinaryTree
|
|||||||
if (Count == 0)
|
if (Count == 0)
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
//Check if value is the root
|
//Check if the only value is the root
|
||||||
if (Count == 1)
|
if (Count == 1)
|
||||||
{
|
{
|
||||||
if (Root!.Value!.Equals(value))
|
if (Root!.Value!.Equals(value))
|
||||||
@ -108,6 +108,45 @@ namespace C_.Datastructures.BinaryTree
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public LinkedList<T>? Inorder(TreeNode<T>? node)
|
||||||
|
{
|
||||||
|
LinkedList<T> list = LinkedList<T>.Create();
|
||||||
|
|
||||||
|
if (Count == 0)
|
||||||
|
return default;
|
||||||
|
|
||||||
|
if (node == null)
|
||||||
|
node = Root;
|
||||||
|
|
||||||
|
Inorder(list, node!);
|
||||||
|
return list;
|
||||||
|
}
|
||||||
|
private void Inorder(LinkedList<T> list, TreeNode<T> node)
|
||||||
|
{
|
||||||
|
if (node.Left != default)
|
||||||
|
Inorder(list, node.Left);
|
||||||
|
|
||||||
|
list.Append(node.Value!);
|
||||||
|
|
||||||
|
if (node.Right != default)
|
||||||
|
Inorder(list, node.Right);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
private TreeNode<T>? Traverse(T value, TreeNode<T>? 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<TreeNode<T>>? Find(T value)
|
private Stack<TreeNode<T>>? Find(T value)
|
||||||
{//Return true if the item can be found within the tree
|
{//Return true if the item can be found within the tree
|
||||||
@ -162,19 +201,6 @@ namespace C_.Datastructures.BinaryTree
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private TreeNode<T>? Traverse(T value, TreeNode<T>? 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<T>? Descend(T value, TreeNode<T>? current)
|
private TreeNode<T>? Descend(T value, TreeNode<T>? current)
|
||||||
{//Keep trying to determine whether to go left or right until null node is found that can be appended to
|
{//Keep trying to determine whether to go left or right until null node is found that can be appended to
|
||||||
if (current == default)
|
if (current == default)
|
||||||
|
@ -122,7 +122,9 @@ tree.Add(1);
|
|||||||
tree.Add(8);
|
tree.Add(8);
|
||||||
tree.Add(7);
|
tree.Add(7);
|
||||||
|
|
||||||
var x = tree.Delete(4);
|
var x = tree.Delete(1);
|
||||||
|
|
||||||
|
var l = tree.Inorder(null);
|
||||||
|
|
||||||
Console.ReadLine();
|
Console.ReadLine();
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user