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#程序设计有所帮助。
热门推荐
10 喜得孙子祝福语简短唯美
11 婚礼祝福语简短朴实大众
12 员工周年祝福语大全简短
13 家长回复节日祝福语简短
14 简短生日祝福语老同学
15 夜雨祝福语简短10字
16 蛋糕祝福语简短闺蜜
17 部门过年祝福语简短独特
18 简短精炼的结婚祝福语