示例#1
0
        private bool IsIdeallyBalanced(BiTrNode root)
        {
            long leftTrNodeCount  = 0;
            long rightTrNodeCount = 0;

            if (root == null)
            {
                return(true);
            }

            if (root.LeftChild != null)
            {
                leftTrNodeCount = CountNodesDFS(root.LeftChild);
            }

            if (root.RightChild != null)
            {
                rightTrNodeCount = CountNodesDFS(root.RightChild);
            }

            if (Math.Abs(leftTrNodeCount - rightTrNodeCount) <= 1 &&
                IsIdeallyBalanced(root.LeftChild) == true && IsIdeallyBalanced(root.RightChild) == true)
            {
                return(true);
            }

            return(false);
        }
示例#2
0
        private int CountNodesDFS(BiTrNode root)
        {
            int nodesCount = 1;
            Stack <BiTrNode> presentNodes = new Stack <BiTrNode>();

            presentNodes.Push(root);

            while (presentNodes.Count > 0)
            {
                BiTrNode currentNode = presentNodes.Pop();
                if (currentNode.LeftChild != null)
                {
                    presentNodes.Push(currentNode.LeftChild);
                    nodesCount++;
                }

                if (currentNode.RightChild != null)
                {
                    presentNodes.Push(currentNode.RightChild);
                    nodesCount++;
                }
            }

            return(nodesCount);
        }
示例#3
0
        public static void Main(string[] args)
        {
            string   rawTree = Console.ReadLine();
            BiTrNode root    = ParseTree(rawTree);
            BiTree   tree    = new BiTree(root);

            Console.WriteLine(tree.IsIdeallyBalanced());
        }
示例#4
0
        public BiTree(BiTrNode value)
        {
            if (value == null)
            {
                throw new ArgumentNullException("Cannot insert null value!");
            }

            this.root = value;
        }
示例#5
0
        public BiTrNode(int value, BiTrNode leftChild, BiTrNode rightChild)
        {
            if (value == null)
            {
                throw new ArgumentNullException("Cannot insert null value!");
            }

            this.value     = value;
            this.LeftChild = leftChild;
        }
示例#6
0
        public BiTree(int value, BiTree leftChild, BiTree rightChild)
        {
            if (value == null)
            {
                throw new ArgumentNullException("Cannot insert null value!");
            }

            BiTrNode leftChildNode  = leftChild != null ? leftChild.root : null;
            BiTrNode rightChildNode = rightChild != null ? rightChild.root : null;

            this.root = new BiTrNode(value, leftChildNode, rightChildNode);
        }
示例#7
0
        public static BiTrNode ParseTree(string tree)
        {
            if (tree.Contains("->") == false)
            {
                int currValue = int.Parse(tree);
                return(new BiTrNode(currValue));
            }

            string cleanTree = tree.Substring(1, tree.Length - 2);

            string[] currentNodes = cleanTree.Split(new string[] { "->" }, 2, StringSplitOptions.RemoveEmptyEntries);
            BiTrNode presentNode  = new BiTrNode(int.Parse(currentNodes[0]));

            if (currentNodes[1].Contains("->") == true)
            {
                List <string> children = GetChildren(currentNodes[1]);

                if (children[0] != "x")
                {
                    presentNode.LeftChild = ParseTree(children[0]);
                }

                if (children[1] != "x")
                {
                    presentNode.RightChild = ParseTree(children[1]);
                }
            }

            else
            {
                string[] leafs = currentNodes[1].Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
                if (leafs[0] != "x")
                {
                    int leafValue = int.Parse(leafs[0]);
                    presentNode.LeftChild = new BiTrNode(leafValue);
                }

                if (leafs[1] != "x")
                {
                    int leafValue = int.Parse(leafs[0]);
                    presentNode.RightChild = new BiTrNode(leafValue);
                }
            }

            return(presentNode);
        }