首页 \ 问答 \ 如何在简单的二叉树中选择节点所在的哪一侧?(How to pick which side a node is on in a simple binary tree?)

如何在简单的二叉树中选择节点所在的哪一侧?(How to pick which side a node is on in a simple binary tree?)

在一个简单的二叉树中,我能够通过添加不可见节点和不可见边来使图形看起来正确,例如:

digraph
{
        vertex_1 [label="A"];
        vertex_2 [label="B"];
        vertex_2 -> vertex_1 [label="Left"];
}

产生:

在此处输入图像描述

至:

digraph
{
        vertex_1 [label="A"];
        vertex_2 [label="B"];
        vertex_0 [style=invis];
        vertex_2 -> vertex_1 [label="Left"];
        vertex_2 -> vertex_0 [style=invis];
}

产生:

在此处输入图像描述

但是当我尝试使用4节点图形(它最初是直接向上和向下)时,这就是我得到的:

digraph
{
        vertex_1 [label="A"];
        vertex_2 [label="B"];
        vertex_3 [label="F"];
        vertex_4 [label="G"];
        vertex_01 [style=invis];
        vertex_02 [style=invis];
        vertex_03 [style=invis];
        vertex_4 -> vertex_3 [label="Left"];
        vertex_3 -> vertex_1 [label="Left"];
        vertex_1 -> vertex_02 [style=invis];
        vertex_4 -> vertex_03 [style=invis];
        vertex_3 -> vertex_01 [style=invis];
        vertex_1 -> vertex_2 [label="Right"];
}

产生:

在此处输入图像描述

显然我希望B在A的右侧。我尝试将不可见边缘语句的顺序和实际边缘从B切换到A,就像哪一个先出现但是没有区别。 如何告诉程序将特定节点或边缘放在特定端?


In a simple binary tree, I was able to make the graph look right by adding invisible nodes and invisible edges, for instance from:

digraph
{
        vertex_1 [label="A"];
        vertex_2 [label="B"];
        vertex_2 -> vertex_1 [label="Left"];
}

which produces:

enter image description here

to:

digraph
{
        vertex_1 [label="A"];
        vertex_2 [label="B"];
        vertex_0 [style=invis];
        vertex_2 -> vertex_1 [label="Left"];
        vertex_2 -> vertex_0 [style=invis];
}

which produces:

enter image description here

But when I tried that trick with a 4 node graph (it was originally straight up and down) here is what I got:

digraph
{
        vertex_1 [label="A"];
        vertex_2 [label="B"];
        vertex_3 [label="F"];
        vertex_4 [label="G"];
        vertex_01 [style=invis];
        vertex_02 [style=invis];
        vertex_03 [style=invis];
        vertex_4 -> vertex_3 [label="Left"];
        vertex_3 -> vertex_1 [label="Left"];
        vertex_1 -> vertex_02 [style=invis];
        vertex_4 -> vertex_03 [style=invis];
        vertex_3 -> vertex_01 [style=invis];
        vertex_1 -> vertex_2 [label="Right"];
}

which produced:

enter image description here

Obviously I want B to be on the right side of A. I tried switching the order of the invisible edge statement and the actual edge from B to A, like which one came first but that make no difference. How can I tell the program to put a specific node or edge on a specific side?


原文:https://stackoverflow.com/questions/36785513
更新时间:2019-12-07 10:50

最满意答案

对于从左到右的顺序,节点创建顺序是相关的。 必须在其右兄弟之前创建左节点。

digraph
{
    vertex_1 [label="A"];
    vertex_02 [style=invis];
    vertex_2 [label="B"];
    vertex_3 [label="F"];
    vertex_4 [label="G"];
    vertex_01 [style=invis];
    vertex_03 [style=invis];
    vertex_4 -> vertex_3 [label="Left"];
    vertex_3 -> vertex_1 [label="Left"];
    vertex_1 -> vertex_02 [style=invis];
    vertex_4 -> vertex_03 [style=invis];
    vertex_3 -> vertex_01 [style=invis];
    vertex_1 -> vertex_2 [label="Right"];
}

在此处输入图像描述


For the order left to right the node creation order is relevant. A left node must be created before its right sibling.

digraph
{
    vertex_1 [label="A"];
    vertex_02 [style=invis];
    vertex_2 [label="B"];
    vertex_3 [label="F"];
    vertex_4 [label="G"];
    vertex_01 [style=invis];
    vertex_03 [style=invis];
    vertex_4 -> vertex_3 [label="Left"];
    vertex_3 -> vertex_1 [label="Left"];
    vertex_1 -> vertex_02 [style=invis];
    vertex_4 -> vertex_03 [style=invis];
    vertex_3 -> vertex_01 [style=invis];
    vertex_1 -> vertex_2 [label="Right"];
}

enter image description here

2016-04-22

相关问答

更多

退出线程时如何执行代码(How can I execute code when exiting a thread)

您可以将代码包装在您自己的代码中的单独线程中,该代码具有try / finally块,并从try调用“real” Runnable的run方法,如下所示: final Runnable realRunnable = ... // This is the actual logic of your thread (new Thread(new Runnable() { public void run() { try { realRunnable.run

HTML和CSS间距问题(HTML and CSS spacing issues)

p标签有一个默认的顶部边距(1em?) 尝试重置或使用重置样式表 p { margin-top : 0 } The p tag has a default top margin (of 1em?) try resetting this or use a reset stylesheet p { margin-top : 0 }

修剪switch语句(Trimming down a switch statement)

