二叉树查找

使用二叉链表存储的二叉树很容易施加基于递归的算法,但有一个问题就是定位是很不好处理的问题,例如找某一个节点,查找某一个节点的父节点,记录从根节点到某一个节点的路径等。

最近几天没有时间把自己写的代码整理出来,这里给出一部分,测试代码不给出(自己建测试的树太没有技术含量了:)),有兴趣的可以自己构建一棵测试的树。树节点的数据结构为:

struct binary_tree_node

{

       binary_tree_node* _lchild;

       binary_tree_node* _rchild;

       int _value;

 

       binary_tree_node():_lchild(NULL),_rchild(NULL),_value(-1){ };

       binary_tree_node(int val):_lchild(NULL),_rchild(NULL),_value(val){ };

};

 

typedef binary_tree_node* node;

定位一个节点递归实现为:

node FindNode(const node root,int val)

{

       node n = root;

       if (NULL == n)

              return NULL;

 

       if (n->_value == val)

              return n;

 

       node p = FindNode(n->_lchild,val);

       if (NULL == p)

              return FindNode(n->_rchild,val);

       else

              return p;

}

定位一个节点非递归实现为(因为上面的递归实现是一个伪递归,使用堆栈很容易改成非递归:)):

node FindNode2(const node root,int val)

{

       if (NULL == root)

              return NULL;

 

       stack<node> tree;

       tree.push(root);

 

       node n = NULL;

 

       while (!tree.empty())

       {

              n = tree.top();

              tree.pop();

 

              if (n->_value != val)

              {

                     if (n->_rchild != NULL)

                            tree.push(n->_rchild);

 

                     if (n->_lchild != NULL)

                            tree.push(n->_lchild);

              }

              else

              {

                     return n;

              }

       }

 

       return NULL;

}

输出到当前节点的所有路径,也就是当前节点的所有父亲节点集合(这个很有用的,呵呵,知道的人自然知道:))。这个实现其实是根据中序遍历得到的,采用中序遍历的方式查找节点,当查找到当前节点的时候堆栈中保存的就是当前节点以及当前节点的所有父亲节点。实现代码为:

int* GetParent(const node root,int val)

{

       assert(NULL != root);

       if (root->_value == val)

       {

              cout<<"found: "<<root->_value<<endl;

              int* prt = new int(root->_value);

              return prt;

       }

 

       stack<node> tree;

       node n = root;

       node tmp = NULL;

 

       while ((!tree.empty()) || (NULL != root))

       {

              while (n != NULL)

              {

                     tree.push(n);

                     n = n->_lchild;

              }

 

              if (!tree.empty())

              {

                     int len = tree.size();

                     int* prt = new int[len];

                     int i = 0;

 

                     tmp = tree.top();

                     if (tmp->_value == val)

                     {

                            while (tree.size() > 0)

                            {

                                   prt[i++] = tree.top()->_value;

                                   cout<<tree.top()->_value<<"->";

                                   tree.pop();

                            }

                            cout<<endl;

 

                            return prt;

                     }

                     else

                            n = n->_rchild;

              }

       }

 

       return NULL;

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 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、付费专栏及课程。

余额充值