Question

Hello, I need help implementing the c++ code for successor and predecessor of a BST following the pseudocode below, thank you for your help in advance.
TREE-SUCCESSOR (x) l if, right[x]〆NIL 2 then return TREE-MINIMUM(right[x]) 3 y ←p[x] 4 while y NIL and x = right[y] ply] 7 return y The code for TREE-PREDECESSOR is similar.

struct treeNode

{

int data;

treeNode *left;

treeNode *right;

};

treeNode* FindMin(treeNode *node) /* find the minumum node */

{

while (node->left != NULL)

{

node = node->left;

}

return node;

}

treeNode* FindMax(treeNode *node) /* find the maximum node */

{

while (node->left != NULL)

{

node = node->right;

}

return node;

}

0 0
Add a comment Improve this question Transcribed image text
Answer #1

C++ code:

// C++ program to find predecessor and successor in a BST

#include <iostream>

using namespace std;

// BST Node

struct treeNode{

int data;

treeNode *left;

treeNode *right;

};

treeNode* FindMin(treeNode *node) /* find the minumum node */

{

while (node->left != NULL){

node = node->left;

}

return node;

}

treeNode* FindMax(treeNode *node) /* find the maximum node */

{

while (node->left != NULL){

node = node->right;

}

return node;

}

// This function finds predecessor and successor of key in BST.

// It sets pre and suc as predecessor and successor respectively

void findPreSuc(treeNode* root, treeNode*& pre, treeNode*& suc, int key )

{

// Base case

if (root == NULL) return ;

// If key is present at root

if (root->data == key)

{

// the maximum value in left subtree is predecessor

treeNode *temp=FindMin(root->right);

}

// the minimum value in right subtree is successor

if (root->right != NULL)

{

treeNode* tmp = FindMax(root->left);

  

}

// If key is smaller than root's key, go to left subtree

if (root->data > key)

{

suc = root ;

findPreSuc(root->left, pre, suc, key) ;

}

else // go to right subtree

{

pre = root ;

findPreSuc(root->right, pre, suc, key) ;

}

}

// A utility function to create a new BST node

treeNode *newNode(int item)

{

treeNode *temp = new treeNode;

temp->data = item;

temp->left = temp->right = NULL;

return temp;

}

/* A utility function to insert a new node with given key in BST */

treeNode* insert(treeNode* node, int key)

{

if (node == NULL) return newNode(key);

if (key < node->data)

node->left = insert(node->left, key);

else

node->right = insert(node->right, key);

return node;

}

// Driver program to test above function

int main()

{

int key = 65; //Key to be searched in BST

treeNode *root = NULL;

root = insert(root, 50);

insert(root, 30);

insert(root, 20);

insert(root, 40);

insert(root, 70);

insert(root, 60);

insert(root, 80);

treeNode* pre = NULL, *suc = NULL;

findPreSuc(root, pre, suc, key);

if (pre != NULL)

cout << "Predecessor is " << pre->data << endl;

else

cout << "No Predecessor";

if (suc != NULL)

cout << "Successor is " << suc->data;

else

cout << "No Successor";

return 0;

}

screenshots:

ain.cpp 2 // C++ program to find predecessor and successor in a BST #include iostream» 4 using namespace std 6 I/ BST Node 7 struct treeNode 8 int data; 9 treeNode left; 10 treeNode right; 12 13 treeNode FindMin(treeNode node)* find the minumum node */ 14 15 ▼ 16 17 18 19 20 21 22 treeNode FindMax(treeNode node)* find the maximum node */ 23 while (node->left-NULL) node node-left return node; while (node->left NULL ){ node node->right; 25 26 27 28 return node;

