mirror of
https://github.com/mrfluffy-dev/lpr.git
synced 2026-01-17 02:10:33 +00:00
Lets gooooo
This commit is contained in:
69
BusinessLogic/BinaryTree.cs
Normal file
69
BusinessLogic/BinaryTree.cs
Normal file
@@ -0,0 +1,69 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace BusinessLogic
|
||||
{
|
||||
public class BinaryTree
|
||||
{
|
||||
public BinaryTreeNode Root { get; set; }
|
||||
|
||||
public void Add(List<List<List<double>>> subProblem, List<List<List<double>>> parentProblem = null)
|
||||
{
|
||||
BinaryTreeNode newNode = new BinaryTreeNode();
|
||||
newNode.Data = subProblem;
|
||||
|
||||
if (Root == null)
|
||||
{
|
||||
Root = newNode;
|
||||
return;
|
||||
}
|
||||
|
||||
if (parentProblem != null)
|
||||
{
|
||||
BinaryTreeNode parentNode = Find(parentProblem);
|
||||
if (parentNode != null && parentNode.LeftNode == null)
|
||||
{
|
||||
parentNode.LeftNode = newNode;
|
||||
return;
|
||||
}
|
||||
|
||||
if (parentNode != null && parentNode.RightNode == null)
|
||||
{
|
||||
parentNode.RightNode = newNode;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public BinaryTreeNode Find(List<List<List<double>>> subProblem)
|
||||
{
|
||||
return this.Find(subProblem, this.Root);
|
||||
}
|
||||
|
||||
private BinaryTreeNode Find(List<List<List<double>>> subProblem, BinaryTreeNode parent)
|
||||
{
|
||||
if (parent == null)
|
||||
return null;
|
||||
|
||||
if (parent.Data == subProblem)
|
||||
{
|
||||
return parent;
|
||||
}
|
||||
|
||||
return Find(subProblem, parent.LeftNode) == null ? Find(subProblem, parent.RightNode) : Find(subProblem, parent.LeftNode);
|
||||
}
|
||||
|
||||
public int GetHeight(BinaryTreeNode root)
|
||||
{
|
||||
if (root == null)
|
||||
return 0;
|
||||
|
||||
int leftHeight = GetHeight(root.LeftNode);
|
||||
int rightHeight = GetHeight(root.RightNode);
|
||||
|
||||
return leftHeight > rightHeight ? leftHeight + 1 : rightHeight + 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user