コード例 #1
0
        private void Postorder(DSTreeNode <T> node)
        {
            if (node.Left != null)
            {
                Postorder(node.Left);
            }

            if (node.Right != null)
            {
                Postorder(node.Right);
            }

            _TempDataConsole.Add(node.Data);
        }
コード例 #2
0
        private void traverseLevel(DSTreeNode <T> node, int level)
        {
            if (node != null)
            {
                if (level == 1)
                {
                    this._TempDataConsole.Add(node.Data);
                    return;
                }

                this.traverseLevel(node.Left, level - 1);
                this.traverseLevel(node.Right, level - 1);
            }
        }
コード例 #3
0
        public void Add(T value)
        {
            /// check if we have root?
            /// if not then add new root element and then return.
            /// If yes then traverse its children from left to right. find first free spot and add the node in that spot.

            DSTreeNode <T> newNode = new DSTreeNode <T>(value);

            if (this.Head == null)
            {
                this.Head = newNode;
                return;
            }

            DSQueue <DSTreeNode <T> > dSQueue = new DSQueue <DSTreeNode <T> >();

            dSQueue.Enqueue(this.Head);

            while (dSQueue.Count > 0)
            {
                var current = dSQueue.Dequeue();

                if (current.Data.Left == null)
                {
                    current.Data.Left = newNode;
                    break;
                }
                else
                {
                    dSQueue.Enqueue(current.Data.Left);
                }

                if (current.Data.Right == null)
                {
                    current.Data.Right = newNode;
                    break;
                }
                else
                {
                    dSQueue.Enqueue(current.Data.Right);
                }
            }
        }
コード例 #4
0
        private int findHeightOfTree(DSTreeNode <T> node)
        {
            if (node == null)
            {
                return(0);
            }

            int hLeft  = findHeightOfTree(node.Left);
            int hRight = findHeightOfTree(node.Right);

            if (hRight > hLeft)
            {
                return(hRight + 1);
            }
            else
            {
                return(hLeft + 1);
            }
        }
コード例 #5
0
        private void traverseUsingQueue(DSTreeNode <T> node, DSQueue <DSTreeNode <T> > queue, List <T> newList)
        {
            while (queue.Count != 0)
            {
                var current = queue.Dequeue();

                newList.Add(current.Data.Data);

                if (current.Data.Left != null)
                {
                    queue.Enqueue(current.Data.Left);
                }

                if (current.Data.Right != null)
                {
                    queue.Enqueue(current.Data.Right);
                }
            }
        }