Updated Binary Tree to Include all Traversals
Traversals to be selected using enum
This commit is contained in:
parent
d4291ab9ec
commit
a8d47f1061
@ -1,5 +1,6 @@
|
||||
using C_.Datastructures.Stack;
|
||||
using C_.Datastructures.LinkedList;
|
||||
using C_.Datastructures.Stack;
|
||||
using C_.Datastructures.Queue;
|
||||
using System;
|
||||
|
||||
namespace C_.Datastructures.BinaryTree
|
||||
@ -116,31 +117,86 @@ namespace C_.Datastructures.BinaryTree
|
||||
return true;
|
||||
}
|
||||
|
||||
public LinkedList<T>? Inorder()
|
||||
{//
|
||||
if (Count == 0)
|
||||
public LinkedList<T>? Traverse(TraversalType traversalType)
|
||||
{
|
||||
if (Root == default)
|
||||
return default;
|
||||
|
||||
LinkedList<T> list = LinkedList<T>.Create();
|
||||
|
||||
Inorder(list, Root!);
|
||||
switch (traversalType)
|
||||
{//Select which type of traversal to do
|
||||
case TraversalType.Inorder:
|
||||
Inorder(list, Root);
|
||||
break;
|
||||
case TraversalType.Preorder:
|
||||
Preorder(list, Root);
|
||||
break;
|
||||
case TraversalType.Postorder:
|
||||
Postorder(list, Root);
|
||||
break;
|
||||
case TraversalType.Breadth:
|
||||
Queue<TreeNode<T>> queue = Queue<TreeNode<T>>.Create();
|
||||
BreadthFirst(list, queue, Root);
|
||||
break;
|
||||
default:
|
||||
return default;
|
||||
}
|
||||
|
||||
return list;
|
||||
}
|
||||
private void Inorder(LinkedList<T> list, TreeNode<T> node)
|
||||
{
|
||||
{//Inorder Traversal
|
||||
if (node.Left != default)
|
||||
Inorder(list, node.Left);
|
||||
|
||||
list.Append(node.Value!);
|
||||
list.Append(node.Value);
|
||||
|
||||
if (node.Right != default)
|
||||
Inorder(list, node.Right);
|
||||
}
|
||||
|
||||
private void Preorder(LinkedList<T> list, TreeNode<T> node)
|
||||
{//Preorder Traversal
|
||||
list.Append(node.Value);
|
||||
|
||||
if (node.Left != default)
|
||||
Preorder(list, node.Left);
|
||||
|
||||
if (node.Right != default)
|
||||
Preorder(list, node.Right);
|
||||
}
|
||||
|
||||
private void Postorder(LinkedList<T> list, TreeNode<T> node)
|
||||
{//Postorder Traversal
|
||||
if (node.Left != default)
|
||||
Postorder(list, node.Left);
|
||||
|
||||
if (node.Right != default)
|
||||
Postorder(list, node.Right);
|
||||
|
||||
list.Append(node.Value);
|
||||
}
|
||||
|
||||
private void BreadthFirst(LinkedList<T> list, Queue<TreeNode<T>> queue, TreeNode<T> node)
|
||||
{//Breadth First Traversal
|
||||
list.Append(node.Value);
|
||||
|
||||
if (node.Left != default)
|
||||
queue.Push(node.Left);
|
||||
|
||||
if (node.Right != default)
|
||||
queue.Push(node.Right);
|
||||
|
||||
//Only continue to traverse if there are no mode nodes to process
|
||||
if (queue.Peek() != default)
|
||||
BreadthFirst(list, queue, queue.Pop()!);
|
||||
}
|
||||
|
||||
|
||||
|
||||
private TreeNode<T>? Traverse(T value, TreeNode<T>? node)
|
||||
|
||||
private TreeNode<T>? GetNext(T value, TreeNode<T>? node)
|
||||
{//T is comparable so use methods to determine which way to traverse
|
||||
if(node == default)
|
||||
return default;
|
||||
@ -168,7 +224,7 @@ namespace C_.Datastructures.BinaryTree
|
||||
if (stack.Peek()!.Value!.Equals(value))
|
||||
return stack;
|
||||
|
||||
stack.Push(Traverse(value, stack.Peek()));
|
||||
stack.Push(GetNext(value, stack.Peek()));
|
||||
}
|
||||
return default;
|
||||
}
|
||||
@ -213,7 +269,7 @@ namespace C_.Datastructures.BinaryTree
|
||||
|
||||
TreeNode<T>? node;
|
||||
|
||||
node = Descend(value, Traverse(value, current));
|
||||
node = Descend(value, GetNext(value, current));
|
||||
if (node == null)
|
||||
{
|
||||
return current;
|
||||
@ -221,4 +277,12 @@ namespace C_.Datastructures.BinaryTree
|
||||
return node;
|
||||
}
|
||||
}
|
||||
|
||||
public enum TraversalType
|
||||
{//Enum to allow for Traversal selection
|
||||
Inorder,
|
||||
Preorder,
|
||||
Postorder,
|
||||
Breadth
|
||||
}
|
||||
}
|
@ -112,20 +112,27 @@ Console.WriteLine("Hello, World!");
|
||||
|
||||
Tree<int> tree = new Tree<int>();
|
||||
|
||||
tree.Add(10);
|
||||
tree.Add(8);
|
||||
tree.Add(6);
|
||||
tree.Add(3);
|
||||
tree.Add(4);
|
||||
tree.Add(5);
|
||||
tree.Add(2);
|
||||
tree.Add(0);
|
||||
tree.Add(40);
|
||||
tree.Add(30);
|
||||
tree.Add(50);
|
||||
tree.Add(25);
|
||||
tree.Add(35);
|
||||
tree.Add(45);
|
||||
tree.Add(60);
|
||||
tree.Add(15);
|
||||
tree.Add(28);
|
||||
tree.Add(55);
|
||||
tree.Add(70);
|
||||
|
||||
|
||||
var x = tree.Delete(10);
|
||||
//var x = tree.Delete(10);
|
||||
//tree.Delete(5);
|
||||
|
||||
var l = tree.Inorder(null);
|
||||
var inorder = tree.Traverse(TraversalType.Inorder);
|
||||
var preorder = tree.Traverse(TraversalType.Preorder);
|
||||
var postorder = tree.Traverse(TraversalType.Postorder);
|
||||
var breadthFirst = tree.Traverse(TraversalType.Breadth);
|
||||
|
||||
|
||||
Console.ReadLine();
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user