日期:2014-05-17 浏览次数:20879 次
// 学习小结 吴新强于2013年3月18日17:22:55 桂电 2057 实验室
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
namespace ConsoleApplication1
{
class Program
{
// static void Main(string[] args)
static void Main()
{
BinarySearchTree nums = new BinarySearchTree();
// /*
nums.Insert(23);
nums.Insert(45);
nums.Insert(16);
nums.Insert(37);
nums.Insert(3);
nums.Insert(99);
nums.Insert(22);
// nums.GetSuccessor(nums.root);
Console.WriteLine("中序排序: ");//Inorder traversal
nums.InOrder(nums.root);
Console.WriteLine();
Console.WriteLine("先序排序: ");
nums.PreOrder(nums.root);
Console.WriteLine();
Console.WriteLine("后序排序: ");//Postorder traversal
nums.PostOrder(nums.root);
Console.WriteLine();
Console.WriteLine("序列中的最大值: " + nums.FindMax());//Max traversal
Console.WriteLine();
Console.WriteLine("序列中的最小值: " + nums.FindMin());//Min traversal:
Console.WriteLine();
Console.WriteLine("Find traversal: "+nums .Find(99));
Console.WriteLine();
Console.WriteLine("删除节点 99 : " + nums.Delete(99)+"\n反回true表示删除成功");//Delete traversal
Console.WriteLine("删除节点 4 : " + nums.Delete(4) + "\n反回flash表示删除序列中不存在这个值");
Console.WriteLine("删除后的中序排序: ");
nums.InOrder(nums.root);
Console.WriteLine();
// Console.WriteLine("Delete traversal: " + nums.GetSuccessor());
Console.WriteLine();
}
}
public class Node
{
public int Data;
public Node Left;
public Node Right;
public void DisplayNode()
{
Console.Write(Data + " ");
}
}
public class BinarySearchTree
{
public Node root;
public BinarySearchTree()
{
root = null;
}
public Node GetSuccessor(Node delNode)
{
Node successorParent = delNode;
Node successor = delNode;
Node current = delNode.Right;
while (!(current == null))
{
successorParent = current;
successor = current;
current = current.Left;
}
if (!(successor == delNode.Right))
{
successorParent.Left = successor.Right;
successor.Right = delNode.Right;
}
return successor;
}
public void PreOrder(Node theRoot)
{
&nb