• 二叉树


    using System;
    using System.Collections.Generic;
    
    namespace BinaryTree
    {
        class Program
        {
            static void Main(string[] args)
            {
                var a = new BinaryTree("ABCDEF");
                a.First(a.Head);
                Console.WriteLine();
                a.Level();
                Console.ReadLine();
            }
        }
    
        public class Node
        {
            private object _data;
            public Node(object data)
            {
                _data = data;
            }
    
            public Node Left { get; set; }//左子结点
    
            public Node Right { get; set; }//右子结点
    
            public override string ToString()
            {
                return _data.ToString();
            }
        }
    
        public class BinaryTree
        {
            private Node _head;
            private string _cStor;
            public Node Head
            {
                get { return _head; }
            }
    
            public BinaryTree(string str)
            {
                _cStor = str;
                _head = new Node(_cStor[0]);
                Add(_head,0);
    
            }
    
            public void Add(Node parent,int index)
            {
                int left = index*2 + 1;
                if(left < _cStor.Length)
                {
                    if(_cStor[left] != '#')
                    {
                        parent.Left = new Node(_cStor[left]);
                        Add(parent.Left,left);
                    }
                }
    
                int right = index*2 + 2;
                if(right < _cStor.Length)
                {
                    if(_cStor[right] != '#')//#为空结点
                    {
                        parent.Right = new Node(_cStor[right]);
                        Add(parent.Right,right);
                    }
                }
            }
    
            public void First(Node parent)//深度优先遍历
            {
                if (parent != null)
                {
                    Console.Write(parent);
                    First(parent.Left);
                    First(parent.Right);
                }
            }
    
            public void Level()//广度优先遍历
            {
                var queue = new Queue<Node>();
                queue.Enqueue(_head);
               
                while (queue.Count > 0)
                {
                    var node = queue.Dequeue();
                    Console.Write(node);
                    if(node.Left != null)
                        queue.Enqueue(node.Left);
                    if(node.Right != null)
                        queue.Enqueue(node.Right);
                }
            }
        }
    }
  • 相关阅读:
    python 多进程队列数据处理
    python mqtt 客户端实现
    elasticsearch 父子关系
    elasticsearch Mapping使用自定义分词器
    elk 解决浏览器跨域问题
    elasticsearch 英文数字组合字符串模糊检索
    elasticsearch 关联单词查询以及Shingles
    elasticsearch 分析器 分词器
    Leetcode: Binary Tree Postorder Traversal
    Leetcode: Binary Tree Preorder Traversal
  • 原文地址:https://www.cnblogs.com/abyss0303/p/3727976.html
Copyright © 2020-2023  润新知