用递归的方法进行二叉树的搜索和插入
2006-10-06 21:11
246 查看
/*************************************************************************
用递归的方法进行二叉树的搜索和插入
当然二叉树的搜索和插入可以不用递归的方法,而且一般情况下递归也不是一
个很好的解决问题的方案,以下函数用一种严谨实现方法仅体现一种考虑问题的思想,
锻炼人严谨的思维方式。
Leo,2005/10/6
stefzeus@163.com
*************************************************************************/
/*TreeSearch:search for target starting st node root
pre: The tree root points has been created.
post: The function returns a pointer to a tree node that matches
target or NULL if the target is not in the tree */
TreeNode *TreeSearch(TreeNode *root,KeyType target)
{
if(root)
{
if(target < root->entry.key)
root=TreeSearch(root->left,target);
else if(target > root->entry.key)
root=TreeSearch(root->right,target);
}
return root;
}
/*InsertTree:insert a new node in the tree
pre: The tree root points has been created.The parameter newnode points to a node that has been created
and contains a key in its
post: The node newnode has been inserted into the tree in such a way
that the properties of a binary search tree are preserved. */
TreeNode *InsertTree(TreeNode *root,TreeNode *newnode)
{
if(!root)
{
root=newnode;
root->left=root->right=NULL;
}
else if (newnode->entry.key < root->entry.key)
root->left=InsertTree(root->left,newnode);
else
root->right=InsertTree(root->right,newnode);
return root;
}
用递归的方法进行二叉树的搜索和插入
当然二叉树的搜索和插入可以不用递归的方法,而且一般情况下递归也不是一
个很好的解决问题的方案,以下函数用一种严谨实现方法仅体现一种考虑问题的思想,
锻炼人严谨的思维方式。
Leo,2005/10/6
stefzeus@163.com
*************************************************************************/
/*TreeSearch:search for target starting st node root
pre: The tree root points has been created.
post: The function returns a pointer to a tree node that matches
target or NULL if the target is not in the tree */
TreeNode *TreeSearch(TreeNode *root,KeyType target)
{
if(root)
{
if(target < root->entry.key)
root=TreeSearch(root->left,target);
else if(target > root->entry.key)
root=TreeSearch(root->right,target);
}
return root;
}
/*InsertTree:insert a new node in the tree
pre: The tree root points has been created.The parameter newnode points to a node that has been created
and contains a key in its
post: The node newnode has been inserted into the tree in such a way
that the properties of a binary search tree are preserved. */
TreeNode *InsertTree(TreeNode *root,TreeNode *newnode)
{
if(!root)
{
root=newnode;
root->left=root->right=NULL;
}
else if (newnode->entry.key < root->entry.key)
root->left=InsertTree(root->left,newnode);
else
root->right=InsertTree(root->right,newnode);
return root;
}
相关文章推荐
- java递归方法建立搜索二叉树,具备查找关键字,插入新节点功能
- c++实现二叉树的查找,插入,删除,深度,叶子节点数,度为1的节点数(递归方法)及运行实例结果
- c++实现二叉树的查找,插入,删除,深度,叶子节点数,度为1的节点数(递归方法)及运行实例结果
- 用递归方法对二叉树进行层次遍历
- c++实现二叉树的查找,插入,删除,深度,叶子节点数,度为1的节点数(递归方法)及运行实例结果
- c++实现二叉树的查找,插入,删除,深度,叶子节点数,度为1的节点数(递归方法)及运行实例结果
- 用递归方法对二叉树进行先序、中序和后序遍历
- 用递归方法对二叉树进行层次遍历
- 用递归方法对二叉树进行先序、中序和后序遍历
- c++实现二叉树的查找,插入,删除,深度,叶子节点数,度为1的节点数(递归方法)及运行实例结果
- c++实现二叉树的查找,插入,删除,深度,叶子节点数,度为1的节点数(递归方法)及运行实例结果
- 用递归方法对二叉树进行层次遍历
- 二叉树的学习(四种遍历方法,搜索,插入,删除等)
- c++实现二叉树的查找,插入,删除,深度,叶子节点数,度为1的节点数(递归方法)及运行实例结果
- c++实现二叉树的查找,插入,删除,深度,叶子节点数,度为1的节点数(递归方法)及运行实例结果
- 用递归方法对二叉树进行先序、中序和后序遍历
- 【数据结构】——搜索二叉树的插入,查找和删除(递归&非递归)
- 用递归方法对二叉树进行层次遍历
- c++实现二叉树的查找,插入,删除,深度,叶子节点数,度为1的节点数(递归方法)及运行实例结果
- 用c语言创建一颗二叉树,用递归方法实现对其进行先序、中序和后序遍历的操作。