Add a comment
Know the answer?
Add Answer to:
Hello, I need help implementing the c++ code for successor and predecessor of a BST following...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • BST JAVA FILE import java.util.*; public class BST <E extends Comparable <E>> {    private TreeNode<E>...

    BST JAVA FILE import java.util.*; public class BST <E extends Comparable <E>> {    private TreeNode<E> overallRoot;    public BST() {        overallRoot = null;    }    // ************ ADD ************ //    public void add(E addThis) {        if (overallRoot == null) {            overallRoot = new TreeNode<>(addThis);        } else {            add(overallRoot, addThis);        }    }    private TreeNode<E> add(TreeNode<E> node, E addThis) {        if...

  • I need to make it so this program outputs to an output.txt, the program works fine,...

    I need to make it so this program outputs to an output.txt, the program works fine, just need it to fprintf to output.txt #include <stdio.h> #include <string.h> #include <malloc.h> #define MAX 30 struct treeNode { char names[MAX];    struct treeNode *right; struct treeNode *left; }*node; void searchName(char names[], struct treeNode ** parent, struct treeNode ** location) { struct treeNode * ptr, * tempPtr; if(node == NULL)    { *location = NULL; *parent = NULL; return; } if(strcmp(names, node->names) == 0)...

  • I need help to write this code in C++, I am struggling to find max node's parent's right child and max node&#39...

    I need help to write this code in C++, I am struggling to find max node's parent's right child and max node's left child. Node* maxValueNode(Node* node) {    Node* current = node;    while (current && current->right != NULL)        current = current->right;    return current; } void deleteNode(BST* tree, Node* node, Node* parent) {    //TODO - follow the lecture pseudocode to write the deleteNode function    // - returns true if the value was deleted, false if not    // - don't forget to...

  • Please I need help ASAP Java Programing: Binary Search Tree Fully implement the BST class in Listing 25.4 (on page 961 of the 11th Edition of the text). Design and write a (main) driver program to com...

    Please I need help ASAP Java Programing: Binary Search Tree Fully implement the BST class in Listing 25.4 (on page 961 of the 11th Edition of the text). Design and write a (main) driver program to completely test every method in the BST class to ensure the class meets all its requirements. You should read the Listing 25.5: TestBST.java for an idea of what your program should look like. Listing 25.4 BST.java public class BST> extends AbstractTree { protected TreeNode...

  • C++ EXERCISE (DATA STRUCTURES). I just need a code for some functions that are missing. Please...

    C++ EXERCISE (DATA STRUCTURES). I just need a code for some functions that are missing. Please help me figure out. Thanks. C++ BST implementation (using a struct) Enter the code below, and then compile and run the program. After the program runs successfully, add the following functions: postorder() This function is similar to the inorder() and preorder() functions, but demonstrates postorder tree traversal. displayParentsWithTwo() This function is similar to the displayParents WithOne() function, but displays nodes having only two children....

  • PLEASE HELP! The assignment details are in the *noted part of the code. I REALLY need...

    PLEASE HELP! The assignment details are in the *noted part of the code. I REALLY need help. import java.util.LinkedList; public class TwoDTree { private TwoDTreeNode root; private int size; public TwoDTree() {    clear(); } /** * Returns true if a point is already in the tree. * Returns false otherwise. * * The traversal remains the same. Start at the root, if the tree * is not empty, and compare the x-coordinates of the point passed * in and...

  • Having code issues wth my C++ program. My program checks if two binary trees are similar...

    Having code issues wth my C++ program. My program checks if two binary trees are similar and if they're not they return false. My program is return true with different binary trees. Could use some help thanks #include <iostream> #include <string> using namespace std; //Struct of Nodes struct BinarySearchTree { int data; BinarySearchTree *left; BinarySearchTree *right; }; // Inserting nodes into BST BinarySearchTree* insert( BinarySearchTree* node, int val) { if (node == NULL) { BinarySearchTree *newNode = new BinarySearchTree(); newNode->data...

  • Question - modify the code below so that for a node, the value of every node...

    Question - modify the code below so that for a node, the value of every node of its right subtree is less the node, and the value of each node of its left subtree is greater than the node. - create such a binary tree in the Main method, and call the following method:  InOrder(Node theRoot),  PreOrder(Node theRoot),  PostOrder(Node theRoot),  FindMin(),  FindMax(),  Find(int key) using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks;...

  • write a new test program called RemoveDuplicates.java. The program reads text input from keyboard or a text file and adds the words to a BST. The program then traverses the BST and prints out the word...

    write a new test program called RemoveDuplicates.java. The program reads text input from keyboard or a text file and adds the words to a BST. The program then traverses the BST and prints out the words in order (based on ASCII/UNICODE order) on the screen (or to output text file). Note that you may need to make some changes to BST.java. Sample test: ----jGRASP exec: java -ea removeDuplicates Original Text: a B 2 n w C q K l 0...

  • In C++ I need the printRange function, and the main.cpp program. Thanks. (Binary search tree) Write...

    In C++ I need the printRange function, and the main.cpp program. Thanks. (Binary search tree) Write a function printRange that takes as input a binary search tree t and two keys, k1 and k2, which are ordered so that k1 < k2, and print all elements x in the tree such that k1 <= x <= k2. You can add this function in BinarySearchTree.h (click the link) that we used in the lecture and lab 7. public: void printRange(int k1,...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT