我在C#中有一个Windows From Application,我使用AVLTree并用类Country(string a, string b, string c, string d, string e, LinkedList<string> list)
的实例填充它。 Country类具有很少的属性,我需要在填充AVLTree后访问它们。该树包含InsertItem(T item)
,Delete(T item)
,InOrder(ref string buffer)
,PreOrder(ref string buffer)
,PostOrder(ref string buffer)
,Contains(T item)
,Count()
,{{1}等方法}和Height()
。创建类实例的数据来自Excel文件,我使用RemoveItem(T item)
循环来读取文件中的行,并在读取每一行后创建实例并使用foreach
方法填充树。 InsertItem(T item)
类会覆盖Country()
方法,因此我可以打印出国家/地区的名称,但我需要能够删除或手动编辑国家/地区信息。
这是国家类
ToString()
节点类:
namespace International_Trading_Data
{
class Country : IComparable
{
string name;
string gdpGrowth;
string inflation;
string tradeBalance;
string hdiRanking;
LinkedList<string> mainTradePartners;
public Country(string name, string gdpGrowth, string inflation, string tradeBalance, string hdiRanking, LinkedList<string> mainTradePartners)
{
this.name = name;
this.gdpGrowth = gdpGrowth;
this.inflation = inflation;
this.tradeBalance = tradeBalance;
this.hdiRanking = hdiRanking;
this.mainTradePartners = mainTradePartners;
}
public string Name
{
get { return name; }
set { name = value; }
}
public string GdpGrowth
{
get { return gdpGrowth; }
set { gdpGrowth = value; }
}
public string Inflation
{
get { return inflation; }
set { inflation = value; }
}
public string TradeBalance
{
get { return tradeBalance; }
set { tradeBalance = value; }
}
public string HdiRanking
{
get { return hdiRanking; }
set { hdiRanking = value; }
}
public LinkedList<string> MainTradePartners
{
get { return mainTradePartners; }
set { mainTradePartners = value; }
}
public int CompareTo(object obj)
{
Country temp = (Country)obj;
return name.CompareTo(temp.name);
}
public override string ToString()
{
return Name+"\n";
}
}
}
BinaryTree类:
namespace International_Trading_Data
{
class Node<T> where T : IComparable
{
private T data;
public Node<T> Left, Right;
private int balanceFactor = 0;
public Node(T item)
{
data = item;
Left = null;
Right = null;
}
public T Data
{
set { data = value; }
get { return data; }
}
public int BalanceFactor
{
set { balanceFactor = value; }
get { return balanceFactor; }
}
}
}
BinarySearchTree类:
namespace International_Trading_Data
{
class BinaryTree<T> where T : IComparable
{
protected Node<T> root;
public BinaryTree()
{
root = null;
}
public BinaryTree(Node<T> node)
{
root = node;
}
public void InOrder(ref string buffer)
{
inOrder(root, ref buffer);
}
private void inOrder(Node<T> tree, ref string buffer)
{
if (tree != null)
{
inOrder(tree.Left, ref buffer);
buffer += tree.Data.ToString();
inOrder(tree.Right, ref buffer);
}
}
public void PreOrder(ref string buffer)
{
preOrder(root, ref buffer);
}
private void preOrder(Node<T> tree, ref string buffer)
{
if (tree != null)
{
buffer += tree.Data.ToString();
preOrder(tree.Left, ref buffer);
preOrder(tree.Right, ref buffer);
}
}
public void PostOrder(ref string buffer)
{
postOrder(root, ref buffer);
}
private void postOrder(Node<T> tree, ref string buffer)
{
if (tree != null)
{
postOrder(tree.Left, ref buffer);
postOrder(tree.Right, ref buffer);
buffer += tree.Data.ToString();
}
}
}
}
AVLTree课程:
namespace International_Trading_Data
{
class BSTree<T> : BinaryTree<T> where T : IComparable
{
int c = 0;
public BSTree()
{
root = null;
}
public void InsertItem(T item)
{
insertItem(item, ref root);
}
private void insertItem(T item, ref Node<T> tree)
{
if (tree == null)
tree = new Node<T>(item);
else if (item.CompareTo(tree.Data) < 0)
insertItem(item, ref tree.Left);
else if (item.CompareTo(tree.Data) > 0)
insertItem(item, ref tree.Right);
}
public int Height()
{
return height(root);
}
protected int height(Node<T> tree)
{
if (root == null)
return 0;
else
{
if (tree != null)
{
return 1 + Math.Max(height(tree.Left), height(tree.Right));
}
else
{
return 0;
}
}
}
public int Count()
{
return count(root, ref c);
}
private int count(Node<T> tree, ref int c)
{
if (tree == null)
{
return 0;
}
else
{
count(tree.Left, ref c);
count(tree.Right, ref c);
c++;
return c;
}
}
public Boolean Contains(T item)
{
return contains(ref root, item);
}
private Boolean contains(ref Node<T> tree, T item)
{
if (root == null)
{
return false;
}
else if (tree != null)
{
if (item.CompareTo(tree.Data) < 0)
{
return contains(ref tree.Left, item);
}
else if (item.CompareTo(tree.Data) > 0)
{
return contains(ref tree.Right, item);
}
else
{
return true;
}
}
else
{
return false;
}
}
public void RemoveItem(T item)
{
removeItem(item, ref root);
}
private void removeItem(T item, ref Node<T> tree)
{
if (tree == null)
{
Console.WriteLine("The tree doesn't have items.");
}
if (tree != null)
{
if (item.CompareTo(tree.Data) < 0)
{
removeItem(item, ref tree.Left);
}
if (item.CompareTo(tree.Data) > 0)
{
removeItem(item, ref tree.Right);
}
if (item.CompareTo(tree.Data) == 0)
{
if (tree.Left == null)
{
tree = tree.Right;
}
else if (tree.Right == null)
{
tree = tree.Left;
}
else
{
T newRoot = leastItem(tree.Right);
tree.Data = newRoot;
removeItem(newRoot, ref tree.Right);
}
}
}
}
private T leastItem(Node<T> tree)
{
if (tree.Left == null)
return tree.Data;
else
return leastItem(tree.Left);
}
}
}
这就是我的全部。如果我使用调试器,我可以看到AVLTree及其所有节点与Country类的实例。
在namespace International_Trading_Data
{
class AVLTree<T> : BSTree<T>, IEnumerable<T> where T : IComparable
{
public new void InsertItem(T item)
{
insertItem(item, ref root);
}
private void insertItem(T item, ref Node<T> tree)
{
if (tree == null)
{
tree = new Node<T>(item);
}
else if (item.CompareTo(tree.Data) < 0)
{
insertItem(item, ref tree.Left);
}
else if (item.CompareTo(tree.Data) > 0)
{
insertItem(item, ref tree.Right);
}
tree.BalanceFactor = height(tree.Left) - height(tree.Right);
if (tree.BalanceFactor <= -2)
{
rotateLeft(ref tree);
}
if (tree.BalanceFactor >= 2)
{
rotateRight(ref tree);
}
}
public new void RemoveItem(T item)
{
removeItem(item, ref root);
}
private void removeItem(T item, ref Node<T> tree)
{
if (tree == null)
{
Console.WriteLine("The tree doesn't have items.");
}
else if (tree != null)
{
if (item.CompareTo(tree.Data) < 0)
{
removeItem(item, ref tree.Left);
}
if (item.CompareTo(tree.Data) > 0)
{
removeItem(item, ref tree.Right);
}
if (item.CompareTo(tree.Data) == 0)
{
if (tree.Left == null)
{
tree = tree.Right;
}
else if (tree.Right == null)
{
tree = tree.Left;
}
else
{
T newRoot = leastItem(tree.Right);
tree.Data = newRoot;
removeItem(newRoot, ref tree.Right);
}
}
}
if (tree != null)
{
tree.BalanceFactor = height(tree.Left) - height(tree.Right);
if (tree.BalanceFactor <= -2)
{
rotateLeft(ref tree);
}
if (tree.BalanceFactor >= 2)
{
rotateRight(ref tree);
}
}
}
private T leastItem(Node<T> tree)
{
if (tree.Left == null)
return tree.Data;
else
return leastItem(tree.Left);
}
private void rotateLeft(ref Node<T> tree)
{
if (tree.Right != null)
{
if (tree.Right.BalanceFactor > 0)
{
rotateRight(ref tree.Right);
}
Node<T> oldRoot = tree;
Node<T> newRoot = oldRoot.Right;
oldRoot.Right = newRoot.Left;
newRoot.Left = oldRoot;
tree = newRoot;
}
}
private void rotateRight(ref Node<T> tree)
{
if (tree.Left != null)
{
if (tree.Left.BalanceFactor < 0)
{
rotateLeft(ref tree.Left);
}
Node<T> oldRoot = tree;
Node<T> newRoot = oldRoot.Left;
oldRoot.Left = newRoot.Right;
newRoot.Right = oldRoot;
tree = newRoot;
}
}
public IEnumerator<T> GetEnumerator()
{
throw new NotImplementedException();
}
IEnumerator IEnumerable.GetEnumerator()
{
throw new NotImplementedException();
}
}
}
循环结束时,我没有任何类的实例,它们都存储在树中。如何使用树具有的方法访问它们并编辑信息?
任何答案将不胜感激。非常感谢!