Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Kirill karamysh #44

Open
wants to merge 24 commits into
base: Kirill_Karamysh
Choose a base branch
from
Open
Prev Previous commit
Next Next commit
Task1Module5
  • Loading branch information
Kir0Ka committed Jun 3, 2022
commit ececa9e8f685e025892c49f6da914de9549b8188
139 changes: 139 additions & 0 deletions CourseApp/Module5/Branchtree
Original file line number Diff line number Diff line change
@@ -0,0 +1,139 @@
using System;
using System.Collections.Generic;

namespace CourseApp.Module5.Task_1
{
public class BinarTreeBranch
{
private Node root;
private List<int> size;

public static void BinaryTreeBranchMethod()
{
string s = Console.ReadLine();

string[] sValues = s.Split(' ');

var tree = new BinarTreeBranch();

for (int i = 0; i < sValues.Length - 1; i++)
{
tree.Insert(int.Parse(sValues[i]));
}

tree.FindLastElem();
}

public void Insert(int n)
{
root = InnerInsert(n, root);
}

public List<int> GetValues()
{
size = new List<int>();
InnerTraversal(root);
return size;
}

private bool InnerFind(int n, Node root)
{
if (root == null)
{
return false;
}

if (n == root.Data)
{
return true;
}
else if (n < root.Data)
{
return InnerFind(n, root.Left);
}
else
{
return InnerFind(n, root.Right);
}
}

public static void BinaryTreeMethod()
{
throw new NotImplementedException();
}

private bool Find(int n)
{
return InnerFind(n, root);
}

private void FindLastElem()
{
LastElem(root);
}

private void LastElem(Node value)
{
if (value == null)
{
return;
}

LastElem(value.Left);

if ((value.Left != null && value.Right == null) || (value.Right != null && value.Left == null))
{
Console.WriteLine(value.Data);
}

LastElem(value.Right);
}

private Node InnerInsert(int n, Node root)
{
if (root == null)
{
return new Node(n);
}

if (root.Data > n)
{
root.Left = InnerInsert(n, root.Left);
}
else if (root.Data < n)
{
root.Right = InnerInsert(n, root.Right);
}

return root;
}

private void InnerTraversal(Node node)
{
if (node == null)
{
return;
}

InnerTraversal(node.Left);
size.Add(node.Data);
InnerTraversal(node.Right);
}

internal class Node
{
public Node(int n)
{
Data = n;
Left = null;
Right = null;
}

public Node Left { get; set; }

public Node Right { get; set; }

public int Data { get; set; }
}
}
}