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;
}