二叉树:

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);
    }
}

 

posted @ 2021-11-30 13:13  Kyle0418  阅读(33)  评论(0编辑  收藏  举报