您可以使用以下代码,但是您必须确保该字符串与函数名称完全相同 switch (e.ClickedItem.Text.ToLower()) { case "find": Find find = new Find(customTextBox1); find.Show(); break; case "undo": case "redo": case "cut": case "copy": case "paste": case "selec

在主模板的编辑器中如何使solr搜索对象?(How to make object of solr search in editor of main template?)

您需要使用基本的Solr TypoScript。 You need to use the basic Solr TypoScript.

为什么我的C#-console应用程序在复制文件时因“内存不足”而异常?(Why does my C#-console application crash due to a 'Out of Memory'-exception when copying files?)

正如其他答案中所述,您可以使用using语句以正确的方式调用对象上的Dispose方法。 using (Image Billede = Bitmap.FromFile(file)){ ... } 但是,对于您的代码,您可能需要检查图像格式,因为如果(来自MSDN) Image.FromFile函数将引发OutOfMemoryException : 该文件没有有效的图像格式。 -要么- GDI +不支持文件的像素格式。 看看这个 As noted in other answers,

使用命名参数作为变量(Using named arguments as variables)

从我能做出来的事情你可以构造出密钥,然后传递一个arg来构造字典,然后将其包含在过滤器中 key = '%s_%s' % (kwargs['m0'], kwargs['m1']) result = Artists.objects.filter(**{key: arg}) From what I can make out you can construct the key and then pass in an arg to construct the dict, then include th

通过HTTP访问AEM 6.2错误日志(Accessing AEM 6.2 error logs over HTTP)

一位同事在聊天中为我解答了这个问题,所以我将它放在这里,以便将来更容易找到。 OSGi控制台中现在有一个整洁的实用程序,可以查看日志以及配置各种记录器。 你可以在http:// localhost:4502 / system / console / slinglog找到它 Appender选项卡提供指向可用于通过HTTP加载日志的各种日志文件的链接。 这是一个示例请求: http://localhost:4502/system/console/slinglog/tailer.txt?tail=1

分段错误:C ++使用lambda比较器对字符串向量进行排序(Segmentation Fault: C++ sort an string vector with lambda comparator)

您的比较功能不是有效的。 它在无反射性( comp(x,x)永远不会是真 )和不对称( 如果comp(x,y)为真,然后comp(y,x)必须为假 )两者都失败,两者都是严格弱排序哪些std::sort需要它的比较器。 operator<或operator>满足此要求。 但是operator<=和operator>=不。 如果您将比较函数更改为: auto comp = [](const string& first, const string& second)->bool { retur

相关文章

更多

最新问答

更多
  • asp.net任意用户信息(asp.net arbitrary user info)
  • 如何使用python计算docx文件中表中行的值(How to count the row's values in tables in docx file by using python)
  • MySQL:用户访问和数据库覆盖(MySQL: User access and DB overwriting)
  • 还有另一种“使用未分配的局部变量”的问题(Yet Another “Use of unassigned local variable 'whatever' ” question)
  • 开源证书颁发机构软件(Open source certificate authority software)
  • Rails中的迭代form_for是在create上添加模型的所有实例(Iteration in Rails form_for is adding all instances of model on create)
  • 如何扩展我的表视图单元格?(How to expand my table view cell?)
  • 如何使用SPARQL区分Thing和无生命对象(How to differentiate between a Thing and an inanimate object with SPARQL)
  • 在IdentityServer中,Client Secrets和Scope Secrets有什么区别?(In IdentityServer, what is the difference between Client Secrets and Scope Secrets?)
  • 如何在具有附加类时重写类(How do I override a class when it has a attached class)
  • 如何使用Git在Azure上部署C#,MVC4应用程序(How to deploy a C#, MVC4 application on Azure using Git)
  • Sitecore 7内容搜索爬网程序根目录之外的索引项(Sitecore 7 Content Search indexing items outside of crawler root)
  • 我应该在线课程使用utf-8编码吗?(Should I use utf-8 encoding for an online course?)
  • 如何在Cucumber-JS步骤定义中使用Node-mysql连接到MySQL?(How to connect to MySQL using Node-mysql in a Cucumber-JS step definition?)
  • 在MVC 4中的google.maps.LatLng(lat,lon)中将JSON字符串值分配给纬度和经度(Assign the JSON string value to Latitude and Longitude in google.maps.LatLng(lat,lon) in MVC 4)
  • awk:通过特定的分隔符删除字符串(awk: remove strings by specific delimiter)
  • 如何测试Vista的应用程序(How to test app for Vista)
  • Elasticsearch聚合器 - 缺失值的工作原理(Elasticsearch aggregators - How missing values work)
  • 绘制datetime.date熊猫(Plot datetime.date pandas)
  • PostgreSQL作为WSO2 EI和APIM + IS的数据源(PostgreSQL as datasource for WSO2 EI and APIM+IS)
  • 如何使用bash在postgres中运行alter table脚本(How to run alter table script in postgres using bash)
  • 可能使用PHP阻止整个美国州访问我的网站?(Might it be possible to block an entire US state from accessing my site, using PHP?)
  • restangular删除并输入错误网:: ERR_NAME_NOT_RESOLVED(restangular remove and put error net::ERR_NAME_NOT_RESOLVED)
  • 常见问题解答的Modx(Revolution)搜索功能(Modx(Revolution) search function for FAQs)
  • Rubymine如何使用远程口译员和Git?(How Does Rubymine Work With Remote Interpreters and Git?)
  • prepareForSegue和PerformSegueWithIdentifier发件人(prepareForSegue and PerformSegueWithIdentifier sender)
  • postgrsql与PowerShell无提示安装问题(postgresql silent installation issue with powershell)
  • 比较两个greps的输出(Comparing output from two greps)
  • 使用.NET RIA Data Services删除Silverlight 3中的数据(Deleting data in Silverlight 3 with .NET RIA Data Services)
  • 此行中AND运算符的含义(meaning of the AND operator in this line)