Compare commits
6 Commits
master
...
developmen
Author | SHA1 | Date | |
---|---|---|---|
0dfb647f8f | |||
e2157e7093 | |||
78ac09c4e9 | |||
a37a81a2bf | |||
e66473bb25 | |||
e462929612 |
44
Datastructures/Heap/Heap.cs
Normal file
44
Datastructures/Heap/Heap.cs
Normal file
@ -0,0 +1,44 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace C_.Datastructures.Heap
|
||||
{
|
||||
internal class Heap<T> where T:IComparable
|
||||
{
|
||||
internal HeapNode<T>? Root { get; set; }
|
||||
private int Count { get; set; }
|
||||
|
||||
public static Heap<T> Create(){
|
||||
return new Heap<T>{
|
||||
Root = null,
|
||||
Count = 0
|
||||
};
|
||||
}
|
||||
|
||||
public static Heap<T> Create(T value){
|
||||
return new Heap<T>{
|
||||
Root = HeapNode<T>.Create(value),
|
||||
Count = 1
|
||||
};
|
||||
}
|
||||
|
||||
public void Add(T value){
|
||||
Count++;
|
||||
|
||||
if (Root == default)
|
||||
{//If the new node needs to be added to the top of the heap
|
||||
Root = HeapNode<T>.Create(value);
|
||||
return;
|
||||
}
|
||||
|
||||
//If the new node can be placed in a subchild
|
||||
HeapNode<T> node = Root;
|
||||
|
||||
while(node.Left != default){
|
||||
node = node.Left;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
38
Datastructures/Heap/HeapNode.cs
Normal file
38
Datastructures/Heap/HeapNode.cs
Normal file
@ -0,0 +1,38 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using C_.Datastructures.Generic;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace C_.Datastructures.Heap
|
||||
{
|
||||
internal class HeapNode<T> : DirectedNode<T, HeapNode<T>>
|
||||
{
|
||||
internal int LeftWeight { get; set; }
|
||||
internal int RightWeight { get; set; }
|
||||
|
||||
//All properties inherited from base class
|
||||
public static HeapNode<T> Create(T value)
|
||||
{
|
||||
//Create a new node without any children
|
||||
return new HeapNode<T>
|
||||
{
|
||||
Value = value
|
||||
};
|
||||
}
|
||||
|
||||
public static HeapNode<T> Create(T value, HeapNode<T>? left, HeapNode<T>? right)
|
||||
{
|
||||
//Create a new node with the option of having children
|
||||
return new HeapNode<T>
|
||||
{
|
||||
Value = value,
|
||||
Left = left,
|
||||
LeftWeight = (left != default) ? 1 : 0,
|
||||
Right = right,
|
||||
RightWeight = (right != default) ? 1 : 0
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
@ -1,5 +1,7 @@
|
||||
using System;
|
||||
using C_.Datastructures.DoublyLinkedList;
|
||||
using C_.Datastructures.BinaryTree;
|
||||
using C_.Datastructures.Heap;
|
||||
|
||||
// See https://aka.ms/new-console-template for more information
|
||||
Console.WriteLine("Hello, World!");
|
||||
@ -37,6 +39,8 @@ Console.WriteLine("Hello, World!");
|
||||
// list.Append(6);
|
||||
// list.Insert(6, 1);
|
||||
|
||||
// list.Delete(0);
|
||||
|
||||
// Console.Write($"{list[0]} ");
|
||||
// Console.Write($"{list[1]} ");
|
||||
// Console.Write($"{list[2]} ");
|
||||
@ -107,34 +111,43 @@ Console.WriteLine("Hello, World!");
|
||||
|
||||
|
||||
|
||||
//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);
|
||||
|
||||
|
||||
|
||||
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);
|
||||
|
||||
var x = HeapNode<int>.Create(1, null, new HeapNode<int> { Left = default, Right = default });
|
||||
|
||||
Console.ReadLine();
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
@ -1,6 +1,6 @@
|
||||
# DataStructures
|
||||
|
||||
List of DataStructures implemented in C#, Go And C++
|
||||
List of DataStructures implemented in C#
|
||||
|
||||
## What is Included
|
||||
|
||||
@ -11,7 +11,7 @@ Doubly Linked List
|
||||
Stack
|
||||
Queue
|
||||
Binary Tree
|
||||
Heap
|
||||
Heap ~
|
||||
|
||||
### Algorithms
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user