二叉查找树的一些操作(search,getminvalue,get maxvalue,insert)
2010-09-26 17:03
836 查看
在前一篇关于二叉查找树中,我们创建了一个二叉查找树,然后用递归和非递归的方法,遍历了树中的所有节点,文章的地址:http://www.cnblogs.com/chenping-987123/archive/2010/09/25/1834341.html
下面我们就进一步多了解一下关于这个二叉查找树其他的功能:
(1) search功能:在二叉查找树中查找某个节点,代码如下:(根据二叉查找树的性质左节点的value<=根节点的value<=右节点的value)
private static MyNode TreeSearch(MyNode root, int p)
{
if (root == null || root._value == p)
{
return root;
}
if (p < root._value)
{
return TreeSearch(root.left,p);
}
else
{
return TreeSearch(root.right, p);
}
}
(2) getminvalue(得到树中的最小值,根据性质,可以知道,二叉查找树的最小值就是最左边的节点),代码如下:
private static MyNode TreeMin(MyNode root)
{
MyNode node = null;
while (root != null)
{
node = root;
root = root.left;
}
return node;
}
(3) getmaxvalue(得到树中的最大值,根据性质,可以知道,二叉查找树的最大值就是最右边的节点),代码如下:
private static MyNode TreeMax(MyNode root)
{
MyNode node = null;
while (root != null)
{
node = root;
root = root.right;
}
return node;
}
(4) insert节点
private static void InsertTreeNode(MyNode root, int p)
{
MyNode y = null;
MyNode x = root;
while (x != null)
{
y = x;
if (p < x._value)
{
x = x.left;
}
else
{
x = x.right;
}
}
MyNode z = new MyNode();
z._value = p;
z.parent = y;
z.left = z.right = null;
if (y ==null)
{
root = z;
}
else if (z._value < y._value)
{
y.left = z;
}
else
{
y.right = z;
}
}
下面我们就进一步多了解一下关于这个二叉查找树其他的功能:
(1) search功能:在二叉查找树中查找某个节点,代码如下:(根据二叉查找树的性质左节点的value<=根节点的value<=右节点的value)
private static MyNode TreeSearch(MyNode root, int p)
{
if (root == null || root._value == p)
{
return root;
}
if (p < root._value)
{
return TreeSearch(root.left,p);
}
else
{
return TreeSearch(root.right, p);
}
}
(2) getminvalue(得到树中的最小值,根据性质,可以知道,二叉查找树的最小值就是最左边的节点),代码如下:
private static MyNode TreeMin(MyNode root)
{
MyNode node = null;
while (root != null)
{
node = root;
root = root.left;
}
return node;
}
(3) getmaxvalue(得到树中的最大值,根据性质,可以知道,二叉查找树的最大值就是最右边的节点),代码如下:
private static MyNode TreeMax(MyNode root)
{
MyNode node = null;
while (root != null)
{
node = root;
root = root.right;
}
return node;
}
(4) insert节点
private static void InsertTreeNode(MyNode root, int p)
{
MyNode y = null;
MyNode x = root;
while (x != null)
{
y = x;
if (p < x._value)
{
x = x.left;
}
else
{
x = x.right;
}
}
MyNode z = new MyNode();
z._value = p;
z.parent = y;
z.left = z.right = null;
if (y ==null)
{
root = z;
}
else if (z._value < y._value)
{
y.left = z;
}
else
{
y.right = z;
}
}
相关文章推荐
- 实现一堆栈,要求三个操作,Pop,Push,GetMaxValue,时间均为O(1)
- 实现一堆栈,要求三个操作,Pop,Push,GetMaxValue,时间均为O(1)
- HttpWebRequest中GetResponse或者说GetRequestStream偶尔超时,或者是各种操作超时造成的假死的一些解决方案
- delphi中操作access 数据库 Insert,Update的一些情况
- map的insert操作范例(用value_type)
- HttpWebRequest中GetResponse或者说GetRequestStream偶尔超时,或者是各种操作超时造成的假死的一些解决方案
- 各种数据结构insert和search操作的平均时间复杂度比较
- python中Mysql的create,insert,update,fetchall,fetchone一些简单操作
- grid.getSelectionModel的一些操作【转】
- 算法导论10.2-5 使用单向循环链表实现字典操作 INSERT、DELETE 和 SEARCH
- LintCode Insert a Node in a Binary Search Tree 在二叉查找树中插入节点
- 二叉查找树(binary search tree)上的基本操作
- lintcode 容易题:Insert Node in a Binary Search Tree 在二叉查找树中插入节点
- 二叉搜索树的查询、插入与删除操作(Binary Search Tree, Search, Insert, Delete)(C++)
- Search Insert Position--寻找插入点Given a sorted array and a target value, return the index if the target
- 【trie树】insert和search操作
- GetMenuItemInfo & InsertMenuItem 枚举和追加菜单操作
- lintcode insert-node-in-a-binary-search-tree 在二叉查找树中插入节点
- (2)Mysql 一次性insert 20000或更多个value。其中N条执行出错,则记录错误原因。没错的正常执行插入操作。
- 使用单向循环链表实现字典操作 INSERT、DELETE 和 SEARCH