📄 binarytree.cs
字号:
namespace Opus6
{
using System;
[Copyright("Copyright (c) 2001 by Bruno R. Preiss, P.Eng."), Version("$Id: BinaryTree.cs,v 1.5 2001/10/28 19:50:09 brpreiss Exp $")]
public class BinaryTree : AbstractTree
{
public BinaryTree() : this(null, null, null)
{
}
public BinaryTree(object key) : this(key, new BinaryTree(), new BinaryTree())
{
}
public BinaryTree(object key, BinaryTree left, BinaryTree right)
{
this.key = key;
this.left = left;
this.right = right;
}
public virtual void AttachKey(object obj)
{
if (!this.IsEmpty)
{
throw new InvalidOperationException();
}
this.key = obj;
this.left = new BinaryTree();
this.right = new BinaryTree();
}
public void AttachLeft(BinaryTree t)
{
if (this.IsEmpty || !this.left.IsEmpty)
{
throw new InvalidOperationException();
}
this.left = t;
}
public void AttachRight(BinaryTree t)
{
if (this.IsEmpty || !this.right.IsEmpty)
{
throw new InvalidOperationException();
}
this.right = t;
}
public override int CompareTo(object obj)
{
BinaryTree tree1 = (BinaryTree) obj;
if (this.IsEmpty)
{
return (tree1.IsEmpty ? 0 : -1);
}
if (tree1.IsEmpty)
{
return 1;
}
int num1 = ((IComparable) this.Key).CompareTo(tree1.Key);
if (num1 == 0)
{
num1 = this.Left.CompareTo(tree1.Left);
}
if (num1 == 0)
{
num1 = this.Right.CompareTo(tree1.Right);
}
return num1;
}
public override void DepthFirstTraversal(PrePostVisitor visitor)
{
if (!this.IsEmpty)
{
visitor.PreVisit(this.key);
this.Left.DepthFirstTraversal(visitor);
visitor.InVisit(this.key);
this.Right.DepthFirstTraversal(visitor);
visitor.PostVisit(this.key);
}
}
public virtual object DetachKey()
{
if (!this.IsLeaf)
{
throw new InvalidOperationException();
}
object obj1 = this.key;
this.key = null;
this.left = null;
this.right = null;
return obj1;
}
public BinaryTree DetachLeft()
{
if (this.IsEmpty)
{
throw new InvalidOperationException();
}
BinaryTree tree1 = this.left;
this.left = new BinaryTree();
return tree1;
}
public BinaryTree DetachRight()
{
if (this.IsEmpty)
{
throw new InvalidOperationException();
}
BinaryTree tree1 = this.right;
this.right = new BinaryTree();
return tree1;
}
public override Tree GetSubtree(int i)
{
Bounds.Check(i, 0, 2);
if (i == 0)
{
return this.Left;
}
return this.Right;
}
public static void Main()
{
BinaryTree tree1 = new BinaryTree(4);
tree1.AttachLeft(new BinaryTree(2));
tree1.AttachRight(new BinaryTree(6));
AbstractTree.TestTree(tree1);
}
public override void Purge()
{
this.key = null;
this.left = null;
this.right = null;
}
public override int Degree
{
get
{
return (this.IsEmpty ? 0 : 2);
}
}
public override bool IsEmpty
{
get
{
return (this.key == null);
}
}
public override bool IsLeaf
{
get
{
return ((!this.IsEmpty && this.left.IsEmpty) && this.right.IsEmpty);
}
}
public override object Key
{
get
{
if (this.IsEmpty)
{
throw new InvalidOperationException();
}
return this.key;
}
}
public BinaryTree Left
{
get
{
if (this.IsEmpty)
{
throw new InvalidOperationException();
}
return this.left;
}
}
public BinaryTree Right
{
get
{
if (this.IsEmpty)
{
throw new InvalidOperationException();
}
return this.right;
}
}
protected object key;
protected BinaryTree left;
protected BinaryTree right;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -