Exemple #1
0
                public static int Get(TreeNode root)
                {
                    if (Instance == null)
                    {
                        Instance = new TreeDepth();
                    }

                    return(Instance.GetDepth(root));
                }
Exemple #2
0
        public void TreeDepthIsCalculatedCorrectly()
        {
            /* For this array, we expect a tree like :
             *              8
             *      4                  14
             *  2       6            12      16
             * 0               8
             * i.e. of height 3. */
            int[] sortedArray = new int[] { 0, 2, 4, 6, 8, 10, 12, 14, 16 };
            var   shortie     = ShortestBinaryTree <int> .Go(sortedArray);

            int depth = TreeDepth <int> .Go(shortie);

            Assert.Equal(3, depth);
        }
Exemple #3
0
            public bool IsBalanced_Solution(TreeNode pRoot)
            {
                if (pRoot == null)
                {
                    return(true);
                }

                var depthLeft  = TreeDepth.Get(pRoot.left);
                var depthRight = TreeDepth.Get(pRoot.right);
                var diff       = depthLeft - depthRight;

                if (diff > BalanceDiffMax || diff < BalanceDiffMin)
                {
                    return(false);
                }

                return(IsBalanced_Solution(pRoot.left) && IsBalanced_Solution(pRoot.right));
            }
        public TreeItemViewModel(ViewModelBase modelVm, TreeDepth type, bool isNew = false)
        {
            CurrentViewModel = modelVm;
            TreeType         = type;
            if (modelVm is BranchViewModel branch)
            {
                Name = branch.Name;
            }
            else if (modelVm is LeafViewModel leaf)
            {
                Name = leaf.Name;
            }

            if (isNew) // If the item has been newly created - then it's saveable.
            {
                IsDirty = true;
                TaskLogger.Instance.Track($"{Name} has been created!");
            }
            else
            {
                IsDirty = false;
            }
        }