Binary Tree Program
This commit is contained in:
parent
1168d0b934
commit
72ec29eafc
26
BinaryTree/BinaryTree/.vscode/launch.json
vendored
Normal file
26
BinaryTree/BinaryTree/.vscode/launch.json
vendored
Normal file
@ -0,0 +1,26 @@
|
||||
{
|
||||
"version": "0.2.0",
|
||||
"configurations": [
|
||||
{
|
||||
// Use IntelliSense to find out which attributes exist for C# debugging
|
||||
// Use hover for the description of the existing attributes
|
||||
// For further information visit https://github.com/OmniSharp/omnisharp-vscode/blob/master/debugger-launchjson.md
|
||||
"name": ".NET Core Launch (console)",
|
||||
"type": "coreclr",
|
||||
"request": "launch",
|
||||
"preLaunchTask": "build",
|
||||
// If you have changed target frameworks, make sure to update the program path.
|
||||
"program": "${workspaceFolder}/bin/Debug/net5.0/BinaryTree.dll",
|
||||
"args": [],
|
||||
"cwd": "${workspaceFolder}",
|
||||
// For more information about the 'console' field, see https://aka.ms/VSCode-CS-LaunchJson-Console
|
||||
"console": "internalConsole",
|
||||
"stopAtEntry": false
|
||||
},
|
||||
{
|
||||
"name": ".NET Core Attach",
|
||||
"type": "coreclr",
|
||||
"request": "attach"
|
||||
}
|
||||
]
|
||||
}
|
42
BinaryTree/BinaryTree/.vscode/tasks.json
vendored
Normal file
42
BinaryTree/BinaryTree/.vscode/tasks.json
vendored
Normal file
@ -0,0 +1,42 @@
|
||||
{
|
||||
"version": "2.0.0",
|
||||
"tasks": [
|
||||
{
|
||||
"label": "build",
|
||||
"command": "dotnet",
|
||||
"type": "process",
|
||||
"args": [
|
||||
"build",
|
||||
"${workspaceFolder}/BinaryTree.csproj",
|
||||
"/property:GenerateFullPaths=true",
|
||||
"/consoleloggerparameters:NoSummary"
|
||||
],
|
||||
"problemMatcher": "$msCompile"
|
||||
},
|
||||
{
|
||||
"label": "publish",
|
||||
"command": "dotnet",
|
||||
"type": "process",
|
||||
"args": [
|
||||
"publish",
|
||||
"${workspaceFolder}/BinaryTree.csproj",
|
||||
"/property:GenerateFullPaths=true",
|
||||
"/consoleloggerparameters:NoSummary"
|
||||
],
|
||||
"problemMatcher": "$msCompile"
|
||||
},
|
||||
{
|
||||
"label": "watch",
|
||||
"command": "dotnet",
|
||||
"type": "process",
|
||||
"args": [
|
||||
"watch",
|
||||
"run",
|
||||
"${workspaceFolder}/BinaryTree.csproj",
|
||||
"/property:GenerateFullPaths=true",
|
||||
"/consoleloggerparameters:NoSummary"
|
||||
],
|
||||
"problemMatcher": "$msCompile"
|
||||
}
|
||||
]
|
||||
}
|
8
BinaryTree/BinaryTree/BinaryTree.csproj
Normal file
8
BinaryTree/BinaryTree/BinaryTree.csproj
Normal file
@ -0,0 +1,8 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<OutputType>Exe</OutputType>
|
||||
<TargetFramework>net5.0</TargetFramework>
|
||||
</PropertyGroup>
|
||||
|
||||
</Project>
|
20
BinaryTree/BinaryTree/Program.cs
Normal file
20
BinaryTree/BinaryTree/Program.cs
Normal file
@ -0,0 +1,20 @@
|
||||
using System;
|
||||
|
||||
namespace BinaryTree
|
||||
{
|
||||
class Program
|
||||
{
|
||||
static void Main(string[] args)
|
||||
{
|
||||
tree tree = new tree();
|
||||
tree.Add(5);
|
||||
tree.Add(7);
|
||||
tree.Add(6);
|
||||
tree.Add(8);
|
||||
tree.Add(3);
|
||||
tree.Add(1);
|
||||
tree.Add(4);
|
||||
|
||||
}
|
||||
}
|
||||
}
|
41
BinaryTree/BinaryTree/node.cs
Normal file
41
BinaryTree/BinaryTree/node.cs
Normal file
@ -0,0 +1,41 @@
|
||||
namespace BinaryTree
|
||||
{
|
||||
public class node
|
||||
{
|
||||
public dynamic Data { get; set; }
|
||||
private node Parent { get; set; }
|
||||
private node Left { get; set; }
|
||||
private node Right { get; set; }
|
||||
|
||||
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;
|
||||
|
||||
}
|
||||
|
||||
public node GetLeft(){
|
||||
return Left;
|
||||
}
|
||||
public void SetLeft(dynamic _Data, node _Parent){
|
||||
Left = new node(_Data, _Parent);
|
||||
}
|
||||
|
||||
|
||||
|
||||
public node GetRight(){
|
||||
return Right;
|
||||
}
|
||||
public void SetRight(dynamic _Data, node _Parent){
|
||||
Right = new node(_Data, _Parent);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
}
|
57
BinaryTree/BinaryTree/tree.cs
Normal file
57
BinaryTree/BinaryTree/tree.cs
Normal file
@ -0,0 +1,57 @@
|
||||
namespace BinaryTree
|
||||
{
|
||||
public class tree
|
||||
{
|
||||
public node Root { get; set; }
|
||||
private node Current { get; set; }
|
||||
|
||||
public tree(dynamic _Data = null){
|
||||
//New tree - Sets the Root of the tree to a new node
|
||||
Root = new node(_Data);
|
||||
}
|
||||
|
||||
public void Add(dynamic _Data){
|
||||
//If the Root of the List is not set.
|
||||
if (Root.Data == null)
|
||||
{
|
||||
Root.Data = _Data;
|
||||
return;
|
||||
}else{
|
||||
Current = Root;
|
||||
}
|
||||
|
||||
//traverse through the tree to the relevant point at which the item should be added
|
||||
while(true){
|
||||
var temp = Traverse(_Data, Current);
|
||||
|
||||
//If it is null, we are at a leaf node and we can stop traversing the tree
|
||||
if (temp == null)
|
||||
{
|
||||
break;
|
||||
}
|
||||
Current = temp;
|
||||
}
|
||||
|
||||
//Check whether we need to add the node to the left or right of
|
||||
if (_Data < Current.Data)
|
||||
{
|
||||
Current.SetLeft(_Data, Current);
|
||||
}else{
|
||||
Current.SetRight(_Data, Current);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
private node Traverse(dynamic _Data, node _Current){
|
||||
//Check whether to branch to the right or the left in the tree.
|
||||
if (_Data < _Current.Data)
|
||||
{
|
||||
return _Current.GetLeft();
|
||||
}
|
||||
return _Current.GetRight();
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user