二叉树的查找

实际上跟我上一篇博文重了,这篇多加了查找


上一篇文章

#include<stdio.h>
#include<string.h>
#include<iostream>
using namespace std;
typedef struct node
{
    struct node *leftchild;
    struct node *rightchild;
    char data;
} bitreenode,*bitree;
void createTree(bitree &T)
{
    char ch;
    cin>>ch;
    if(ch=='#')T=NULL;
    else
    {
        T=new node;
        T->data=ch;
        createTree(T->leftchild);
        createTree(T->rightchild);
    }
}
void preTraverse(bitree &T)
{
    if(T==NULL)return ;
    cout<<T->data;
    preTraverse(T->leftchild);
    preTraverse(T->rightchild);
}
void intraverse(bitree &T)
{
    if(T==NULL)return ;
    intraverse(T->leftchild);
    cout<<T->data;
    intraverse(T->rightchild);

}
void posttraverse(bitree &T)
{
    if(T==NULL)return ;
    posttraverse(T->leftchild);
    posttraverse(T->rightchild);
    cout<<T->data;
}

node* TreeFindNode(bitree &treeNode,char data)//二叉树的查找
{
    node *ptr;//这里可以换成bool类型或者int类型的,也就是说返回值不一定要用指针,这仅仅是个标志而已
    if(treeNode==NULL)
    {
        return NULL;
    }
    else
    {
        if(treeNode->data==data)
        {
            return treeNode;
        }
        else        //分别向左右子树查找
        {
            if(ptr=TreeFindNode(treeNode->leftchild,data))  //左子树递归查找
            {
                return ptr;
            }
            else if(ptr=TreeFindNode(treeNode->rightchild,data))         //右子树递归查找
            {
                return ptr;
            }
            else
            {
                return NULL;
            }
        }
    }
}
//比如像下面这样
/*
int TreeFindNode(bitree &treeNode,char data)//二叉树的查找
{
    if(treeNode==NULL)
    {
        return 0;
    }
    else
    {
        if(treeNode->data==data)
        {
            return 1;
        }
        else        //分别向左右子树查找
        {
            if(TreeFindNode(treeNode->leftchild,data))  //左子树递归查找
            {
                return 1;
            }
            else if(TreeFindNode(treeNode->rightchild,data))         //右子树递归查找
            {
                return 1;
            }
            else
            {
                return 0;
            }
        }
    }
}*/
int main()
{
    int i,j,k;
    node *T;
    createTree(T);
    cout<<endl;
    preTraverse(T);
    cout<<endl;
    intraverse(T);
    cout<<endl;
    posttraverse(T);
    if(TreeFindNode(T,'X')!=NULL)printf("\nYES\n");
    else printf("\nNO\n");
    return 0;
}
/*
AB#D##C#E##
*/


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
折半查找(Binary Search)和二叉树查找(Binary Tree Search)是两种常见的查找算法。 1. 折半查找是一种在有序数组中查找指定元素的算法。它的基本思想是将数组分成两部分,然后判断目标元素与中间元素的大小关系,进而确定目标元素在哪一部分中。如果目标元素等于中间元素,则查找成功;如果目标元素小于中间元素,则在前一部分中继续查找;如果目标元素大于中间元素,则在后一部分中继续查找。通过不断缩小查找范围,最终可以找到目标元素或确定目标元素不存在于数组中。 2. 二叉树查找是一种在二叉树查找指定元素的算法。二叉树是一种特殊的树结构,每个节点最多有两个子节点。在二叉树查找中,首先比较目标元素与当前节点的值的大小关系,如果相等则查找成功;如果目标元素小于当前节点的值,则在左子树中继续查找;如果目标元素大于当前节点的值,则在右子树中继续查找。通过不断比较和移动,最终可以找到目标元素或确定目标元素不存在于二叉树中。 下面是两种查找算法的示例代码: 1. 折半查找: ```python def binary_search(arr, target): low = 0 high = len(arr) - 1 while low <= high: mid = (low + high) // 2 if arr[mid] == target: return mid elif arr[mid] < target: low = mid + 1 else: high = mid - 1 return -1 arr = [1, 3, 5, 7, 9] target = 5 result = binary_search(arr, target) print("Index of target element:", result) # 输出:2 ``` 2. 二叉树查找: ```python class TreeNode: def __init__(self, val): self.val = val self.left = None self.right = None def binary_tree_search(root, target): if root is None or root.val == target: return root if root.val < target: return binary_tree_search(root.right, target) else: return binary_tree_search(root.left, target) # 构建二叉树 root = TreeNode(5) root.left = TreeNode(3) root.right = TreeNode(7) root.left.left = TreeNode(1) root.left.right = TreeNode(4) root.right.left = TreeNode(6) root.right.right = TreeNode(9) target = 4 result = binary_tree_search(root, target) if result is not None: print("Target element found in binary tree") else: print("Target element not found in binary tree") ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值