zoukankan      html  css  js  c++  java
  • 二叉树 深度优先遍历

        /// <summary>
        
    /// 二叉树结点类
        
    /// </summary>
        public class BinaryTreeNode
        {
            object _data;
            BinaryTreeNode _left;
            BinaryTreeNode _right;
            
            public BinaryTreeNode Left
            {
                get
                {
                    return this._left;
                }
                set
                {
                    this._left = value;
                }
            }
            public BinaryTreeNode Right
            {
                get
                {
                    return this._right;
                }
                set
                {
                    this._right = value;
                }
            }
            
            public BinaryTreeNode(object data)
            {
                this._data = data;
            }
            
            public override string ToString()
            {
                return (null == _data)? string.Empty : _data.ToString();
            }
        }

        

         
        /// <summary> 

        /// 二叉树集合类
        
    ///</summary>publicclass BinaryTree
        {
            BinaryTreeNode _head;
            string _strBinaryTree;
            
            public BinaryTreeNode Head
            {
                get
                {
                    return this._head;
                }
            }
            void AddNode(BinaryTreeNode parent,int index)
            {
                int leftIndex = index * 2 + 1;
                if(leftIndex < _strBinaryTree.Length)
                {
                    if(_strBinaryTree[leftIndex] != '*')
                    {
                        parent.Left = new BinaryTreeNode(_strBinaryTree[leftIndex]);
                        AddNode(parent.Left,leftIndex);
                    }
                }
                int rightIndex = index * 2 + 2;
                if(rightIndex < _strBinaryTree.Length)
                {
                    if(_strBinaryTree[rightIndex] != '*')
                    {
                        parent.Right = new BinaryTreeNode(_strBinaryTree[rightIndex]);
                        AddNode(parent.Right,rightIndex);
                    }
                }
            }
            public void PerOrder(BinaryTreeNode node)
            {
                if(null != node)
                {
                    Console.Write(node);
                    PerOrder(node.Left);
                    PerOrder(node.Right);
                }
            }
            public void MidOrder(BinaryTreeNode node)
            {
                if(null != node)
                {
                    MidOrder(node.Left);
                    Console.Write(node);
                    MidOrder(node.Right);
                }
            }
            public void AfterOrder(BinaryTreeNode node)
            {
                if(null != node)
                {
                    AfterOrder(node.Left);
                    AfterOrder(node.Right);
                    Console.Write(node);
                }
            }
            public BinaryTree(string constructStr)
            {
                _strBinaryTree = constructStr;
                _head = (string.IsNullOrEmpty(_strBinaryTree)) ? null : new BinaryTreeNode(_strBinaryTree[0]);
                AddNode(_head, 0);
            }
        }

  • 相关阅读:
    (零 ) 天猫精灵接入Home Assistant-总说明
    (二 -2) 天猫精灵接入Home Assistant-自动发现Mqtt设备
    (二 -1) 天猫精灵接入Home Assistant-控制Mqtt设备
    如何在关闭ssh连接的情况下,让程序继续运行?
    hass连接设备
    (一 ) 天猫精灵接入Home Assistant-服务器搭建
    (一) 天猫精灵接入Home Assistant- hass对接天猫精灵
    (3)HomeAssistant 连接MQTT
    (2)HomeAssistant 参数配置
    (1)HomeAssistant 安装开始
  • 原文地址:https://www.cnblogs.com/kakaliush/p/2778253.html
Copyright © 2011-2022 走看看