C# 二叉树中的最低公共祖先,读取输入和算法
我是实习生,有一个问题,我不能独自解决这个问题。所以请帮帮我。我找到了很多话题,但我找不到解决方案。 我刚开始学C#,我不知道该怎么做。我知道这是一个简单的工作,但我真的需要理解和解决它。我试图做一些事情,但这只是一些代码。我用一些值做了二叉树,有一个节点类和print方法。C# 二叉树中的最低公共祖先,读取输入和算法,c#,algorithm,tree,binary-tree,C#,Algorithm,Tree,Binary Tree,我是实习生,有一个问题,我不能独自解决这个问题。所以请帮帮我。我找到了很多话题,但我找不到解决方案。 我刚开始学C#,我不知道该怎么做。我知道这是一个简单的工作,但我真的需要理解和解决它。我试图做一些事情,但这只是一些代码。我用一些值做了二叉树,有一个节点类和print方法。 请告诉我如何编写一个可以从控制台读取树的代码,因为我不想有任何硬记录。然后如何找到一个最低的共同祖先——我看到了BFS和DFS算法,所以也许我能找到一些东西,但我不确定。 关于这一点我读了很多书,但我不能解释很多事情。她
请告诉我如何编写一个可以从控制台读取树的代码,因为我不想有任何硬记录。然后如何找到一个最低的共同祖先——我看到了BFS和DFS算法,所以也许我能找到一些东西,但我不确定。
关于这一点我读了很多书,但我不能解释很多事情。她 以下是我的代码:
class Program
{
static void Main(string[] args)
{
var binatyTree = new BinaryTree<int>(1,
new BinaryTree<int>(2,
new BinaryTree<int>(4),
new BinaryTree<int>(5)),
new BinaryTree<int>(3,
new BinaryTree<int>(6,
new BinaryTree<int>(9),
new BinaryTree<int>(10)),
new BinaryTree<int>(7))
);
Console.WriteLine("Binary Tree:");
binatyTree.Print();
}
}
所以,我真的需要理解每一个连接,所以请你为我解释和帮助。这是我的二叉树代码
using System;
namespace MyBinaryTree
{
// Creates a binary tree
// Finds the lowest common ancestor in a binary tree
class МainSolution
{
static void Main(string[] args)
{
// Initialises binary tree view
var btView = new ViewBinaryTree();
btView.BinaryTreeView();
// Initialises new binary tree
BinarySearchTree tree = new BinarySearchTree();
// Reads the desired number of nodes
Console.Write("Enter how many nodes you want: ");
int number = int.Parse(Console.ReadLine());
Console.WriteLine();
// Reads the nodes' values
for (int i = 1; i <= number; i++)
{
Console.Write($"Enter name of {i} node: ");
string nodeName = Console.ReadLine().ToUpper();
tree.Insert(nodeName);
}
// Lowest common ancestor - reads the two required values
// Checks the values
// Prints the lowest common ancestor
bool isValid = true;
while (isValid)
{
Console.WriteLine();
Console.Write("Please enter the first node value: ");
string nameOne = Console.ReadLine().ToUpper();
Console.Write("Please enter the second node value: ");
string nameTwo = Console.ReadLine().ToUpper();
if (tree.Find(nameOne) == true && tree.Find(nameTwo) == true)
{
Console.WriteLine();
var result = tree.SearchLCA(tree.root, nameOne, nameTwo);
Console.WriteLine($"Lowest common ancestor: {result} ");
Console.WriteLine();
isValid = false;
}
else
{
Console.WriteLine();
Console.WriteLine("Please enter a valid name!");
}
}
}
}
}
这是我的逻辑
// Creates a binary tree.
class BinarySearchTree
{
public Node root; // Creates a root node.
public BinarySearchTree()
{
this.root = null;
}
// Adds a node in the binary tree.
public void Insert(string inputValue)
{
Node newNode = new Node(inputValue); // Creates a new node.
if (root == null) // If the tree is empty, inserts the new node as root
{
root = newNode;
return;
}
//Determins where to place the new node in the binary tree.
Node current = root;
Node parent = null;
while (true)
{
parent = current;
if (inputValue.CompareTo(current.Data) < 0)
{
current = current.LeftChild;
if (current == null)
{
parent.LeftChild = newNode;
return;
}
}
else
{
current = current.RightChild;
if (current == null)
{
parent.RightChild = newNode;
return;
}
}
}
}
// Checks if the node exists in the binary tree
public bool Find(string inputValue)
{
Node current = root;
while (current != null)
{
if (current.Data.Equals(inputValue))
{
return true;
}
else if (current.Data.CompareTo(inputValue) > 0)
{
current = current.LeftChild;
}
else
{
current = current.RightChild;
}
}
return false;
}
// Creates a method to find the lowest common ancestor(LCA).
public object SearchLCA(Node searchTree, string compareValue1, string compareValue2)
{
if (searchTree == null)
{
return null;
}
if (searchTree.Data.CompareTo(compareValue1) > 0 && searchTree.Data.CompareTo(compareValue2) > 0)
{
return SearchLCA(searchTree.LeftChild, compareValue1, compareValue2);
}
if (searchTree.Data.CompareTo(compareValue1) < 0 && searchTree.Data.CompareTo(compareValue2) < 0)
{
return SearchLCA(searchTree.RightChild, compareValue1, compareValue2);
}
return searchTree.Data;
}
}
//创建一个二叉树。
类二进制搜索树
{
公共节点根;//创建根节点。
公共二进制搜索树()
{
this.root=null;
}
//在二叉树中添加一个节点。
公共void插入(字符串inputValue)
{
Node newNode=新节点(inputValue);//创建一个新节点。
if(root==null)//如果树为空,则将新节点作为根插入
{
根=新节点;
返回;
}
//确定在二叉树中放置新节点的位置。
节点电流=根;
节点父节点=null;
while(true)
{
父项=当前;
如果(inputValue.CompareTo(current.Data)<0)
{
current=current.LeftChild;
如果(当前==null)
{
parent.LeftChild=newNode;
返回;
}
}
其他的
{
current=current.RightChild;
如果(当前==null)
{
parent.RightChild=newNode;
返回;
}
}
}
}
//检查二叉树中是否存在该节点
公共布尔查找(字符串输入值)
{
节点电流=根;
while(当前!=null)
{
if(当前数据等于(输入值))
{
返回true;
}
else if(当前数据比较(inputValue)>0)
{
current=current.LeftChild;
}
其他的
{
current=current.RightChild;
}
}
返回false;
}
//创建一个查找最低共同祖先(LCA)的方法。
公共对象SearchLCA(节点searchTree、字符串compareValue1、字符串compareValue2)
{
if(searchTree==null)
{
返回null;
}
if(searchTree.Data.CompareTo(compareValue1)>0&&searchTree.Data.CompareTo(compareValue2)>0)
{
返回SearchLCA(searchTree.LeftChild、compareValue1、compareValue2);
}
if(searchTree.Data.CompareTo(compareValue1)<0&&searchTree.Data.CompareTo(compareValue2)<0)
{
返回SearchLCA(searchTree.RightChild、compareValue1、compareValue2);
}
返回searchTree.Data;
}
}
“谢谢!”请分开提问。他们没有血缘关系。最低的共同祖先很难找到。预期的运行时间是多少?
using System;
namespace MyBinaryTree
{
// Creates a binary tree
// Finds the lowest common ancestor in a binary tree
class МainSolution
{
static void Main(string[] args)
{
// Initialises binary tree view
var btView = new ViewBinaryTree();
btView.BinaryTreeView();
// Initialises new binary tree
BinarySearchTree tree = new BinarySearchTree();
// Reads the desired number of nodes
Console.Write("Enter how many nodes you want: ");
int number = int.Parse(Console.ReadLine());
Console.WriteLine();
// Reads the nodes' values
for (int i = 1; i <= number; i++)
{
Console.Write($"Enter name of {i} node: ");
string nodeName = Console.ReadLine().ToUpper();
tree.Insert(nodeName);
}
// Lowest common ancestor - reads the two required values
// Checks the values
// Prints the lowest common ancestor
bool isValid = true;
while (isValid)
{
Console.WriteLine();
Console.Write("Please enter the first node value: ");
string nameOne = Console.ReadLine().ToUpper();
Console.Write("Please enter the second node value: ");
string nameTwo = Console.ReadLine().ToUpper();
if (tree.Find(nameOne) == true && tree.Find(nameTwo) == true)
{
Console.WriteLine();
var result = tree.SearchLCA(tree.root, nameOne, nameTwo);
Console.WriteLine($"Lowest common ancestor: {result} ");
Console.WriteLine();
isValid = false;
}
else
{
Console.WriteLine();
Console.WriteLine("Please enter a valid name!");
}
}
}
}
}
// Creates the node structure
class Node
{
public string Data { get; set; }
public Node RightChild { get; set; }
public Node LeftChild { get; set; }
public Node(string data, Node left = null, Node right = null)
{
this.Data = data;
this.LeftChild = left;
this.RightChild = right;
}
}
// Creates a binary tree.
class BinarySearchTree
{
public Node root; // Creates a root node.
public BinarySearchTree()
{
this.root = null;
}
// Adds a node in the binary tree.
public void Insert(string inputValue)
{
Node newNode = new Node(inputValue); // Creates a new node.
if (root == null) // If the tree is empty, inserts the new node as root
{
root = newNode;
return;
}
//Determins where to place the new node in the binary tree.
Node current = root;
Node parent = null;
while (true)
{
parent = current;
if (inputValue.CompareTo(current.Data) < 0)
{
current = current.LeftChild;
if (current == null)
{
parent.LeftChild = newNode;
return;
}
}
else
{
current = current.RightChild;
if (current == null)
{
parent.RightChild = newNode;
return;
}
}
}
}
// Checks if the node exists in the binary tree
public bool Find(string inputValue)
{
Node current = root;
while (current != null)
{
if (current.Data.Equals(inputValue))
{
return true;
}
else if (current.Data.CompareTo(inputValue) > 0)
{
current = current.LeftChild;
}
else
{
current = current.RightChild;
}
}
return false;
}
// Creates a method to find the lowest common ancestor(LCA).
public object SearchLCA(Node searchTree, string compareValue1, string compareValue2)
{
if (searchTree == null)
{
return null;
}
if (searchTree.Data.CompareTo(compareValue1) > 0 && searchTree.Data.CompareTo(compareValue2) > 0)
{
return SearchLCA(searchTree.LeftChild, compareValue1, compareValue2);
}
if (searchTree.Data.CompareTo(compareValue1) < 0 && searchTree.Data.CompareTo(compareValue2) < 0)
{
return SearchLCA(searchTree.RightChild, compareValue1, compareValue2);
}
return searchTree.Data;
}
}