C#二叉搜索树插入算法实例分析
本文实例讲述了C#二叉搜索树插入算法。分享给大家供大家参考。具体实现方法如下:
publicclassBinaryTreeNode
{
publicBinaryTreeNodeLeft{get;set;}
publicBinaryTreeNodeRight{get;set;}
publicintData{get;set;}
publicBinaryTreeNode(intdata)
{
this.Data=data;
}
}
publicvoidInsertIntoBST(BinaryTreeNoderoot,intdata)
{
BinaryTreeNode_newNode=newBinaryTreeNode(data);
BinaryTreeNode_current=root;
BinaryTreeNode_previous=_current;
while(_current!=null)
{
if(data<_current.Data)
{
_previous=_current;
_current=_current.Left;
}
elseif(data>_current.Data)
{
_previous=_current;
_current=_current.Right;
}
}
if(data<_previous.Data)
_previous.Left=_newNode;
else
_previous.Right=_newNode;
}
希望本文所述对大家的C#程序设计有所帮助。