C# 帮助在C中编译通用AVL树(IEnumerator问题)#
我在尝试实现的AVL树中遇到了一些编译错误。 有些事把整个统计员都搞砸了。它编译得很好,直到我尝试实现一个helper类。我认为这与BTNode本身是一个私有嵌套类有关,但尝试将其公开只是为了看看会发生什么,但没有效果 我在这个问题上有点困惑,不应该有任何转换 任何帮助都将不胜感激 这是源代码,我已经注意到了编译错误的来源,并且已经分解了不同的嵌套类以便于阅读C# 帮助在C中编译通用AVL树(IEnumerator问题)#,c#,generics,ienumerator,avl-tree,C#,Generics,Ienumerator,Avl Tree,我在尝试实现的AVL树中遇到了一些编译错误。 有些事把整个统计员都搞砸了。它编译得很好,直到我尝试实现一个helper类。我认为这与BTNode本身是一个私有嵌套类有关,但尝试将其公开只是为了看看会发生什么,但没有效果 我在这个问题上有点困惑,不应该有任何转换 任何帮助都将不胜感激 这是源代码,我已经注意到了编译错误的来源,并且已经分解了不同的嵌套类以便于阅读 class AVLTree<TKey, TValue> : IEnumerable<TKey> where TK
class AVLTree<TKey, TValue> : IEnumerable<TKey> where TKey : IComparable<TKey>
{
#region nested classes
class-AVLTree:IEnumerable其中TKey:IComparable
{
#区域嵌套类
节点类--
#区域BTNode类
私有类BTNode,其中TKey:IComparable
{
#区域类变量
公共价值数据;
公钥;
公众内部高度;
公共因素;
公共基站向上;
公共BTNode左;
公共节点权;
#端区
#区域con/析构函数
//键和值,第一个节点的构造函数。
公共BTNode(TKey新密钥、TValue新数据)
{
键=新的_键;
数据=新数据;
高度=1;
balFactor=0;
}
//初始化后正常使用构造函数
//已经制作好了。
公共BTNode(TKey新建密钥、TValue新建数据、BTNode新建数据)
{
键=新的_键;
数据=新数据;
向上=新向上;
高度=1;
balFactor=0;
}
#端区
}
#端区
枚举器辅助类--
#区域枚举器类
私有类AVLEnumerator:IEnumerator,其中TKey:IComparable
{
#区域类变量
私有AVLTree AVLTreeEnum;
专用节点电流;
#端区
#区域con/析构函数
公共AVLEnumerator(AVLTree-toEnumerate)
{
AVLTreeEnum=toEnumerate;
电流=零;
}
#端区
#区域接口方法
//要移动到下一个的接口方法
//节点。
公共图书馆
{
BTNode-sendMe;
//如果current为null,则它位于树的开头,
//将当前节点设置为根的左子树中最左侧的节点。
如果(当前==null)
{
/*下面的“sendMe=”行出现错误--
*无法隐式转换类型
*“BSTs.AVLTree.BTNode”到
*'BSTs.AVLTree.BTNode'
*/
sendMe=AVLTreeEnum.root;
/*“当前=”行上有两个错误--
*匹配的最佳重载方法
*'BSTs.AVLTree.GetLeftMost
*(BSTs.AVLTree.BTNode)'
*有一些无效的参数
*
*及
*
*参数1:无法从转换
*“BSTs.AVLTree.BTNode”到
*'BSTs.AVLTree.BTNode'
*/
current=AVLTreeEnum.GetLeftMost(sendMe);
}
其他的
{
//如果我们能从当前位置向右移动,就得到最左边的节点
//当前。右左子树的。
if(current.right!=null)
{
sendMe=current.right;
/*关于“当前”的两个错误--
*匹配的最佳重载方法
*'BSTs.AVLTree.GetLeftMost
*(BSTs.AVLTree.AVLNode)'
*有一些无效的参数
*
*及
*
*参数1:无法从转换
*'BSTs.AVLTree.AVLNode'
*到“BSTs.AVLTree.AVLNode”
*/
current=AVLTreeEnum.GetLeftMost(sendMe);
}
其他的
{
//向上移动,直到找到大于当前值的值。
TKey currentValue=current.key;
while(当前!=null)
{
电流=电流。上升;
如果(当前!=null)
{
如果(当前键比较到(当前值)>=0)
{
打破
}
}
}
}
}
返回(当前!=null);
}
//重置枚举的接口方法
公共无效重置()
{
电流=零;
}
//返回当前键的接口属性
公钥电流
{
收到
{
如果(当前==null)
{
抛出新的InvalidOperationException(
“枚举数得到空值”);
}
返回current.key;
}
}
//接口非泛型方法
对象System.Collections.IEnumerator.Current
{
获取{返回this.Current;}
}
//接口方法,必须具有dispose。
公共空间处置()
{ }
#端区
}
#端区
#端区
--
#区域类变量
私有节点根;
私有整数大小;
#端区
#区域属性
公共整数大小
{get{返回大小;}}
#端区
#区域con/析构函数
公共AVLTree()
{size=0;}
~AVLTree()
{
root=null;
}
#端区
--
#区域接口实现
//IEnumerable接口
公共IEnumerator GetEnumerator()
#region BTNode class
private class BTNode<TKey, TValue> where TKey : IComparable<TKey>
{
#region class variables
public TValue data;
public TKey key;
public int height;
public int balFactor;
public BTNode<TKey, TValue> up;
public BTNode<TKey, TValue> left;
public BTNode<TKey, TValue> right;
#endregion
#region con/destructors
//Key and value, constructor for very first node.
public BTNode(TKey new_key, TValue new_data)
{
key = new_key;
data = new_data;
height = 1;
balFactor = 0;
}
//Normal use constructor after initial
//has been made.
public BTNode(TKey new_key, TValue new_data, BTNode<TKey, TValue> new_up)
{
key = new_key;
data = new_data;
up = new_up;
height = 1;
balFactor = 0;
}
#endregion
}
#endregion
#region Enumerator class
private class AVLEnumerator<TKey,TValue> : IEnumerator<TKey> where TKey : IComparable<TKey>
{
#region class variables
private AVLTree<TKey, TValue> AVLTreeEnum;
private BTNode<TKey, TValue> current;
#endregion
#region con/destructors
public AVLEnumerator(AVLTree<TKey, TValue> toEnumerate)
{
AVLTreeEnum = toEnumerate;
current = null;
}
#endregion
#region interface methods
//interface method to move to the next
//node.
public bool MoveNext()
{
BTNode<TKey, TValue> sendMe;
//If current is null, it's at start of tree,
//set current to leftmost node in left subtree of root.
if (current == null)
{
/* error below on 'sendMe =' line--
* Cannot implicitly convert type
* 'BSTs.AVLTree<TKey,TValue>.BTNode<TKey,TValue> ' to
* 'BSTs.AVLTree<TKey,TValue>.BTNode<TKey,TValue> '
*/
sendMe = AVLTreeEnum.root;
/* two errors on 'current =' line--
* The best overloaded method match for
* 'BSTs.AVLTree<TKey,TValue>.GetLeftMost
* (BSTs.AVLTree<TKey,TValue>.BTNode<TKey,TValue>)'
* has some invalid arguments
*
* and
*
* Argument 1: cannot convert from
* 'BSTs.AVLTree<TKey,TValue>.BTNode<TKey,TValue>' to
* 'BSTs.AVLTree<TKey,TValue>.BTNode<TKey,TValue>'
*/
current = AVLTreeEnum.GetLeftMost(sendMe);
}
else
{
//If we can go right from current, get leftmost node
//of current.rights left subtree.
if (current.right != null)
{
sendMe = current.right;
/* two errors on 'current =' --
* The best overloaded method match for
* 'BSTs.AVLTree<TKey,TValue>.GetLeftMost
* (BSTs.AVLTree<TKey,TValue>.AVLNode<TKey,TValue>)'
* has some invalid arguments
*
* and
*
* Argument 1: cannot convert from
* 'BSTs.AVLTree<TKey,TValue>.AVLNode<TKey,TValue>'
* to 'BSTs.AVLTree<TKey,TValue>.AVLNode<TKey,TValue>'
*/
current = AVLTreeEnum.GetLeftMost(sendMe);
}
else
{
//Move up until we find a value larger than current.
TKey currentValue = current.key;
while (current != null)
{
current = current.up;
if (current != null)
{
if (current.key.CompareTo(currentValue) >= 0)
{
break;
}
}
}
}
}
return (current != null);
}
//Interface method to reset enumeration
public void Reset()
{
current = null;
}
//Interface property to return current key
public TKey Current
{
get
{
if (current == null)
{
throw new InvalidOperationException(
"Enumerator got a null");
}
return current.key;
}
}
//interface non-generic method
Object System.Collections.IEnumerator.Current
{
get { return this.Current; }
}
//interface method, must have a dispose.
public void Dispose()
{ }
#endregion
}
#endregion
#endregion
#region class variables
private BTNode<TKey, TValue> root;
private int size;
#endregion
#region properties
public int Size
{ get { return size; } }
#endregion
#region con/destructors
public AVLTree()
{ size = 0; }
~AVLTree()
{
root = null;
}
#endregion
#region interface implementation
//Interface for IEnumerable<T>
public IEnumerator<TKey> GetEnumerator()
{
return new AVLEnumerator<TKey,TValue>(this);
}
//Interface for IEnumerable. Must be included w/ Ienumerable<T>
System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
{
return this.GetEnumerator();
}
#endregion
//This method will get the leftmost node of it's left subtree.
//used by the Enumeration class.
private BTNode<TKey, TValue> GetLeftMost(BTNode<TKey, TValue> currentNode)
{
while (currentNode.left != null)
{
currentNode = currentNode.left;
}
return currentNode;
}
private class AVLEnumerator : IEnumerator<TKey>
{
#region class variables
private AVLTree<TKey, TValue> AVLTreeEnum;
private BTNode<TKey, TValue> current;
#endregion