コード例 #1
0
 /// <summary>
 /// 后序遍历
 /// </summary>
 public static void ForEachLRD(DoubleLinkTreeNode <T> node)
 {
     if (node == null)
     {
         return;
     }
     ForEachLRD(node.LeftChild);
     ForEachLRD(node.RightChild);
     Console.WriteLine(node.Data);
 }
コード例 #2
0
        /// <summary>
        /// 删除右节点
        /// </summary>
        /// <param name="node"></param>
        /// <returns></returns>
        public static DoubleLinkTreeNode <T> DeleteRight(DoubleLinkTreeNode <T> node)
        {
            if (node?.RightChild == null)
            {
                return(null);
            }
            var result = node.RightChild;

            node.RightChild = null;
            return(result);
        }
コード例 #3
0
 /// <summary>
 /// 计算叶子节点的数量
 /// </summary>
 /// <param name="node"></param>
 /// <returns></returns>
 public static int GetLeafCount(DoubleLinkTreeNode <T> node)
 {
     if (node == null)
     {
         return(0);
     }
     else if (node.LeftChild == null && node.RightChild == null)
     {
         return(1);
     }
     else
     {
         return(GetLeafCount(node.LeftChild) + GetLeafCount(node.RightChild));
     }
 }
コード例 #4
0
 /// <summary>
 /// 计算深度
 /// </summary>
 /// <param name="node"></param>
 /// <returns></returns>
 public static int GetDepth(DoubleLinkTreeNode <T> node)
 {
     if (node == null)
     {
         return(0);
     }
     else if (node.LeftChild == null && node.RightChild == null)
     {
         return(1);
     }
     else
     {
         var left  = GetDepth(node.LeftChild);
         var right = GetDepth(node.RightChild);
         return((left > right ? left : right) + 1);
     }
 }
コード例 #5
0
        /// <summary>
        /// 层序遍历
        /// </summary>
        public static void ForEachLevel(DoubleLinkTreeNode <T> node)
        {
            if (node == null)
            {
                return;
            }
            var queue = new Queue <DoubleLinkTreeNode <T> >();

            queue.Enqueue(node);
            while (queue.Count() != 0)
            {
                var item = queue.Dequeue();
                Console.WriteLine(item.Data);
                if (item.LeftChild != null)
                {
                    queue.Enqueue(item.LeftChild);
                }
                if (item.RightChild != null)
                {
                    queue.Enqueue(item.RightChild);
                }
            }
        }
コード例 #6
0
 /// <summary>
 /// 插入右节点,当前右节点作为新节点的左节点
 /// </summary>
 /// <param name="data"></param>
 /// <param name="node"></param>
 public static void InsertRight(T data, DoubleLinkTreeNode <T> node)
 {
     node.RightChild = new DoubleLinkTreeNode <T>(data, node.RightChild, null);
 }
コード例 #7
0
 public DoubleLinkTreeNode(T data, DoubleLinkTreeNode <T> left, DoubleLinkTreeNode <T> right)
 {
     Data       = data;
     LeftChild  = left;
     RightChild = right;
 }