public IEnumerable <DepthNode <T> > GetChildren(DepthNode <T> node)
 {
     return
         (this
          ._Walker
          .GetChildren(node.BaseNode)
          .Select(x => new DepthNode <T>(x, node.Depth + 1)));
 }
 public IEnumerable <DepthNode <T> > GetAncestors(DepthNode <T> node)
 {
     return
         (this
          ._Walker
          .GetAncestors(node.BaseNode)
          .Select((x, i) => new DepthNode <T>(x, node.Depth - i - 1)));
 }
Example #3
0
        public DepthMap(ushort[] depthData, int width)
        {
            this.width = width;
            nodes = new DepthNode[depthData.Length];

            for (int i = 0; i < depthData.Length; i++)
            {
                nodes[i]=new DepthNode(i);
            }
            baseBuffer = new List<int>[nodes.Length];
        }
        //M(1) (root) finishes and left = 2 from M(3) and right = 2 from M(2);
        ////left is not bigger tha right so M(2) returns h = 1 + 1 (2)
        //M(2) finishes and left = 1 and right = 1;
        ////M(8) finishes and returns 1
        //M(null) returns 0 for both right and left(for M(8))
        //M(2) right = M(8);
        //M(3) finishes and returns 1
        //M(null) returns 0 for both right and left(for M(3))
        //M(3)
        //M(2) left = M(3)
        //left is not bigger tha right so M(3) returns h = 1 + 1 (2)
        //M(3) finishes and left = 1 and right = 1;
        //M(4) finishes(both l and r) and returns 1;
        // M(null) returns 0 for both right and left
        //     right = M(4)
        //M(3) resumes
        //M(5) finishes(both l and r) and returns 1;
        //M(null)right M(5) returns 0;
        //M(null)left M(5) return0;
        //M(5) left = M(null), right = M(null)
        //M(3) left = M(5)
        //M(1) left = M(3)



        //1 finishes returns 1 + 0
        //2 finishes returns 1 + 0
        //MaxDepth(null) right of 2 return 0
        //MaxDepth(3) finsihes returns 1 + 0
        //0= Math.Max(MaxDepth(null), MaxDepth(null));
        //MaxDepth(3)
        //0 = Math.Max(MaxDepth(3), MaxDepth(null))
        // Max.Depth(2) right of 1
        //3 finishes returns 1 + 0;
        //4 finishes returns 1 + 0;
        //0 = Math.Max(MaxDepth(null) returns 0, MaxDepth(null) returns 0)
        //MaxDepth(4) right of 3
        //5 finishes returns 1 + 0;
        //0 = Math.Max(MaxDepth(null) returns 0, MaxDepth(null) returns 0)
        //MaxDepth(5)
        //0 = Math.Max(MaxDepth(5), MaxDepth(4))
        //MaxDepth(3)
        //0 = Math.Max(MaxDepth(3), MaxDepth(2))
        //MaxDepth(1)



        //ITERATIVELY
        public int MaxDepthIter(TreeNode root)
        {
            if (root == null)
            {
                return(0);
            }
            int maxDepth            = 0;
            Stack <DepthNode> stack = new Stack <DepthNode>();

            stack.Push(new DepthNode()
            {
                Node = root, Depth = 1
            });
            while (stack.Count != 0)
            {
                DepthNode dn = stack.Pop();
                if (dn.Depth > maxDepth)
                {
                    maxDepth = dn.Depth;
                }
                if (dn.Node.right != null)
                {
                    stack.Push(new DepthNode()
                    {
                        Node = dn.Node.right, Depth = dn.Depth + 1
                    });
                }
                if (dn.Node.left != null)
                {
                    dn.Node = dn.Node.left;
                    dn.Depth++;
                    stack.Push(dn);
                }
            }
            return(maxDepth);
        }
Example #5
0
 public int Y(DepthNode n)
 {
     return n.depthIndex / width;
 }
Example #6
0
 public int X(DepthNode n)
 {
     return width - (n.depthIndex / width);
 }