Compare commits
74 Commits
8945da2569
...
master
Author | SHA1 | Date | |
---|---|---|---|
6a3d627d5a | |||
2056bac6b0 | |||
b21efe408b | |||
44fa9f8440 | |||
c7ccd641c4 | |||
a8d47f1061 | |||
d4291ab9ec | |||
a4dfb0f58a | |||
e8c99f8d5d | |||
5360fdd47b | |||
a03771cce5 | |||
2a9cf2ff4d | |||
1c5a9c83ce | |||
564e4d161f | |||
6ae360f3bc | |||
88a7a7577e | |||
e5b0d1659b | |||
4b45f5d561 | |||
8ac85d8a1f | |||
1c14ff75d6 | |||
9de49129e7 | |||
6906d2e764 | |||
a2c7326e4b | |||
31888a1529 | |||
9fab683f85 | |||
cb09f165f8 | |||
3b39165316 | |||
3f97e4b770 | |||
702b251a8a | |||
735bf68e0d | |||
e9df1cab67 | |||
e1a85f7b53 | |||
27c51969f8 | |||
78df73e66d | |||
e5fa390608 | |||
88d84ab448 | |||
fc832edb9d | |||
703826c8a6 | |||
c54401fdbb | |||
1738ef0e5a | |||
bff4abff01 | |||
40f73375b9 | |||
4fb6d4e15a | |||
c69c6ee0d3 | |||
3d5f1cfbc1 | |||
ea5d5a73ed | |||
de0f4195ed | |||
fdd1461e3a | |||
f263a14170 | |||
ba2f01f836 | |||
7a3c15d602 | |||
8444f0fa0a | |||
6e63055274 | |||
1db992492b | |||
7bb55b8845 | |||
e6557a1d90 | |||
f2c3e74d15 | |||
1998e77f18 | |||
2828466e23 | |||
431785b586 | |||
7ced980a90 | |||
5c91fce224 | |||
c853d5250c | |||
79debf3d62 | |||
bd5c546794 | |||
9c1a5738ca | |||
80bb1cc492 | |||
fffce6c6bb | |||
00f52ee5c7 | |||
8165bb849b | |||
02f49782ee | |||
dc2768f1ac | |||
2382011f26 | |||
3cc5502db4 |
2
.gitignore
vendored
2
.gitignore
vendored
@@ -1,5 +1,5 @@
|
||||
# ---> VisualStudioCode
|
||||
.vscode/*
|
||||
.vscode/
|
||||
!.vscode/settings.json
|
||||
!.vscode/tasks.json
|
||||
!.vscode/launch.json
|
||||
|
414
C#/.gitignore
vendored
414
C#/.gitignore
vendored
@@ -1,414 +0,0 @@
|
||||
# ---> VisualStudioCode
|
||||
.vscode/*
|
||||
!.vscode/settings.json
|
||||
!.vscode/tasks.json
|
||||
!.vscode/launch.json
|
||||
!.vscode/extensions.json
|
||||
!.vscode/*.code-snippets
|
||||
|
||||
# Local History for Visual Studio Code
|
||||
.history/
|
||||
|
||||
# Built Visual Studio Code Extensions
|
||||
*.vsix
|
||||
|
||||
# ---> VisualStudio
|
||||
## Ignore Visual Studio temporary files, build results, and
|
||||
## files generated by popular Visual Studio add-ons.
|
||||
##
|
||||
## Get latest from https://github.com/github/gitignore/blob/main/VisualStudio.gitignore
|
||||
|
||||
# User-specific files
|
||||
*.rsuser
|
||||
*.suo
|
||||
*.user
|
||||
*.userosscache
|
||||
*.sln.docstates
|
||||
|
||||
# User-specific files (MonoDevelop/Xamarin Studio)
|
||||
*.userprefs
|
||||
|
||||
# Mono auto generated files
|
||||
mono_crash.*
|
||||
|
||||
# Build results
|
||||
[Dd]ebug/
|
||||
[Dd]ebugPublic/
|
||||
[Rr]elease/
|
||||
[Rr]eleases/
|
||||
x64/
|
||||
x86/
|
||||
[Ww][Ii][Nn]32/
|
||||
[Aa][Rr][Mm]/
|
||||
[Aa][Rr][Mm]64/
|
||||
bld/
|
||||
[Bb]in/
|
||||
[Oo]bj/
|
||||
[Ll]og/
|
||||
[Ll]ogs/
|
||||
|
||||
# Visual Studio 2015/2017 cache/options directory
|
||||
.vs/
|
||||
# Uncomment if you have tasks that create the project's static files in wwwroot
|
||||
#wwwroot/
|
||||
|
||||
# Visual Studio 2017 auto generated files
|
||||
Generated\ Files/
|
||||
|
||||
# MSTest test Results
|
||||
[Tt]est[Rr]esult*/
|
||||
[Bb]uild[Ll]og.*
|
||||
|
||||
# NUnit
|
||||
*.VisualState.xml
|
||||
TestResult.xml
|
||||
nunit-*.xml
|
||||
|
||||
# Build Results of an ATL Project
|
||||
[Dd]ebugPS/
|
||||
[Rr]eleasePS/
|
||||
dlldata.c
|
||||
|
||||
# Benchmark Results
|
||||
BenchmarkDotNet.Artifacts/
|
||||
|
||||
# .NET Core
|
||||
project.lock.json
|
||||
project.fragment.lock.json
|
||||
artifacts/
|
||||
|
||||
# ASP.NET Scaffolding
|
||||
ScaffoldingReadMe.txt
|
||||
|
||||
# StyleCop
|
||||
StyleCopReport.xml
|
||||
|
||||
# Files built by Visual Studio
|
||||
*_i.c
|
||||
*_p.c
|
||||
*_h.h
|
||||
*.ilk
|
||||
*.meta
|
||||
*.obj
|
||||
*.iobj
|
||||
*.pch
|
||||
*.pdb
|
||||
*.ipdb
|
||||
*.pgc
|
||||
*.pgd
|
||||
*.rsp
|
||||
*.sbr
|
||||
*.tlb
|
||||
*.tli
|
||||
*.tlh
|
||||
*.tmp
|
||||
*.tmp_proj
|
||||
*_wpftmp.csproj
|
||||
*.log
|
||||
*.tlog
|
||||
*.vspscc
|
||||
*.vssscc
|
||||
.builds
|
||||
*.pidb
|
||||
*.svclog
|
||||
*.scc
|
||||
|
||||
# Chutzpah Test files
|
||||
_Chutzpah*
|
||||
|
||||
# Visual C++ cache files
|
||||
ipch/
|
||||
*.aps
|
||||
*.ncb
|
||||
*.opendb
|
||||
*.opensdf
|
||||
*.sdf
|
||||
*.cachefile
|
||||
*.VC.db
|
||||
*.VC.VC.opendb
|
||||
|
||||
# Visual Studio profiler
|
||||
*.psess
|
||||
*.vsp
|
||||
*.vspx
|
||||
*.sap
|
||||
|
||||
# Visual Studio Trace Files
|
||||
*.e2e
|
||||
|
||||
# TFS 2012 Local Workspace
|
||||
$tf/
|
||||
|
||||
# Guidance Automation Toolkit
|
||||
*.gpState
|
||||
|
||||
# ReSharper is a .NET coding add-in
|
||||
_ReSharper*/
|
||||
*.[Rr]e[Ss]harper
|
||||
*.DotSettings.user
|
||||
|
||||
# TeamCity is a build add-in
|
||||
_TeamCity*
|
||||
|
||||
# DotCover is a Code Coverage Tool
|
||||
*.dotCover
|
||||
|
||||
# AxoCover is a Code Coverage Tool
|
||||
.axoCover/*
|
||||
!.axoCover/settings.json
|
||||
|
||||
# Coverlet is a free, cross platform Code Coverage Tool
|
||||
coverage*.json
|
||||
coverage*.xml
|
||||
coverage*.info
|
||||
|
||||
# Visual Studio code coverage results
|
||||
*.coverage
|
||||
*.coveragexml
|
||||
|
||||
# NCrunch
|
||||
_NCrunch_*
|
||||
.*crunch*.local.xml
|
||||
nCrunchTemp_*
|
||||
|
||||
# MightyMoose
|
||||
*.mm.*
|
||||
AutoTest.Net/
|
||||
|
||||
# Web workbench (sass)
|
||||
.sass-cache/
|
||||
|
||||
# Installshield output folder
|
||||
[Ee]xpress/
|
||||
|
||||
# DocProject is a documentation generator add-in
|
||||
DocProject/buildhelp/
|
||||
DocProject/Help/*.HxT
|
||||
DocProject/Help/*.HxC
|
||||
DocProject/Help/*.hhc
|
||||
DocProject/Help/*.hhk
|
||||
DocProject/Help/*.hhp
|
||||
DocProject/Help/Html2
|
||||
DocProject/Help/html
|
||||
|
||||
# Click-Once directory
|
||||
publish/
|
||||
|
||||
# Publish Web Output
|
||||
*.[Pp]ublish.xml
|
||||
*.azurePubxml
|
||||
# Note: Comment the next line if you want to checkin your web deploy settings,
|
||||
# but database connection strings (with potential passwords) will be unencrypted
|
||||
*.pubxml
|
||||
*.publishproj
|
||||
|
||||
# Microsoft Azure Web App publish settings. Comment the next line if you want to
|
||||
# checkin your Azure Web App publish settings, but sensitive information contained
|
||||
# in these scripts will be unencrypted
|
||||
PublishScripts/
|
||||
|
||||
# NuGet Packages
|
||||
*.nupkg
|
||||
# NuGet Symbol Packages
|
||||
*.snupkg
|
||||
# The packages folder can be ignored because of Package Restore
|
||||
**/[Pp]ackages/*
|
||||
# except build/, which is used as an MSBuild target.
|
||||
!**/[Pp]ackages/build/
|
||||
# Uncomment if necessary however generally it will be regenerated when needed
|
||||
#!**/[Pp]ackages/repositories.config
|
||||
# NuGet v3's project.json files produces more ignorable files
|
||||
*.nuget.props
|
||||
*.nuget.targets
|
||||
|
||||
# Microsoft Azure Build Output
|
||||
csx/
|
||||
*.build.csdef
|
||||
|
||||
# Microsoft Azure Emulator
|
||||
ecf/
|
||||
rcf/
|
||||
|
||||
# Windows Store app package directories and files
|
||||
AppPackages/
|
||||
BundleArtifacts/
|
||||
Package.StoreAssociation.xml
|
||||
_pkginfo.txt
|
||||
*.appx
|
||||
*.appxbundle
|
||||
*.appxupload
|
||||
|
||||
# Visual Studio cache files
|
||||
# files ending in .cache can be ignored
|
||||
*.[Cc]ache
|
||||
# but keep track of directories ending in .cache
|
||||
!?*.[Cc]ache/
|
||||
|
||||
# Others
|
||||
ClientBin/
|
||||
~$*
|
||||
*~
|
||||
*.dbmdl
|
||||
*.dbproj.schemaview
|
||||
*.jfm
|
||||
*.pfx
|
||||
*.publishsettings
|
||||
orleans.codegen.cs
|
||||
|
||||
# Including strong name files can present a security risk
|
||||
# (https://github.com/github/gitignore/pull/2483#issue-259490424)
|
||||
#*.snk
|
||||
|
||||
# Since there are multiple workflows, uncomment next line to ignore bower_components
|
||||
# (https://github.com/github/gitignore/pull/1529#issuecomment-104372622)
|
||||
#bower_components/
|
||||
|
||||
# RIA/Silverlight projects
|
||||
Generated_Code/
|
||||
|
||||
# Backup & report files from converting an old project file
|
||||
# to a newer Visual Studio version. Backup files are not needed,
|
||||
# because we have git ;-)
|
||||
_UpgradeReport_Files/
|
||||
Backup*/
|
||||
UpgradeLog*.XML
|
||||
UpgradeLog*.htm
|
||||
ServiceFabricBackup/
|
||||
*.rptproj.bak
|
||||
|
||||
# SQL Server files
|
||||
*.mdf
|
||||
*.ldf
|
||||
*.ndf
|
||||
|
||||
# Business Intelligence projects
|
||||
*.rdl.data
|
||||
*.bim.layout
|
||||
*.bim_*.settings
|
||||
*.rptproj.rsuser
|
||||
*- [Bb]ackup.rdl
|
||||
*- [Bb]ackup ([0-9]).rdl
|
||||
*- [Bb]ackup ([0-9][0-9]).rdl
|
||||
|
||||
# Microsoft Fakes
|
||||
FakesAssemblies/
|
||||
|
||||
# GhostDoc plugin setting file
|
||||
*.GhostDoc.xml
|
||||
|
||||
# Node.js Tools for Visual Studio
|
||||
.ntvs_analysis.dat
|
||||
node_modules/
|
||||
|
||||
# Visual Studio 6 build log
|
||||
*.plg
|
||||
|
||||
# Visual Studio 6 workspace options file
|
||||
*.opt
|
||||
|
||||
# Visual Studio 6 auto-generated workspace file (contains which files were open etc.)
|
||||
*.vbw
|
||||
|
||||
# Visual Studio 6 auto-generated project file (contains which files were open etc.)
|
||||
*.vbp
|
||||
|
||||
# Visual Studio 6 workspace and project file (working project files containing files to include in project)
|
||||
*.dsw
|
||||
*.dsp
|
||||
|
||||
# Visual Studio 6 technical files
|
||||
*.ncb
|
||||
*.aps
|
||||
|
||||
# Visual Studio LightSwitch build output
|
||||
**/*.HTMLClient/GeneratedArtifacts
|
||||
**/*.DesktopClient/GeneratedArtifacts
|
||||
**/*.DesktopClient/ModelManifest.xml
|
||||
**/*.Server/GeneratedArtifacts
|
||||
**/*.Server/ModelManifest.xml
|
||||
_Pvt_Extensions
|
||||
|
||||
# Paket dependency manager
|
||||
.paket/paket.exe
|
||||
paket-files/
|
||||
|
||||
# FAKE - F# Make
|
||||
.fake/
|
||||
|
||||
# CodeRush personal settings
|
||||
.cr/personal
|
||||
|
||||
# Python Tools for Visual Studio (PTVS)
|
||||
__pycache__/
|
||||
*.pyc
|
||||
|
||||
# Cake - Uncomment if you are using it
|
||||
# tools/**
|
||||
# !tools/packages.config
|
||||
|
||||
# Tabs Studio
|
||||
*.tss
|
||||
|
||||
# Telerik's JustMock configuration file
|
||||
*.jmconfig
|
||||
|
||||
# BizTalk build output
|
||||
*.btp.cs
|
||||
*.btm.cs
|
||||
*.odx.cs
|
||||
*.xsd.cs
|
||||
|
||||
# OpenCover UI analysis results
|
||||
OpenCover/
|
||||
|
||||
# Azure Stream Analytics local run output
|
||||
ASALocalRun/
|
||||
|
||||
# MSBuild Binary and Structured Log
|
||||
*.binlog
|
||||
|
||||
# NVidia Nsight GPU debugger configuration file
|
||||
*.nvuser
|
||||
|
||||
# MFractors (Xamarin productivity tool) working folder
|
||||
.mfractor/
|
||||
|
||||
# Local History for Visual Studio
|
||||
.localhistory/
|
||||
|
||||
# Visual Studio History (VSHistory) files
|
||||
.vshistory/
|
||||
|
||||
# BeatPulse healthcheck temp database
|
||||
healthchecksdb
|
||||
|
||||
# Backup folder for Package Reference Convert tool in Visual Studio 2017
|
||||
MigrationBackup/
|
||||
|
||||
# Ionide (cross platform F# VS Code tools) working folder
|
||||
.ionide/
|
||||
|
||||
# Fody - auto-generated XML schema
|
||||
FodyWeavers.xsd
|
||||
|
||||
# VS Code files for those working on multiple tools
|
||||
.vscode/*
|
||||
!.vscode/settings.json
|
||||
!.vscode/tasks.json
|
||||
!.vscode/launch.json
|
||||
!.vscode/extensions.json
|
||||
*.code-workspace
|
||||
|
||||
# Local History for Visual Studio Code
|
||||
.history/
|
||||
|
||||
# Windows Installer files from build outputs
|
||||
*.cab
|
||||
*.msi
|
||||
*.msix
|
||||
*.msm
|
||||
*.msp
|
||||
|
||||
# JetBrains Rider
|
||||
*.sln.iml
|
||||
|
@@ -4,8 +4,9 @@
|
||||
<OutputType>Exe</OutputType>
|
||||
<TargetFramework>net6.0</TargetFramework>
|
||||
<RootNamespace>C_</RootNamespace>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
<ImplicitUsings>disable</ImplicitUsings>
|
||||
<Nullable>enable</Nullable>
|
||||
</PropertyGroup>
|
||||
|
||||
</Project>
|
||||
|
||||
|
286
C#/Datastructures/BinaryTree/Tree.cs
Normal file
286
C#/Datastructures/BinaryTree/Tree.cs
Normal file
@@ -0,0 +1,286 @@
|
||||
using C_.Datastructures.LinkedList;
|
||||
using C_.Datastructures.Stack;
|
||||
using C_.Datastructures.Queue;
|
||||
using System;
|
||||
|
||||
namespace C_.Datastructures.BinaryTree
|
||||
{
|
||||
internal class Tree<T> where T:IComparable
|
||||
{
|
||||
public TreeNode<T>? Root { get; set; }
|
||||
public int Count { get; set; }
|
||||
|
||||
public static Tree<T> Create(){
|
||||
//Create a new Tree with no Head
|
||||
return new Tree<T>{
|
||||
Root = null,
|
||||
Count = 0
|
||||
};
|
||||
}
|
||||
|
||||
public static Tree<T> Create(T value){
|
||||
//Create a new Tree with Head
|
||||
return new Tree<T>{
|
||||
Root = TreeNode<T>.Create(value)
|
||||
};
|
||||
}
|
||||
|
||||
public void Add(T value)
|
||||
{//Add item to the correct position in the tree (Input cannot be null)
|
||||
Count++;
|
||||
|
||||
if (Root == default)
|
||||
{//If new node should become the root
|
||||
Root = TreeNode<T>.Create(value);
|
||||
return;
|
||||
}
|
||||
|
||||
//Find position to insert
|
||||
TreeNode<T> node = Root;
|
||||
node = Descend(value, node)!;
|
||||
|
||||
if (value.CompareTo(node.Value) < 0)
|
||||
{//Insert to left
|
||||
node.Left = TreeNode<T>.Create(value);
|
||||
return;
|
||||
}
|
||||
//Insert to right
|
||||
node.Right = TreeNode<T>.Create(value);
|
||||
return;
|
||||
}
|
||||
|
||||
public bool Delete(T value)
|
||||
{
|
||||
//Check if root of tree is null
|
||||
if (Count == 0)
|
||||
return false;
|
||||
|
||||
//Check if the only value is the root
|
||||
if (Count == 1)
|
||||
{
|
||||
if (Root!.Value!.Equals(value))
|
||||
{//If the only item is the one we are trying to delete
|
||||
Count = 0;
|
||||
Root = default;
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
//Stack to store the items leading up to and including the one we are trying to delete
|
||||
Stack<TreeNode<T>>? deletionStack;
|
||||
|
||||
//Search for item being deleted + Parents
|
||||
deletionStack = Find(value);
|
||||
|
||||
if (deletionStack == default)
|
||||
return false; //Item was not found
|
||||
|
||||
//Delete Item (replace) and replace pointers to retain integrity of tree
|
||||
|
||||
TreeNode<T>? node = deletionStack.Pop();
|
||||
|
||||
//stack to store the items leading up to the value that we will use to replace the node
|
||||
Stack<TreeNode<T>>? replacementStack = Min(node!.Right);
|
||||
|
||||
if (replacementStack == default)
|
||||
{//Nothing to the right of the value we are deleting
|
||||
if (deletionStack.Peek() != default)
|
||||
{//Parent adopts left hand side of node if present
|
||||
deletionStack.Pop()!.Left = node.Left;
|
||||
}
|
||||
|
||||
if (node.Left != default)
|
||||
{//Node adopts left value if no lower value to the right
|
||||
node.Value = node.Left!.Value;
|
||||
node.Left = node.Left.Left;
|
||||
}
|
||||
}
|
||||
else
|
||||
{//Replace the value + reorder nodes
|
||||
node.Value = replacementStack.Peek()!.Value;
|
||||
TreeNode<T>? replacementNode = replacementStack.Pop();
|
||||
|
||||
switch (replacementStack.GetCount())
|
||||
{//Determine what to do based on number of items in replacement stack
|
||||
case 1:
|
||||
node.Right = replacementNode!.Right;
|
||||
break;
|
||||
case >=2:
|
||||
replacementStack.Peek()!.Left = replacementNode!.Right;
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
Count--;
|
||||
return true;
|
||||
}
|
||||
|
||||
public LinkedList<T>? Traverse(TraversalType traversalType)
|
||||
{
|
||||
if (Root == default)
|
||||
return default;
|
||||
|
||||
LinkedList<T> list = LinkedList<T>.Create();
|
||||
|
||||
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);
|
||||
|
||||
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 static 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;
|
||||
|
||||
if (value.CompareTo(node.Value) < 0)
|
||||
{//Traverse Left
|
||||
return node.Left;
|
||||
}
|
||||
//Traverse Right
|
||||
return node.Right;
|
||||
}
|
||||
|
||||
private Stack<TreeNode<T>>? Find(T value)
|
||||
{//Return true if the item can be found within the tree
|
||||
if (Root == default || Root.Value!.Equals(default))
|
||||
return default;
|
||||
|
||||
Stack<TreeNode<T>>? stack = Stack<TreeNode<T>>.Create(Root);
|
||||
|
||||
while (stack.Peek() != default)
|
||||
{
|
||||
//Compare value at node to see if we are looking for the root item
|
||||
if (stack.Peek()!.Value!.Equals(value))
|
||||
return stack;
|
||||
|
||||
stack.Push(GetNext(value, stack.Peek()));
|
||||
}
|
||||
return default;
|
||||
}
|
||||
|
||||
private static Stack<TreeNode<T>>? Min(TreeNode<T>? node)
|
||||
{//Returns a Stack with the value on top being the minimum of the subtree
|
||||
|
||||
if(node == default)
|
||||
return default;
|
||||
|
||||
//Stack to store and be able to get the parent values
|
||||
Stack<TreeNode<T>> stack = Stack<TreeNode<T>>.Create(node);
|
||||
|
||||
while(true){
|
||||
if (stack.Peek()!.Left == default)
|
||||
return stack;
|
||||
|
||||
stack.Push(stack.Peek()!.Left);
|
||||
}
|
||||
}
|
||||
|
||||
private static Stack<TreeNode<T>>? Max(TreeNode<T>? node)
|
||||
{///Returns a Stack with the value on top being the maximum of the subtree
|
||||
if(node == default)
|
||||
return default;
|
||||
|
||||
//Stack to store and be able to get the parent values
|
||||
Stack<TreeNode<T>> stack = Stack<TreeNode<T>>.Create(node);
|
||||
|
||||
while(true){
|
||||
if (stack.Peek()!.Right == default)
|
||||
return stack;
|
||||
|
||||
stack.Push(stack.Peek()!.Right);
|
||||
}
|
||||
}
|
||||
|
||||
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
|
||||
if (current == default)
|
||||
return default;
|
||||
|
||||
TreeNode<T>? node;
|
||||
|
||||
node = Descend(value, GetNext(value, current));
|
||||
if (node == null)
|
||||
{
|
||||
return current;
|
||||
}
|
||||
return node;
|
||||
}
|
||||
}
|
||||
|
||||
public enum TraversalType
|
||||
{//Enum to allow for Traversal selection
|
||||
Inorder,
|
||||
Preorder,
|
||||
Postorder,
|
||||
Breadth
|
||||
}
|
||||
}
|
28
C#/Datastructures/BinaryTree/TreeNode.cs
Normal file
28
C#/Datastructures/BinaryTree/TreeNode.cs
Normal file
@@ -0,0 +1,28 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
using C_.Datastructures.Generic;
|
||||
|
||||
namespace C_.Datastructures.BinaryTree
|
||||
{
|
||||
internal class TreeNode<T> : DirectedNode<T, TreeNode<T>>
|
||||
{
|
||||
//All properties inherited from base class
|
||||
public static TreeNode<T> Create(T value){
|
||||
//Create a new node without any children
|
||||
return new TreeNode<T>{
|
||||
Value = value
|
||||
};
|
||||
}
|
||||
|
||||
public static TreeNode<T> Create(T value, TreeNode<T>? left, TreeNode<T>? right){
|
||||
//Create a new node with the option of having children
|
||||
return new TreeNode<T>{
|
||||
Value = value,
|
||||
Left = left,
|
||||
Right = right
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
214
C#/Datastructures/DoublyLinkedList/DoublyLinkedList.cs
Normal file
214
C#/Datastructures/DoublyLinkedList/DoublyLinkedList.cs
Normal file
@@ -0,0 +1,214 @@
|
||||
namespace C_.Datastructures.DoublyLinkedList
|
||||
{
|
||||
public class DoublyLinkedList<T>
|
||||
{
|
||||
internal DoublyLinkedListNode<T>? Head { get; set; } = default;
|
||||
internal DoublyLinkedListNode<T>? Tail { get; set; } = default;
|
||||
private int Count { get; set; } = 0;
|
||||
|
||||
public static DoublyLinkedList<T> Create()
|
||||
{
|
||||
//Create a new empty list
|
||||
return new DoublyLinkedList<T>();
|
||||
}
|
||||
|
||||
public static DoublyLinkedList<T> Create(T? value)
|
||||
{
|
||||
//Create a new Class with a single item
|
||||
return new DoublyLinkedList<T>()
|
||||
{
|
||||
Head = DoublyLinkedListNode<T>.Create(value, default, default),
|
||||
Count = 1
|
||||
};
|
||||
}
|
||||
|
||||
public static DoublyLinkedList<T> Create(DoublyLinkedList<T> list1, DoublyLinkedList<T> list2)
|
||||
{
|
||||
//Create a new list from 2 separate lists
|
||||
|
||||
DoublyLinkedList<T> list;
|
||||
|
||||
list = list1;
|
||||
if (list == default || list.Count == 0) return list2;
|
||||
|
||||
//Find end of list and append fist item of next list
|
||||
if (list2 == default || list.Count == 0) return list;
|
||||
|
||||
DoublyLinkedListNode<T>? end = list.Traverse();
|
||||
|
||||
//Connect up pointers at ajoining section
|
||||
end!.Next = list2!.Head;
|
||||
end!.Next!.Prev = end;
|
||||
end = list2.Tail;
|
||||
list.Count += list2!.Count;
|
||||
|
||||
return list;
|
||||
}
|
||||
|
||||
public T? this[int i]
|
||||
{
|
||||
get
|
||||
{
|
||||
//Check Range
|
||||
if (i >= Count || i < 0) throw new System.Exception("Error! Index out of Bounds");
|
||||
|
||||
//Return Value
|
||||
DoublyLinkedListNode<T>? node = Traverse(i);
|
||||
if (node != default) return node.Value;
|
||||
return default;
|
||||
}
|
||||
set
|
||||
{
|
||||
//Check Range
|
||||
if (i >= Count || i < 0) throw new System.Exception("Error! Index out of Bounds");
|
||||
|
||||
//Change Value
|
||||
DoublyLinkedListNode<T>? node = Traverse(i);
|
||||
node!.Value = value;
|
||||
}
|
||||
}
|
||||
public void Append(T? value)
|
||||
{
|
||||
Count++;
|
||||
|
||||
//Set head to new item if list is empty
|
||||
if (Head == null)
|
||||
{//Append item to front of list (End as well)
|
||||
Head = DoublyLinkedListNode<T>.Create(value, default, default);
|
||||
Tail = Head;
|
||||
return;
|
||||
}
|
||||
|
||||
//Append item to the end of the list
|
||||
Tail!.Next = DoublyLinkedListNode<T>.Create(value, default, Tail);
|
||||
Tail = Tail.Next;
|
||||
}
|
||||
|
||||
public void Insert(int index, T? value)
|
||||
{
|
||||
Count++;
|
||||
if (index > Count || index < 0) throw new System.Exception("Error! Index outside of Bounds");
|
||||
|
||||
//Set head to new item if list is empty
|
||||
if (index == 0 || Head == null)
|
||||
{
|
||||
Head = DoublyLinkedListNode<T>.Create(value, Head, default);
|
||||
Tail = Head;
|
||||
return;
|
||||
}
|
||||
|
||||
//Set tail to new item if index is the end
|
||||
if (index == Count - 1)
|
||||
{
|
||||
//Decrement count as it will be be re-incremented once appended
|
||||
Count--;
|
||||
Append(value);
|
||||
return;
|
||||
}
|
||||
|
||||
//Fetch point in list and add new item
|
||||
DoublyLinkedListNode<T>? node = Traverse(index - 1);
|
||||
node!.Next = DoublyLinkedListNode<T>.Create(value, node.Next, node);
|
||||
|
||||
//Create backlink in the list
|
||||
if (node.Next.Next != default)
|
||||
node.Next.Next.Prev = node.Next;
|
||||
}
|
||||
|
||||
public void Delete(int index)
|
||||
{
|
||||
Count--;
|
||||
if (index > Count || index < 0) throw new System.Exception("Error! Index outside of Bounds");
|
||||
|
||||
//Check if we are trying to reference the first item
|
||||
if (index == 0 && Head != default)
|
||||
{
|
||||
Head = Head!.Next;
|
||||
if (Head != default)
|
||||
Head.Prev = default;
|
||||
return;
|
||||
}
|
||||
|
||||
//Set tail to new item if index is the end
|
||||
if (index == Count && Tail != default)
|
||||
{
|
||||
Tail = Tail!.Prev;
|
||||
|
||||
if (Tail != default)
|
||||
Tail.Next = default;
|
||||
return;
|
||||
}
|
||||
|
||||
DoublyLinkedListNode<T>? node = Traverse(index - 1);
|
||||
|
||||
node!.Next = node.Next!.Next;
|
||||
|
||||
//Connect item after to the the item before the node we are deleting
|
||||
if (node.Next != default)
|
||||
{
|
||||
node.Next.Prev = node;
|
||||
}
|
||||
}
|
||||
|
||||
public int GetCount()
|
||||
{//Return the number of items in the list
|
||||
return Count;
|
||||
}
|
||||
|
||||
private DoublyLinkedListNode<T>? Traverse()
|
||||
{
|
||||
//Return the final item in the list
|
||||
return Tail;
|
||||
}
|
||||
|
||||
private DoublyLinkedListNode<T>? Traverse(int i)
|
||||
{
|
||||
//Determine whether to start at the start or end of the list
|
||||
int direction = 1;
|
||||
DoublyLinkedListNode<T>? node = Head;
|
||||
|
||||
if (i > (Count / 2))
|
||||
{
|
||||
//reverse direction of search
|
||||
direction = -1;
|
||||
node = Tail;
|
||||
//i becomes the amount of hops left to reach the item
|
||||
i = Count - i - 1;
|
||||
}
|
||||
|
||||
if (node != null)
|
||||
{
|
||||
|
||||
//continue to given point in the list ('i' hops)
|
||||
for (int x = 0; x < i; x++)
|
||||
{
|
||||
if (direction == 1)
|
||||
{//Going forwards
|
||||
node = node!.Next;
|
||||
}
|
||||
else
|
||||
{
|
||||
node = node!.Prev;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
return node;
|
||||
}
|
||||
private DoublyLinkedListNode<T>? Next(DoublyLinkedListNode<T> current)
|
||||
{
|
||||
if (current != default)
|
||||
return current.Next;
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
private DoublyLinkedListNode<T>? Prev(DoublyLinkedListNode<T> current)
|
||||
{
|
||||
if (current != default)
|
||||
return current.Prev;
|
||||
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
19
C#/Datastructures/DoublyLinkedList/DoublyLinkedListNode.cs
Normal file
19
C#/Datastructures/DoublyLinkedList/DoublyLinkedListNode.cs
Normal file
@@ -0,0 +1,19 @@
|
||||
using C_.Datastructures.Generic;
|
||||
|
||||
namespace C_.Datastructures.DoublyLinkedList
|
||||
{
|
||||
internal class DoublyLinkedListNode<T> : UndirectedNode<T, DoublyLinkedListNode<T>>
|
||||
{//Inherits from Node
|
||||
internal DoublyLinkedListNode<T>? Prev { get; set; } = default;
|
||||
|
||||
public static DoublyLinkedListNode<T> Create(T? value, DoublyLinkedListNode<T>? next, DoublyLinkedListNode<T>? prev)
|
||||
{
|
||||
return new DoublyLinkedListNode<T>
|
||||
{
|
||||
Value = value,
|
||||
Next = next,
|
||||
Prev = prev
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
15
C#/Datastructures/Generic/DirectedNode.cs
Normal file
15
C#/Datastructures/Generic/DirectedNode.cs
Normal file
@@ -0,0 +1,15 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace C_.Datastructures.Generic
|
||||
{
|
||||
abstract internal class DirectedNode<T, NodeType>
|
||||
{
|
||||
public T? Value { get; set; } = default;
|
||||
internal NodeType? Left { get; set; } = default;
|
||||
internal NodeType? Right { get; set; } = default;
|
||||
}
|
||||
}
|
10
C#/Datastructures/Generic/UndirectedNode.cs
Normal file
10
C#/Datastructures/Generic/UndirectedNode.cs
Normal file
@@ -0,0 +1,10 @@
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace C_.Datastructures.Generic
|
||||
{
|
||||
abstract internal class UndirectedNode<T, NodeType>
|
||||
{//Generic Node type that every other type inherits from
|
||||
public T? Value { get; set; } = default;
|
||||
internal NodeType? Next { get; set; } = default;
|
||||
}
|
||||
}
|
@@ -1,88 +0,0 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
using C_.Datastructures.Nodes;
|
||||
|
||||
namespace C_.Datastructures
|
||||
{
|
||||
public class LinkedList<T>
|
||||
{
|
||||
public Node<T>? Head { get; set; } = default(Node<T>);
|
||||
private int Count { get; set; } = 0;
|
||||
|
||||
|
||||
public static LinkedList<T> Create(){
|
||||
//Create a new empty list
|
||||
return new LinkedList<T>();
|
||||
}
|
||||
|
||||
public static LinkedList<T> Create(T value){
|
||||
//Create a new Class with a single item
|
||||
return new LinkedList<T>(){
|
||||
Head = Node<T>.Create(value, null),
|
||||
Count = 1
|
||||
};
|
||||
}
|
||||
|
||||
public static LinkedList<T> Create(LinkedList<T> list1, LinkedList<T> list2){
|
||||
//Append a previous list to a new List
|
||||
return null;
|
||||
}
|
||||
|
||||
|
||||
|
||||
public void Add(T value){
|
||||
Node<T> newItem = Node<T>.Create(value, default(Node<T>));
|
||||
Count++;
|
||||
|
||||
//Set head to new item if list is empty
|
||||
if (Head == null)
|
||||
{
|
||||
Head = newItem;
|
||||
return;
|
||||
}
|
||||
|
||||
//Find last item in list
|
||||
Node<T>? end = Head;
|
||||
if (end != null)
|
||||
{
|
||||
end = Traverse();
|
||||
}
|
||||
|
||||
//Append item to end
|
||||
end!.Next = new Node<T>{
|
||||
Value = value,
|
||||
Next = default(Node<T>)
|
||||
};
|
||||
}
|
||||
|
||||
private Node<T>? Traverse(){
|
||||
//Start at Head of list
|
||||
Node<T>? node = Head;
|
||||
if (node == null)
|
||||
{
|
||||
//continue to end of list
|
||||
while (node!.Next != default(Node<T>))
|
||||
{
|
||||
node = (Node<T>)node.Next;
|
||||
}
|
||||
}
|
||||
return node;
|
||||
}
|
||||
|
||||
private Node<T> Traverse(Node<T> start){
|
||||
//Start at given point in list
|
||||
Node<T> node = start;
|
||||
if (node == null)
|
||||
{
|
||||
//Continue to end of list
|
||||
while (node!.Next != default(Node<T>))
|
||||
{
|
||||
node = (Node<T>)node.Next;
|
||||
}
|
||||
}
|
||||
return node;
|
||||
}
|
||||
}
|
||||
}
|
161
C#/Datastructures/LinkedList/LinkedList.cs
Normal file
161
C#/Datastructures/LinkedList/LinkedList.cs
Normal file
@@ -0,0 +1,161 @@
|
||||
namespace C_.Datastructures.LinkedList
|
||||
{
|
||||
public class LinkedList<T>
|
||||
{
|
||||
internal LinkedListNode<T>? Head { get; set; } = default;
|
||||
private int Count { get; set; } = 0;
|
||||
|
||||
public static LinkedList<T> Create()
|
||||
{
|
||||
//Create a new empty list
|
||||
return new LinkedList<T>();
|
||||
}
|
||||
|
||||
public static LinkedList<T> Create(T? value)
|
||||
{
|
||||
//Create a new Class with a single item
|
||||
return new LinkedList<T>()
|
||||
{
|
||||
Head = LinkedListNode<T>.Create(value, null),
|
||||
Count = 1
|
||||
};
|
||||
}
|
||||
|
||||
public static LinkedList<T> Create(LinkedList<T> list1, LinkedList<T> list2)
|
||||
{
|
||||
//Append a previous list to a new List
|
||||
LinkedList<T> list;
|
||||
|
||||
list = list1;
|
||||
if (list == null || list.Count == 0) return list2;
|
||||
|
||||
//Find end of list and append fist item of next list
|
||||
if (list2 == null || list.Count == 0) return list;
|
||||
|
||||
LinkedListNode<T>? end = list.Traverse();
|
||||
end!.Next = list2!.Head;
|
||||
list.Count += list2!.Count;
|
||||
|
||||
return list;
|
||||
}
|
||||
|
||||
public T? this[int i]
|
||||
{
|
||||
get
|
||||
{
|
||||
//Check Range
|
||||
if (i >= Count || i < 0) throw new System.Exception("Error! Index out of Bounds");
|
||||
|
||||
//Return Value
|
||||
LinkedListNode<T>? node = Traverse(i);
|
||||
if (node != null) return node.Value;
|
||||
return default;
|
||||
}
|
||||
set
|
||||
{
|
||||
//Check Range
|
||||
if (i >= Count || i < 0) throw new System.Exception("Error! Index out of Bounds");
|
||||
|
||||
//Change Value
|
||||
LinkedListNode<T>? node = Traverse(i);
|
||||
node!.Value = value;
|
||||
}
|
||||
}
|
||||
|
||||
public void Append(T? value)
|
||||
{
|
||||
//Create new node
|
||||
Count++;
|
||||
LinkedListNode<T> newItem = LinkedListNode<T>.Create(value, default);
|
||||
|
||||
//Set head to new item if list is empty
|
||||
if (Head == null)
|
||||
{
|
||||
Head = newItem;
|
||||
return;
|
||||
}
|
||||
|
||||
//Find last item in list
|
||||
LinkedListNode<T>? end = Head;
|
||||
if (end != null)
|
||||
{
|
||||
end = Traverse();
|
||||
}
|
||||
|
||||
//Append item to end
|
||||
end!.Next = newItem;
|
||||
}
|
||||
|
||||
public void Insert(int index, T? value)
|
||||
{
|
||||
Count++;
|
||||
if (index > Count || index < 0) throw new System.Exception("Error! Index outside of Bounds");
|
||||
|
||||
//Set head to new item if list is empty
|
||||
if (index == 0 || Head == null)
|
||||
{
|
||||
Head = LinkedListNode<T>.Create(value, Head);
|
||||
return;
|
||||
}
|
||||
|
||||
//Fetch point in list at which item will be added
|
||||
LinkedListNode<T>? node = Traverse(index - 1);
|
||||
|
||||
node!.Next = LinkedListNode<T>.Create(value, node!.Next);
|
||||
}
|
||||
|
||||
public void Delete(int index)
|
||||
{
|
||||
Count--;
|
||||
if (index > Count || index < 0) throw new System.Exception("Error! Index outside of Bounds");
|
||||
|
||||
//Check if we are trying to reference the first item
|
||||
if (index == 0 && Head != default)
|
||||
{
|
||||
Head = Head!.Next;
|
||||
return;
|
||||
}
|
||||
|
||||
//Find node before the one we are trying to delete and then remove / relink
|
||||
LinkedListNode<T>? node = Traverse(index - 1);
|
||||
|
||||
node!.Next = node.Next!.Next;
|
||||
}
|
||||
|
||||
public int GetCount()
|
||||
{//Return the number of items in the list
|
||||
return Count;
|
||||
}
|
||||
|
||||
private LinkedListNode<T>? Traverse()
|
||||
{
|
||||
//Start at Head of list
|
||||
LinkedListNode<T>? node = Head;
|
||||
if (node != null)
|
||||
{
|
||||
//continue to end of list
|
||||
while (node!.Next != default)
|
||||
{
|
||||
node = (LinkedListNode<T>)node.Next;
|
||||
}
|
||||
}
|
||||
return node;
|
||||
}
|
||||
|
||||
private LinkedListNode<T>? Traverse(int i)
|
||||
{
|
||||
//Start at given point in list
|
||||
LinkedListNode<T>? node = Head;
|
||||
if (node != null || i == 0)
|
||||
{
|
||||
//Continue to end of list
|
||||
for (int j = 0; j < i; j++)
|
||||
{
|
||||
if (node!.Next == null) return null;
|
||||
node = (LinkedListNode<T>)node.Next;
|
||||
}
|
||||
}
|
||||
return node;
|
||||
}
|
||||
}
|
||||
}
|
16
C#/Datastructures/LinkedList/LinkedListNode.cs
Normal file
16
C#/Datastructures/LinkedList/LinkedListNode.cs
Normal file
@@ -0,0 +1,16 @@
|
||||
using C_.Datastructures.Generic;
|
||||
|
||||
namespace C_.Datastructures.LinkedList
|
||||
{
|
||||
internal class LinkedListNode<T> : UndirectedNode<T, LinkedListNode<T>>
|
||||
{//Inherits from Node
|
||||
public static LinkedListNode<T> Create(T? value, LinkedListNode<T>? next)
|
||||
{
|
||||
return new LinkedListNode<T>
|
||||
{
|
||||
Value = value,
|
||||
Next = next
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
@@ -1,26 +0,0 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace C_.Datastructures.Nodes
|
||||
{
|
||||
public interface iNode<T>
|
||||
{
|
||||
public T? Value { get; set; }
|
||||
public iNode<T>? Next { get; set; }
|
||||
|
||||
}
|
||||
|
||||
public class Node<T> : iNode<T>{
|
||||
public T? Value { get; set; } = default(T);
|
||||
public iNode<T>? Next { get; set; } = default(Node<T>);
|
||||
|
||||
public static Node<T> Create(T? value, Node<T>? next){
|
||||
return new Node<T>{
|
||||
Value = value,
|
||||
Next = next
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
75
C#/Datastructures/Queue/Queue.cs
Normal file
75
C#/Datastructures/Queue/Queue.cs
Normal file
@@ -0,0 +1,75 @@
|
||||
namespace C_.Datastructures.Queue
|
||||
{
|
||||
internal class Queue<T>
|
||||
{
|
||||
internal QueueNode<T>? Head { get; set; }
|
||||
internal QueueNode<T>? Tail { get; set; }
|
||||
private int Count { get; set; } = 0;
|
||||
|
||||
public static Queue<T> Create()
|
||||
{
|
||||
//Create a new queue without a head / tail
|
||||
return new Queue<T>();
|
||||
}
|
||||
|
||||
public static Queue<T> Create(T? value)
|
||||
{
|
||||
//Create a new Queue with a head + tail
|
||||
QueueNode<T> newNode = QueueNode<T>.Create(value, default);
|
||||
return new Queue<T>
|
||||
{
|
||||
Head = newNode,
|
||||
Tail = newNode,
|
||||
Count = 1
|
||||
};
|
||||
}
|
||||
|
||||
public void Push(T? value)
|
||||
{
|
||||
//Add an Item to the end of the Queue
|
||||
Count++;
|
||||
if (Count > 1)
|
||||
{
|
||||
Tail!.Next = QueueNode<T>.Create(value, default);
|
||||
Tail = Tail!.Next;
|
||||
return;
|
||||
}
|
||||
Head = QueueNode<T>.Create(value, default);
|
||||
Tail = Head;
|
||||
return;
|
||||
}
|
||||
|
||||
public T? Pop()
|
||||
{
|
||||
//Take the item off the front of the queue
|
||||
T? value = default;
|
||||
if (Count > 0)
|
||||
{//Assign the default value if there are any items left in the Queue
|
||||
Count--;
|
||||
value = Head!.Value;
|
||||
Head = Head.Next;
|
||||
if (Count == 0)
|
||||
{
|
||||
Head = default;
|
||||
Tail = Head;
|
||||
}
|
||||
}
|
||||
return value;
|
||||
}
|
||||
|
||||
public T? Peek()
|
||||
{
|
||||
//View item at the front of the Queue
|
||||
if (Count > 0)
|
||||
{
|
||||
return Head!.Value;
|
||||
}
|
||||
return default;
|
||||
}
|
||||
|
||||
public int GetCount()
|
||||
{//Return the number of items in the list
|
||||
return Count;
|
||||
}
|
||||
}
|
||||
}
|
16
C#/Datastructures/Queue/QueueNode.cs
Normal file
16
C#/Datastructures/Queue/QueueNode.cs
Normal file
@@ -0,0 +1,16 @@
|
||||
using C_.Datastructures.Generic;
|
||||
|
||||
namespace C_.Datastructures.Queue
|
||||
{
|
||||
internal class QueueNode<T> : UndirectedNode<T, QueueNode<T>>
|
||||
{
|
||||
public static QueueNode<T> Create(T? value, QueueNode<T>? next)
|
||||
{
|
||||
return new QueueNode<T>
|
||||
{
|
||||
Value = value,
|
||||
Next = next
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
64
C#/Datastructures/Stack/Stack.cs
Normal file
64
C#/Datastructures/Stack/Stack.cs
Normal file
@@ -0,0 +1,64 @@
|
||||
namespace C_.Datastructures.Stack
|
||||
{
|
||||
public class Stack<T>
|
||||
{
|
||||
internal StackNode<T>? Head { get; set; }
|
||||
private int Count { get; set; } = 0;
|
||||
|
||||
public static Stack<T> Create()
|
||||
{
|
||||
//Create a new stack without a head
|
||||
return new Stack<T>();
|
||||
}
|
||||
|
||||
public static Stack<T> Create(T? value)
|
||||
{
|
||||
//Create a new Stack with a head
|
||||
return new Stack<T>
|
||||
{
|
||||
Head = StackNode<T>.Create(value, default),
|
||||
Count = 1
|
||||
};
|
||||
}
|
||||
|
||||
public void Push(T? value)
|
||||
{
|
||||
//Add an Item to the top of the stack
|
||||
Count++;
|
||||
Head = StackNode<T>.Create(value, Head);
|
||||
return;
|
||||
}
|
||||
|
||||
public T? Pop()
|
||||
{
|
||||
//Take the item off of the top of the stack
|
||||
T? value = default;
|
||||
if (Count > 0)
|
||||
{//Assign the default value if there are any items left in the stack
|
||||
Count--;
|
||||
value = Head!.Value;
|
||||
Head = Head.Next;
|
||||
if (Count == 0)
|
||||
{
|
||||
Head = default;
|
||||
}
|
||||
}
|
||||
return value;
|
||||
}
|
||||
|
||||
public T? Peek()
|
||||
{
|
||||
//View item on top of the stack
|
||||
if (Count > 0)
|
||||
{
|
||||
return Head!.Value;
|
||||
}
|
||||
return default;
|
||||
}
|
||||
|
||||
public int GetCount()
|
||||
{//Return the number of items in the list
|
||||
return Count;
|
||||
}
|
||||
}
|
||||
}
|
16
C#/Datastructures/Stack/StackNode.cs
Normal file
16
C#/Datastructures/Stack/StackNode.cs
Normal file
@@ -0,0 +1,16 @@
|
||||
using C_.Datastructures.Generic;
|
||||
|
||||
namespace C_.Datastructures.Stack
|
||||
{
|
||||
internal class StackNode<T> : UndirectedNode<T, StackNode<T>>
|
||||
{//Inherits from Node
|
||||
public static StackNode<T> Create(T? value, StackNode<T>? next)
|
||||
{
|
||||
return new StackNode<T>
|
||||
{
|
||||
Value = value,
|
||||
Next = next
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
140
C#/Program.cs
140
C#/Program.cs
@@ -1,2 +1,140 @@
|
||||
// See https://aka.ms/new-console-template for more information
|
||||
using System;
|
||||
using C_.Datastructures.BinaryTree;
|
||||
|
||||
// See https://aka.ms/new-console-template for more information
|
||||
Console.WriteLine("Hello, World!");
|
||||
|
||||
|
||||
//LinkedList<int> list = new LinkedList<int>();
|
||||
|
||||
//list.Append(1);
|
||||
//list.Append(2);
|
||||
//list.Append(3);
|
||||
//list.Append(4);
|
||||
//list.Append(5);
|
||||
//list.Append(6);
|
||||
|
||||
//list.Delete(5);
|
||||
//list.Delete(2);
|
||||
//list.Delete(0);
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
// DoublyLinkedList<int> list = new DoublyLinkedList<int>();
|
||||
|
||||
// list.Append(1);
|
||||
// list.Append(2);
|
||||
// list.Append(3);
|
||||
// list.Append(4);
|
||||
// list.Append(5);
|
||||
// list.Append(6);
|
||||
// list.Insert(6, 1);
|
||||
|
||||
// Console.Write($"{list[0]} ");
|
||||
// Console.Write($"{list[1]} ");
|
||||
// Console.Write($"{list[2]} ");
|
||||
// Console.Write($"{list[3]} ");
|
||||
// Console.Write($"{list[4]} ");
|
||||
// Console.Write($"{list[5]} ");
|
||||
// Console.Write($"{list[6]} \n");
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
//Stack<int> stack = Stack<int>.Create();
|
||||
|
||||
//stack.Push(1);
|
||||
//stack.Push(2);
|
||||
//stack.Push(3);
|
||||
//stack.Push(4);
|
||||
//stack.Push(5);
|
||||
//stack.Push(6);
|
||||
|
||||
//Console.WriteLine(stack.Peek());
|
||||
|
||||
//stack.Pop();
|
||||
//stack.Push(7);
|
||||
|
||||
//stack.Pop();
|
||||
//stack.Pop();
|
||||
//stack.Pop();
|
||||
//stack.Pop();
|
||||
//stack.Pop();
|
||||
//stack.Pop();
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
//Queue<int> queue = Queue<int>.Create();
|
||||
|
||||
//queue.Push(1);
|
||||
//queue.Push(2);
|
||||
//queue.Push(3);
|
||||
//queue.Push(4);
|
||||
//queue.Push(5);
|
||||
//queue.Push(6);
|
||||
|
||||
//Console.WriteLine(queue.Peek());
|
||||
|
||||
//queue.Pop();
|
||||
//queue.Push(7);
|
||||
|
||||
//queue.Pop();
|
||||
//queue.Pop();
|
||||
//queue.Pop();
|
||||
//queue.Pop();
|
||||
//queue.Pop();
|
||||
//queue.Pop();
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
Tree<int> tree = new Tree<int>();
|
||||
|
||||
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);
|
||||
//tree.Delete(5);
|
||||
|
||||
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();
|
||||
|
||||
|
||||
|
||||
|
25
C#/c#.sln
Normal file
25
C#/c#.sln
Normal file
@@ -0,0 +1,25 @@
|
||||
|
||||
Microsoft Visual Studio Solution File, Format Version 12.00
|
||||
# Visual Studio Version 17
|
||||
VisualStudioVersion = 17.1.32210.238
|
||||
MinimumVisualStudioVersion = 10.0.40219.1
|
||||
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "C#", "C#.csproj", "{CBD2D14D-2DD3-473C-A11D-23867ACD7056}"
|
||||
EndProject
|
||||
Global
|
||||
GlobalSection(SolutionConfigurationPlatforms) = preSolution
|
||||
Debug|Any CPU = Debug|Any CPU
|
||||
Release|Any CPU = Release|Any CPU
|
||||
EndGlobalSection
|
||||
GlobalSection(ProjectConfigurationPlatforms) = postSolution
|
||||
{CBD2D14D-2DD3-473C-A11D-23867ACD7056}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
|
||||
{CBD2D14D-2DD3-473C-A11D-23867ACD7056}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||
{CBD2D14D-2DD3-473C-A11D-23867ACD7056}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||
{CBD2D14D-2DD3-473C-A11D-23867ACD7056}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||
EndGlobalSection
|
||||
GlobalSection(SolutionProperties) = preSolution
|
||||
HideSolutionNode = FALSE
|
||||
EndGlobalSection
|
||||
GlobalSection(ExtensibilityGlobals) = postSolution
|
||||
SolutionGuid = {3B808CFA-3B43-4C7E-AD79-0CF0CA3FB904}
|
||||
EndGlobalSection
|
||||
EndGlobal
|
Reference in New Issue
Block a user