KX Community

Find answers, ask questions, and connect with our KX Community around the world.
KX Community Guidelines

Home Forums kdb+ How to walkthrough a tree and calculate value on path? Re: How to walkthrough a tree and calculate value on path?

  • creid

    Member
    April 18, 2023 at 12:00 am

    This is my solution

    tree:([]parent:`A`A`A`B`B`E`E;child:`B`C`D`E`F`G`H;data:1 2 3 4 5 6 7); 
    getVal:{?[`tree;((=;`parent;enlist x);(=;`child;enlist y));();`data]} 
    checkParent:{first ?[`tree;enlist (=;`child;enlist x);();`parent]} 
    getPathVal:{[x;y] $[x~checkParent[y];getVal[x;y];getVal[checkParent[y];y]*.z.s[x;checkParent[y]]]}