Binary Search Tree (4) and Binary Search Tree

Source: Internet
Author: User

Binary Search Tree (4) and Binary Search Tree
Assume that the node of the tree is defined as follows, and find the precursor and successor node of a specified value. If the specified value is not found in the fruit tree, the boundary value between regions is returned.

structNode{   intkey;   Node *left,*right ;};

The following is an algorithm for implementing this operation, using recursion:

Input: root node, key value output: precursor node, successor Node 1. if root is NULL then return2. if key is found then. if its left subtree is not null Then predecessor will be the right most child of left subtree or left child itself. b. if its right subtree is not null The successor will be the left most child of right subtree or right child itself. return3. If key is smaller then root node set the successor as root search recursively into left subtree else set the predecessor as root search recursively into right subtree

The following is based on the C ++ code of the above algorithm:

// Find the C ++ Program # include <iostream> struct Node {int key; Node * left; Node * right;} in the BST ;}; // In BST, find the precursor and successor of the specified value and save them in pre and suc respectively. Void findPreSuc (Node * root, int key, Node * & pre, Node * & suc) {// empty tree if (root = NULL) return; // find the matched node if (root-> key = key) {// The maximum value in the left subtree is the former if (root-> left! = NULL) {Node * tmp = root-> left; while (tmp-> right) tmp = tmp-> right; pre = tmp ;} // The minimum value in the right subtree is the next if (root-> right! = NULL) {Node * tmp = root-> right; while (tmp-> left) tmp = tmp-> left; suc = tmp;} return ;} // if the specified value is smaller than the root node value, continue to search for the left subtree if (root-> key) {suc = root; findPreSuc (root-> left, key, pre, suc);} else // find the right subtree {pre = root; findPreSuc (root-> right, key, pre, suc );}} // create a new BST Node * createNewNode (int item) {Node * temp = new Node; temp-> key = item; temp-> left = temp-> right = NULL; return temp;} // insert a new Node to the Node * insert (Node * Node, int key) {// empty tree if (node = NULL) return createNewNode (key); // recursive insert. If a specified value already exists, if (key <node-> key) node-> left = insert (node-> left, key) is not inserted ); else if (key> node-> key) node-> right = insert (node-> right, key); // return the unmodified node pointer return node ;} // traverse the Binary Search Tree void inorder (Node * root) {if (root! = NULL) {inorder (root-> left); std: cout <"<root-> key <""; inorder (root-> right) ;}} int main () {/* construct a BST 55/\ 33 77/\ 22 44 66 88 */Node * root = NULL; root = insert (root, 55) as shown below ); insert (root, 33); insert (root, 22); insert (root, 44); insert (root, 77); insert (root, 66); insert (root, 88); Node * pre = NULL, * suc = NULL; int key = 59; findPreSuc (root, key, pre, suc); if (pre! = NULL) std: cout <"Predecessor is" <pre-> key <std: endl; elsestd: cout <"No Predecessor \ n "; if (suc! = NULL) std: cout <"Successor is" <suc-> key <std: endl; elsestd: cout <"No Successor \ n "; return 0 ;}
Output:
Predecessor is 55
Successor is 66

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.