zoukankan      html  css  js  c++  java
  • 二叉树:

    1.获取树的深度

    class Program
    {
        static void Main(string[] args)
        {
            Node root = new Node() { Value = 1 };
            Node level21 = new Node() { Value = 2 };
            Node level22 = new Node() { Value = 3 };
            Node level31 = new Node() { Value = 4 };
            Node level32 = new Node() { Value = 5 };
            Node level33 = new Node() { Value = 6 };
            Node level41 = new Node() { Value = 7 };
            Node level51 = new Node() { Value = 8 };
    
            root.LeftChild = level21;
            root.RightChild = level22;
            level21.LeftChild = level31;
            level22.LeftChild = level32;
            level22.RightChild = level33;
            level32.RightChild = level41;
            level41.LeftChild = level51;
    
            int treeDepth = GetTreeDepth(root);
            //int treeDepth = GetTreeDepthByLoop(root);
            Console.WriteLine(treeDepth);
            Console.ReadKey();
        }
    // 递归
    static int GetTreeDepth(Node root) { if (root == null) return 0; int leftchilDepth = GetTreeDepth(root.LeftChild); int rightchilDepth = GetTreeDepth(root.RightChild); return leftchilDepth > rightchilDepth ? leftchilDepth + 1 : rightchilDepth + 1; } // 迭代 static int GetTreeDepthByLoop(Node root) { if (root == null) return 0; int depth = 1; List<Node> list1 = new List<Node>(); List<Node> list2 = new List<Node>(); List<Node> temp; list1.Add(root); while (true) { foreach (var node in list1) { if (node.LeftChild != null) list2.Add(node.LeftChild); if (node.RightChild != null) list2.Add(node.RightChild); } if (list2.Count > 0) depth++; else return depth; list1.Clear(); temp = list1; list1 = list2; list2 = temp; } } } #region Tree class Node { public int Value { get; set; } public Node LeftChild { get; set; } public Node RightChild { get; set; } } #endregion

     2.遍历(前序,中序,后序,层序)

    // 1243576 先序
    static void PreOrder(Node root)
    {
        if (root == null)
            return;
        Console.Write(root.Value);
        PreOrder(root.LeftChild);
        PreOrder(root.RightChild);
    }
    
    // 4215736 中序
    static void InOrder(Node root)
    {
        if (root == null)
            return;
        InOrder(root.LeftChild);
        Console.Write(root.Value);
        InOrder(root.RightChild);
    }
    
    // 4275631 后序
    static void PostOrder(Node root)
    {
        if (root == null)
            return;
        PostOrder(root.LeftChild);
        PostOrder(root.RightChild);
        Console.Write(root.Value);
    }
    
    // 1234567 层序
    static void LevelOrder(Node root)
    {
        Queue<Node> nodes = new Queue<Node>();
        if (root != null)
            nodes.Enqueue(root); // 根节点入队
    
        while (nodes.Any())
        {
            var item = nodes.Dequeue(); //根节点出队
            Console.Write(item.Value);
            if (item.LeftChild != null)
                nodes.Enqueue(item.LeftChild); // 左子节点入队
            if (item.RightChild != null)
                nodes.Enqueue(item.RightChild); // 右子节点入队
        }
    }

    一般的树:

    class Node
    {
        public int Value { get; set; }
        public List<Node> Nodes { get; set; }
    }

    1.获取树的深度:

    public List<Node> Depth
    {
        get
        {
            List<Node> path = new List<Node>();
            if (Nodes != null)
            {
                foreach (Node node in Nodes)
                {
                    List<Node> tmp = node.Depth;
                    if (tmp.Count > path.Count)
                        path = tmp;
                }
            }
            path.Insert(0, this);
            return path;
        }
    }

    或者

    static int FindDepth(Node n, int depth)
    {
        if (n.Nodes == null || n == null)
        {
            return depth;
        }
    
        int maxDepth = 0;
        for (int i = 0; i < n.Nodes.Count; i++)
        {
            int d = FindDepth(n.Nodes[i], depth + 1);
            if (d > maxDepth)
            {
                maxDepth = d;
            }
        }
    
        return maxDepth;
    }

    或者

    static int GetDepth(Node root)
    {
        if (root == null)
            return 0;
    
        List<Node> list1 = new List<Node>();
        List<Node> list2 = new List<Node>();
        List<Node> temp;
    
        list1.Add(root);
        int depth = 1;
    
        while (true)
        {
            foreach (Node node in list1)
            {
                if(node.Nodes!= null && node.Nodes.Any())
                {
                    foreach (Node n in node.Nodes)
                    {
                        list2.Add(n);
                    }
                }
            }
    
            if (list2.Any())
                depth++;
            else
                return depth;
    
            list1.Clear();
            temp = list1;
            list1 = list2;
            list2 = temp;
        }
    }

    2.遍历

    static void GetAllNodes(Node root)
    {
        Console.WriteLine(root.Value);
        if (root == null || root.Nodes == null)
            return;
    
        foreach (Node node in root.Nodes)
        {
            GetAllNodes(node);
        }
    }
    把圈子变小,把语言变干净,把成绩往上提,把故事往心里收,现在想要的以后你都会有。
  • 相关阅读:
    HashMap
    Spring事务异常回滚,捕获异常不抛出就不会回滚(转载) 解决了我一年前的问题
    脏读 不可重复读 幻读
    冒泡优化
    Class.forName的作用以及为什么要用它【转】
    java反射入门
    Java异常分类 转载
    转载 【CSS进阶】伪元素的妙用--单标签之美
    转载文章 利用旋转正方形与图形的组合实现爱心
    Three.js基础学习【修改版】
  • 原文地址:https://www.cnblogs.com/jizhiqiliao/p/15623744.html
Copyright © 2011-2022 走看看