Bst Second Largest

AlgorithmAlgorithmBeginner
Practice Now

This tutorial is from open-source community. Access the source code

Introduction

A binary search tree is a data structure that is used to store data in a sorted manner. Each node in the tree has at most two children, and the left child is always less than the parent, while the right child is always greater than the parent. In this challenge, we will be finding the second largest node in a binary search tree.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL algorithm(("`Algorithm`")) -.-> algorithm/BasicAlgorithmsGroup(["`Basic Algorithms`"]) python(("`Python`")) -.-> python/BasicConceptsGroup(["`Basic Concepts`"]) python(("`Python`")) -.-> python/ControlFlowGroup(["`Control Flow`"]) python(("`Python`")) -.-> python/DataStructuresGroup(["`Data Structures`"]) python(("`Python`")) -.-> python/FunctionsGroup(["`Functions`"]) python(("`Python`")) -.-> python/ObjectOrientedProgrammingGroup(["`Object-Oriented Programming`"]) python(("`Python`")) -.-> python/ErrorandExceptionHandlingGroup(["`Error and Exception Handling`"]) algorithm/BasicAlgorithmsGroup -.-> algorithm/graphs_trees("`Graphs Trees`") algorithm/BasicAlgorithmsGroup -.-> algorithm/recursion_dynamic("`Recursion Dynamic`") python/BasicConceptsGroup -.-> python/variables_data_types("`Variables and Data Types`") python/BasicConceptsGroup -.-> python/strings("`Strings`") python/BasicConceptsGroup -.-> python/type_conversion("`Type Conversion`") python/ControlFlowGroup -.-> python/conditional_statements("`Conditional Statements`") python/DataStructuresGroup -.-> python/tuples("`Tuples`") python/FunctionsGroup -.-> python/function_definition("`Function Definition`") python/FunctionsGroup -.-> python/default_arguments("`Default Arguments`") python/ObjectOrientedProgrammingGroup -.-> python/classes_objects("`Classes and Objects`") python/ObjectOrientedProgrammingGroup -.-> python/constructor("`Constructor`") python/ObjectOrientedProgrammingGroup -.-> python/polymorphism("`Polymorphism`") python/ObjectOrientedProgrammingGroup -.-> python/encapsulation("`Encapsulation`") python/ErrorandExceptionHandlingGroup -.-> python/raising_exceptions("`Raising Exceptions`") python/FunctionsGroup -.-> python/build_in_functions("`Build-in Functions`") subgraph Lab Skills algorithm/graphs_trees -.-> lab-268820{{"`Bst Second Largest`"}} algorithm/recursion_dynamic -.-> lab-268820{{"`Bst Second Largest`"}} python/variables_data_types -.-> lab-268820{{"`Bst Second Largest`"}} python/strings -.-> lab-268820{{"`Bst Second Largest`"}} python/type_conversion -.-> lab-268820{{"`Bst Second Largest`"}} python/conditional_statements -.-> lab-268820{{"`Bst Second Largest`"}} python/tuples -.-> lab-268820{{"`Bst Second Largest`"}} python/function_definition -.-> lab-268820{{"`Bst Second Largest`"}} python/default_arguments -.-> lab-268820{{"`Bst Second Largest`"}} python/classes_objects -.-> lab-268820{{"`Bst Second Largest`"}} python/constructor -.-> lab-268820{{"`Bst Second Largest`"}} python/polymorphism -.-> lab-268820{{"`Bst Second Largest`"}} python/encapsulation -.-> lab-268820{{"`Bst Second Largest`"}} python/raising_exceptions -.-> lab-268820{{"`Bst Second Largest`"}} python/build_in_functions -.-> lab-268820{{"`Bst Second Largest`"}} end

Bst Second Largest

Problem

Given a binary search tree, find the second largest node in the tree. If the input is None or a single node, an exception should be raised.

To solve this problem, we can traverse the tree in a specific order and keep track of the second largest node we have seen so far. We can start by traversing the right subtree of the root node, and if the right subtree is None, then the largest node is the root node itself. If the right subtree is not None, we can continue traversing the right subtree until we reach a node that does not have a right child. At this point, the largest node in the tree is the parent of this node. If this parent node has a left child, then the second largest node is the largest node in the left subtree of the parent node. If the parent node does not have a left child, then the second largest node is the parent node itself.

Requirements

The requirements for this challenge are as follows:

  • If the input is None or a single node, an exception should be raised.
    • None input should raise a TypeError.
    • Single node input should raise a ValueError.
  • We can assume that we already have a Node class with an insert method.
  • We can assume that this problem fits memory.

Example Usage

Here are some examples of how to use this function:

  • None or single node -> Exception
Input:
                _10_
              _/    \_
             5        15
            / \       / \
           3   8     12  20
          /     \         \
         2       4        30

Output: 20

Input:
                 10
                 /
                5
               / \
              3   7
Output: 7

Summary

In this challenge, we have learned how to find the second largest node in a binary search tree. We have also discussed the requirements for this challenge and provided some examples of how to use this function.

Other Algorithm Tutorials you may like