018 given a node of the binary query tree, write an algorithm to find its "Next" Node "(keep it up), 018 binary Query

Source: Internet
Author: User

018 given a node of the binary query tree, write an algorithm to find its "Next" Node "(keep it up), 018 binary Query
Given a node of the Binary Search Tree, write an algorithm to search for its "Next" Node (that is, its successor node after the middle order traversal ),
Each node has a link to its father.
The essence of this question is the question of finding successor nodes when a clue-Based Binary Tree is created. There are two scenarios for finding a successor node:
1. If the current node has a right child, the successor node is the leftmost node of the right child.
2. If no right child exists,
If the current node A is the left child of the parent node, the parent node is the successor node.
B. If the current node is the right child of the parent node, find the parent node until the right child of the current node is not the parent node ends.

The parent node is the successor node.

Code:

struct TreeNode{int data;TreeNode* leftChild;TreeNode* rightChild;TreeNode* parent;};TreeNode* findContinue(const TreeNode* vNode){if (vNode == NULL) return NULL;if (vNode->rightChild != NULL){TreeNode* Tmp = vNode->rightChild;while (Tmp->leftChild != NULL){Tmp = Tmp->leftChild;}return Tmp;}TreeNode* Cur     = vNode;TreeNode* pParent = Cur->parent;while (pParent != NULL && pParent->rightChild == Cur){Cur     = pParent;pParent = pParent->rightChild;}return pParent;}



Ask a question about finding a binary tree node

According to the program you changed, if the node to be checked is not in the root node, it can only run until the final return NULL, of course, it cannot be found ......
The original program returns the pointer recursively if it finds it at the end of a subtree in Search, and does not execute it after the return is complete.

Write a non-recursive algorithm for finding the node with the item value in the binary search tree with the BST as the root pointer. if the search is successful, the item will be taken back to the entire

Typedef int ElemType;
Typedef struct bn {
ElemType item;
Struct bn * left, * right;
} BTreeNode;
Bool Find (BTreeNode * BST, ElemType & item)
{
BTreeNode * now = BST;
While (now)
{
If (item> now-> item) now = now-> right;
Else if (item <now-> item) now = now-> left;
Else return true;
}
Return false;
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.