- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我在使用以下代码片段时遇到问题:
using System;
using System.Collections.Generic;
using System.Text;
namespace trees_by_firas
{
class Program
{
static void Main(string[] args)
{
BinarySearchTree t = new BinarySearchTree();
t.insert(ref t.root, 10);
t.insert(ref t.root, 5);
t.insert(ref t.root, 6);
t.insert(ref t.root, 17);
t.insert(ref t.root, 2);
t.insert(ref t.root, 3);
BinarySearchTree.print(t.root);
Console.WriteLine("--------------------");
Console.WriteLine(t.FindMax());
Console.WriteLine(t.FindMin());
Console.WriteLine("--------------------");
Console.WriteLine(t.CountLeaves());
Console.WriteLine(t.CountNodes());
}
public class TreeNode
{
public int n;
public TreeNode _left;
public TreeNode _right;
public TreeNode(int n, TreeNode _left, TreeNode _right)
{
this.n = n;
this._left = _left;
this._right = _right;
}
public void DisplayNode()
{
Console.Write(n);
}
}
public class BinarySearchTree
{
public TreeNode root;
public BinarySearchTree()
{
root = null;
}
public void insert(ref TreeNode root, int x)
{
if (root == null)
{
root = new TreeNode(x, null, null);
}
else
if (x < root.n)
insert(ref root._left, x);
else
insert(ref root._right, x);
}
public int FindMin()
{
TreeNode current = root;
while (current._left != null)
current = current._left;
return current.n;
}
public int FindMax()
{
TreeNode current = root;
while (current._right != null)
current = current._right;
return current.n;
}
public TreeNode Find(int key)
{
TreeNode current = root;
while (current.n != key)
{
if (key < current.n)
current = current._left;
else
current = current._right;
if (current == null)
return null;
}
return current;
}
public void InOrder(ref TreeNode root)
{
if (root != null)
{
InOrder(ref root._left);
root.DisplayNode();
InOrder(ref root._right);
}
}
public int CountNodes()
{
int count = 1; // me!
if (root._left != null)
count += _left.CountNodes();
if (root._right != null)
count += _right.CountNodes();
return count;
}
public int CountLeaves()
{
int count = (root._left == null && root._right == null) ? 1 : 0;
if (root._left != null)
count += _left.CountLeaves();
if (root._right != null)
count += _right.CountLeaves();
return count;
}
public static void print(TreeNode root)
{
if (root != null)
{
print(root._left);
Console.WriteLine(root.n.ToString());
print(root._right);
}
}
}
}
}
我收到以下错误:
Error 1 The name '_left' does not exist in the current context
// on the countnodes & countleaves
Error 2 The name '_right' does not exist in the current context
// on the countnodes & countleaves
关于如何修复这些错误有什么想法吗?
最佳答案
_left 和_right 是TreeNode 中的字段。您正在尝试使用它们,就好像它们是 BinarySearchTree 的一部分一样。我相信你可以在它们前面加上 root.
:
public int CountNodes()
{
int count = 1; // me!
if (root._left != null)
count += root._left.CountNodes();
if (root._right != null)
count += root._right.CountNodes();
return count;
}
public int CountLeaves()
{
int count = (root._left == null && root._right == null) ? 1 : 0;
if (root._left != null)
count += root._left.CountLeaves();
if (root._right != null)
count += root._right.CountLeaves();
return count;
}
关于c# - 为什么我在二叉搜索树中找不到 left 和 right?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/406791/
我有一个 Segment 类和一个这样的段数组: private static class Segment { int number, type; Segment(in
我在 SO 中看到一些创建 multilanguage websites in JavaScript 的好建议包括 this article on JavaScript internationaliz
我们有浏览器前缀或黑客 (for Google and Safari) text-align: -webkit-right; (for Firefox) text-align:
过去几天我一直在关注这个问题,我正处于需要寻求帮助的地步。 http://cub.northnodes.com/index.php/about/mission/ 我需要立即捐赠 列一直 float 到
When I press right ctrl, I want the right shift the text will align right. When I press left ctrl le
我已经将右侧的列拆分为顶部和底部。在每个部分中,我在执行以下操作时遇到问题:我希望顶部占据左列高度的 50%,底部占据左列高度的另外 50%。 +-------------------+-------
我知道这个问题的标题很糟糕。对不起。 我有四个 div similar to this .我想要做的只是让 div 编号 2 和 4 之间的垂直空间被删除,而不改变 HTML 的结构。是否可以仅使用
我将表格设置为 100% 宽度。我会添加一个带有 php 的随机 div,有时会充满广告。我希望广告 div 位于表格的右侧和内容。我希望表格位于左侧,但仍为 100% 左右,它将填充广告 div 左
这个问题在这里已经有了答案: Bootstrap align navbar items to the right (24 个答案) 关闭 5 年前。
.floatright { float: right;margin: 0 0 10px 10px;clear: right;width:60px; height:60px; } Lorem
我正在尝试将 td 中的某些内容右对齐。 align="right"有效,但 text-align:right 无效。这是一个 jsfiddle显示这两种情况的示例。除了右对齐右列外,这两种情况是相同
在设计网站时,您认为用于特定任务的最佳图像格式是什么? 在试图找出用于特定任务的格式时,我总是发现自己处于两难境地……例如,我应该全面使用 .jpg 吗?或者,我何时以及为什么应该使用 .png? 例
我是一个 MySQL 新手,今天我尝试设置一个超过 5 行的 MySQL 调用。我不断收到语法错误,我尝试修复了几个小时,但我不知道问题出在哪里。这是代码: USE myDatabase; DELIM
这让我发疯。我有一个 div float 到另一个 div 的右侧,如下所示: Current Membership: 我有以下 css 规则: div#container { f
我有以下代码片段,它会产生不需要的“填充”区域,而填充为零。如何避免这个区域? 代码 div.left { background-color: red; max-width: 25%; f
在 C++ 中,表达式 left() = right() 求值 right() left() 按那个顺序。 right() 先行,正如已讨论过的 here. 我想不出让 right() 先走的理由。你
我有一个很小的菜单列表,当鼠标靠近时它应该会增长。在其原始状态下,菜单是右对齐的,悬停时每第二个元素向右移动并左对齐以为增加的高度腾出空间(参见 JSFiddle )。 ul { font-siz
td.myclass{ width: 6em; text-align: right; padding-right: 2em; } 如您所见,我希望单元格中的文本右对齐,距离单元
你怎么能看到 http://jsfiddle.net/73wst/ 我想在停止下开始,但我不知道如何设置它的样式。 我的 HTML: Stop Start 我的 CSS: .sta
一个大的内部 div 在一个小的外部 div 中,并且外部 div 溢出自动。但是为什么没有内部 div margin-right 和外部 div padding-right? html
我是一名优秀的程序员,十分优秀!