c# - 如何连接叶子并返回 BST 周围节点的列表
问题描述
这是我对 BST(二叉搜索树)的实现。谁能帮我创建两种方法,一种是连接叶子,另一种是返回树周围的节点列表,例如:连接叶子示例在这张图片中,它显示了叶子应该如何连接,应该存储在列表中的节点以及存储在 List 中的节点的方式需要以这种方式,其中根是第一个元素,然后向左向下传递到从右侧返回根的叶子。在我的示例中,它应该是 8(根)、3、1、4、7、13、14、10、8(根)。谢谢你!
**class Node
{
private int VL;
private int Niv;
public Node Parent, LC, RC;
public Node()
{
this.Parent = this.LC = this.RC = null;
this.Niv = -1;
}
public Node(int x)
{
this.VL = x;
this.Parent = this.LC = this.RC = null;
this.Niv = -1;
}
public int Vlera
{
get { return this.VL; }
set { this.VL = value; }
}
public int Niveli
{
get { return this.Niv; }
set { this.Niv = value; }
}
}
class BSTree
{
public Node Root;
private int MaxNiv;
public BSTree()
{
this.Root = null;
this.MaxNiv = -1;
}
public void Insert(int x)
{
Node tmp = new Node(x);
if (this.Root == null)
{
tmp.Niveli = 0;
this.Root = tmp;
}
else InsertNode(tmp);
if (tmp.Niveli > this.MaxNiv) MaxNiv = tmp.Niveli;
}
public void ConnectLeafs()
{
//TODO
}
public List<T> ReturnNodesAroundTheTree()
{
//TODO
}
public Node GoTo_Node(Node nd)
{
return GoTo_Node_Rec(this.Root, nd);
}
public Node GoTo_Node(int x)
{
return GoTo_Node_Rec(this.Root, x);
}
private Node GoTo_Node_Rec(Node root, Node nd)
{
if (root.Vlera == nd.Vlera) return root;
if (root.Vlera > nd.Vlera) return GoTo_Node_Rec(root.LC, nd);
else return GoTo_Node_Rec(root.RC, nd);
}
private Node GoTo_Node_Rec(Node root, int x)
{
if (root.Vlera == x) return root;
if (root.Vlera > x) return GoTo_Node_Rec(root.LC, x);
else return GoTo_Node_Rec(root.RC, x);
}
private void InsertNode(Node nd)
{
Node tmp = InsertRecNode(this.Root, nd.Vlera);
if (nd.Vlera >= tmp.Vlera) tmp.RC = nd;
else tmp.LC = nd;
nd.Parent = tmp;
nd.Niveli = nd.Parent.Niveli++;
//if (nd.Niveli > this.MaxNiv) MaxNiv = nd.Niveli;
}
private Node InsertRecNode(Node root, int x)
{
if (x >= root.Vlera)
if (root.RC != null) return InsertRecNode(root.RC, x);
else return root;
else
if (root.LC != null) return InsertRecNode(root.LC, x);
else return root;
}
private bool IsRoot(Node nd)
{
if (nd.Parent == null) return true;
return false;
}
private bool IsLeaf(Node nd)
{
if (nd.LC == null && nd.RC == null) return true;
return false;
}**
解决方案
这是执行此操作的简单方法。我创建了所有节点的列表。当您创建一个新节点时,将节点添加到列表中。请参阅下面的代码
class BSTree
{
public Node Root;
private int MaxNiv;
private List<Node> nodes = new List<Node>();
public BSTree()
{
this.Root = null;
this.MaxNiv = -1;
}
public void Insert(int x)
{
Node tmp = new Node(x);
nodes.Add(tmp);
if (this.Root == null)
{
tmp.Niveli = 0;
this.Root = tmp;
}
else InsertNode(tmp);
if (tmp.Niveli > this.MaxNiv) MaxNiv = tmp.Niveli;
}
public void ConnectLeafs()
{
//TODO
}
public List<Node> ReturnNodesAroundTheTree()
{
return nodes.Where(x => IsLeaf(x)).ToList();
}
}
推荐阅读
- wcf - WCF 服务未在服务器上显示元数据(返回 404),仅在本地有效
- c# - Changing the font size of an element programmatically inside a Viewbox works weird
- javascript - 如何将 JS 函数链接到 Bootstrap 文本输入模式?
- apache-kafka - Flink 处理带有解析错误的 Kafka 消息
- android - 如何在颤动中开发像谷歌照片这样的后台同步功能?
- mongodb - 在 MongoDB 上查询时将 String 类型转换为 Number 类型
- php - 如何在 Laravel 8 的数据库中显示每个菜单中的数据?
- ffmpeg - 在不中断 rtmp 流的情况下更新 ffmpeg 过滤器
- python - 获取 SSL:CERTIFICATE_VERIFY_FAILED 将代理与 python 请求一起使用时
- javascript - 如何在highcharts中动态图表向下钻取滚动条