BinaryTree.java

  1. package org.drip.spaces.big;

  2. /*
  3.  * -*- mode: java; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
  4.  */

  5. /*!
  6.  * Copyright (C) 2020 Lakshmi Krishnamurthy
  7.  * Copyright (C) 2019 Lakshmi Krishnamurthy
  8.  * Copyright (C) 2018 Lakshmi Krishnamurthy
  9.  * Copyright (C) 2017 Lakshmi Krishnamurthy
  10.  * Copyright (C) 2016 Lakshmi Krishnamurthy
  11.  * Copyright (C) 2015 Lakshmi Krishnamurthy
  12.  *
  13.  *  This file is part of DROP, an open-source library targeting analytics/risk, transaction cost analytics,
  14.  *      asset liability management analytics, capital, exposure, and margin analytics, valuation adjustment
  15.  *      analytics, and portfolio construction analytics within and across fixed income, credit, commodity,
  16.  *      equity, FX, and structured products. It also includes auxiliary libraries for algorithm support,
  17.  *      numerical analysis, numerical optimization, spline builder, model validation, statistical learning,
  18.  *      and computational support.
  19.  *  
  20.  *      https://lakshmidrip.github.io/DROP/
  21.  *  
  22.  *  DROP is composed of three modules:
  23.  *  
  24.  *  - DROP Product Core - https://lakshmidrip.github.io/DROP-Product-Core/
  25.  *  - DROP Portfolio Core - https://lakshmidrip.github.io/DROP-Portfolio-Core/
  26.  *  - DROP Computational Core - https://lakshmidrip.github.io/DROP-Computational-Core/
  27.  *
  28.  *  DROP Product Core implements libraries for the following:
  29.  *  - Fixed Income Analytics
  30.  *  - Loan Analytics
  31.  *  - Transaction Cost Analytics
  32.  *
  33.  *  DROP Portfolio Core implements libraries for the following:
  34.  *  - Asset Allocation Analytics
  35.  *  - Asset Liability Management Analytics
  36.  *  - Capital Estimation Analytics
  37.  *  - Exposure Analytics
  38.  *  - Margin Analytics
  39.  *  - XVA Analytics
  40.  *
  41.  *  DROP Computational Core implements libraries for the following:
  42.  *  - Algorithm Support
  43.  *  - Computation Support
  44.  *  - Function Analysis
  45.  *  - Model Validation
  46.  *  - Numerical Analysis
  47.  *  - Numerical Optimizer
  48.  *  - Spline Builder
  49.  *  - Statistical Learning
  50.  *
  51.  *  Documentation for DROP is Spread Over:
  52.  *
  53.  *  - Main                     => https://lakshmidrip.github.io/DROP/
  54.  *  - Wiki                     => https://github.com/lakshmiDRIP/DROP/wiki
  55.  *  - GitHub                   => https://github.com/lakshmiDRIP/DROP
  56.  *  - Repo Layout Taxonomy     => https://github.com/lakshmiDRIP/DROP/blob/master/Taxonomy.md
  57.  *  - Javadoc                  => https://lakshmidrip.github.io/DROP/Javadoc/index.html
  58.  *  - Technical Specifications => https://github.com/lakshmiDRIP/DROP/tree/master/Docs/Internal
  59.  *  - Release Versions         => https://lakshmidrip.github.io/DROP/version.html
  60.  *  - Community Credits        => https://lakshmidrip.github.io/DROP/credits.html
  61.  *  - Issues Catalog           => https://github.com/lakshmiDRIP/DROP/issues
  62.  *  - JUnit                    => https://lakshmidrip.github.io/DROP/junit/index.html
  63.  *  - Jacoco                   => https://lakshmidrip.github.io/DROP/jacoco/index.html
  64.  *
  65.  *  Licensed under the Apache License, Version 2.0 (the "License");
  66.  *      you may not use this file except in compliance with the License.
  67.  *  
  68.  *  You may obtain a copy of the License at
  69.  *      http://www.apache.org/licenses/LICENSE-2.0
  70.  *  
  71.  *  Unless required by applicable law or agreed to in writing, software
  72.  *      distributed under the License is distributed on an "AS IS" BASIS,
  73.  *      WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  74.  *  
  75.  *  See the License for the specific language governing permissions and
  76.  *      limitations under the License.
  77.  */

  78. /**
  79.  * <i>BinaryTree</i> contains an Implementation of the Left/Right Binary Tree.
  80.  *
  81.  * <br><br>
  82.  *  <ul>
  83.  *      <li><b>Module </b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/ComputationalCore.md">Computational Core Module</a></li>
  84.  *      <li><b>Library</b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/StatisticalLearningLibrary.md">Statistical Learning Library</a></li>
  85.  *      <li><b>Project</b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/src/main/java/org/drip/spaces/README.md">R<sup>1</sup> and R<sup>d</sup> Vector/Tensor Spaces (Validated and/or Normed), and Function Classes</a></li>
  86.  *      <li><b>Package</b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/src/main/java/org/drip/spaces/big/README.md">Big-data In-place Manipulator</a></li>
  87.  *  </ul>
  88.  * <br><br>
  89.  *
  90.  * @author Lakshmi Krishnamurthy
  91.  */

  92. public class BinaryTree {
  93.     private int _iCount = 1;
  94.     private BinaryTree _btParent = null;
  95.     private BinaryTree _btLeftChild = null;
  96.     private BinaryTree _btRightChild = null;
  97.     private double _dblNode = java.lang.Double.NaN;

  98.     /**
  99.      * BinaryTree Constructor
  100.      *
  101.      * @param dblNode The Node Value
  102.      * @param btParent The BinaryTree Parent
  103.      *
  104.      * @throws java.lang.Exception Thrown if the Inputs are invalid
  105.      */

  106.     public BinaryTree (
  107.         final double dblNode,
  108.         final BinaryTree btParent)
  109.         throws java.lang.Exception
  110.     {
  111.         if (!org.drip.numerical.common.NumberUtil.IsValid (_dblNode = dblNode))
  112.             throw new java.lang.Exception ("BinaryTree ctr => Invalid Inputs");

  113.         _iCount = 1;
  114.         _btParent = btParent;
  115.     }

  116.     /**
  117.      * Retrieve the BinaryTree Node Value
  118.      *
  119.      * @return The BinaryTree Node Value
  120.      */

  121.     public double node()
  122.     {
  123.         return _dblNode;
  124.     }

  125.     /**
  126.      * Retrieve the Parent BinaryTree Instance
  127.      *
  128.      * @return The Parent BinaryTree Instance
  129.      */

  130.     public BinaryTree parent()
  131.     {
  132.         return _btParent;
  133.     }

  134.     /**
  135.      * Retrieve the Left Child BinaryTree Instance
  136.      *
  137.      * @return The Left Child BinaryTree Instance
  138.      */

  139.     public BinaryTree leftChild()
  140.     {
  141.         return _btLeftChild;
  142.     }

  143.     /**
  144.      * Retrieve the Right Child BinaryTree Instance
  145.      *
  146.      * @return The Right Child BinaryTree Instance
  147.      */

  148.     public BinaryTree rightChild()
  149.     {
  150.         return _btRightChild;
  151.     }

  152.     /**
  153.      * Retrieve the Node Instance Count
  154.      *
  155.      * @return The Node Instance Count
  156.      */

  157.     public int count()
  158.     {
  159.         return _iCount;
  160.     }

  161.     /**
  162.      * Insert a Node into the Tree
  163.      *
  164.      * @param dblNode The Node to be inserted
  165.      *
  166.      * @return The Inserted Node
  167.      */

  168.     public BinaryTree insert (
  169.         final double dblNode)
  170.     {
  171.         if (!org.drip.numerical.common.NumberUtil.IsValid (dblNode)) return null;

  172.         if (_dblNode == dblNode) {
  173.             ++_iCount;
  174.             return this;
  175.         }

  176.         try {
  177.             if (dblNode < _dblNode)
  178.                 return null == _btLeftChild ? _btLeftChild = new BinaryTree (dblNode, this) :
  179.                     _btLeftChild.insert (dblNode);

  180.             return null == _btRightChild ? _btRightChild = new BinaryTree (dblNode, this) :
  181.                 _btRightChild.insert (dblNode);
  182.         } catch (java.lang.Exception e) {
  183.             e.printStackTrace();
  184.         }

  185.         return null;
  186.     }

  187.     /**
  188.      * Retrieve the Left Most Child
  189.      *
  190.      * @return The Left Most Child BinaryTree Instance
  191.      */

  192.     public BinaryTree leftMostChild()
  193.     {
  194.         BinaryTree btParent = this;

  195.         BinaryTree btLeftChild = leftChild();

  196.         while (null != btLeftChild) {
  197.             btParent = btLeftChild;

  198.             btLeftChild = btParent.leftChild();
  199.         }

  200.         return btParent;
  201.     }

  202.     /**
  203.      * Retrieve the Right Most Child
  204.      *
  205.      * @return The Right Most Child BinaryTree Instance
  206.      */

  207.     public BinaryTree rightMostChild()
  208.     {
  209.         BinaryTree btParent = this;

  210.         BinaryTree btRightChild = rightChild();

  211.         while (null != btRightChild) {
  212.             btParent = btRightChild;

  213.             btRightChild = btParent.rightChild();
  214.         }

  215.         return btParent;
  216.     }

  217.     /**
  218.      * Build a Consolidated Ascending List of all the Constituent Nodes
  219.      *
  220.      * @param lsNode The Node List
  221.      *
  222.      * @return TRUE - The Ascending Node List Successfully Built
  223.      */

  224.     public boolean ascendingNodeList (
  225.         final java.util.List<java.lang.Double> lsNode)
  226.     {
  227.         if (null == lsNode) return false;

  228.         if (null != _btLeftChild && !_btLeftChild.ascendingNodeList (lsNode)) return false;

  229.         lsNode.add (_dblNode);

  230.         if (null != _btRightChild && !_btRightChild.ascendingNodeList (lsNode)) return false;

  231.         return true;
  232.     }

  233.     /**
  234.      * Build a Consolidated Ascending List of all the Constituent Nodes
  235.      *
  236.      * @return The Node List
  237.      */

  238.     public java.util.List<java.lang.Double> ascendingNodeList()
  239.     {
  240.         java.util.List<java.lang.Double> lsNode = new java.util.ArrayList<java.lang.Double>();

  241.         return ascendingNodeList (lsNode) ? lsNode : null;
  242.     }

  243.     /**
  244.      * Build a Consolidated Ascending Array of all the Constituent Nodes
  245.      *
  246.      * @param adblNode The Node Array
  247.      * @param iUpdateStartIndex The Update Start Index
  248.      *
  249.      * @return TRUE - The Ascending Node Array Successfully Built
  250.      *
  251.      * @throws java.lang.Exception Thrown if the Inputs are Invalid
  252.      */

  253.     public int ascendingNodeArray (
  254.         final double[] adblNode,
  255.         final int iUpdateStartIndex)
  256.         throws java.lang.Exception
  257.     {
  258.         if (null == adblNode || 0 == adblNode.length)
  259.             throw new java.lang.Exception ("BinaryTree::ascendingNodeArray => Invalid Inputs");

  260.         int iIndexToUpdate = null == _btLeftChild ? iUpdateStartIndex : _btLeftChild.ascendingNodeArray
  261.             (adblNode, iUpdateStartIndex);

  262.         if (iIndexToUpdate >= adblNode.length)
  263.             throw new java.lang.Exception ("BinaryTree::ascendingNodeArray => Invalid Inputs");

  264.         adblNode[iIndexToUpdate++] = _dblNode;

  265.         return null == _btRightChild ? iIndexToUpdate : _btRightChild.ascendingNodeArray (adblNode,
  266.             iIndexToUpdate);
  267.     }

  268.     /**
  269.      * Build a Consolidated Descending List of all the Constituent Nodes
  270.      *
  271.      * @param lsNode The Node List
  272.      *
  273.      * @return TRUE - The Descending Node List Successfully Built
  274.      */

  275.     public boolean descendingNodeList (
  276.         final java.util.List<java.lang.Double> lsNode)
  277.     {
  278.         if (null == lsNode) return false;

  279.         if (null != _btRightChild && !_btRightChild.descendingNodeList (lsNode)) return false;

  280.         lsNode.add (_dblNode);

  281.         if (null != _btLeftChild && !_btLeftChild.descendingNodeList (lsNode)) return false;

  282.         return true;
  283.     }

  284.     /**
  285.      * Build a Consolidated Descending Array of all the Constituent Nodes
  286.      *
  287.      * @param adblNode The Node Array
  288.      * @param iIndexToUpdate The Index To Update
  289.      *
  290.      * @return TRUE - The Descending Node Array Successfully Built
  291.      */

  292.     public boolean descendingNodeArray (
  293.         final double[] adblNode,
  294.         final int iIndexToUpdate)
  295.     {
  296.         if (null == adblNode || 0 == adblNode.length) return false;

  297.         if (null != _btLeftChild && !_btLeftChild.descendingNodeArray (adblNode, iIndexToUpdate))
  298.             return false;

  299.         if (iIndexToUpdate >= adblNode.length) return false;

  300.         adblNode[iIndexToUpdate] = _dblNode;

  301.         if (null != _btRightChild && !_btRightChild.descendingNodeArray (adblNode, iIndexToUpdate + 1))
  302.             return false;

  303.         return true;
  304.     }

  305.     /**
  306.      * Build a Consolidated Descending List of all the Constituent Nodes
  307.      *
  308.      * @return The Node List
  309.      */

  310.     public java.util.List<java.lang.Double> descendingNodeList()
  311.     {
  312.         java.util.List<java.lang.Double> lsNode = new java.util.ArrayList<java.lang.Double>();

  313.         return descendingNodeList (lsNode) ? lsNode : null;
  314.     }
  315. }