zoukankan      html  css  js  c++  java
  • (C# Binary Tree) 基本概念和算法

    A binary tree is defined as a tree where each node can have no more than two children.

    Building a Binary Search Tree:

    首先创建一个节点Class

        public class BtNode
        {
            public int Data { get; set; }
    
            public BtNode Left { get; set; }
    
            public BtNode Right { get; set; }
    
            public void DisplayNode()
            {
                Console.Write("Data: {0}, ", this.Data); 
            }
        }

    然后创建BST Class 提供Insert 方法:

        public class BinarySearchTree
        {
            private BtNode root = null; 
    
            public BtNode Root
            {
                get
                {
                    return this.root; 
                }
                set
                {
                    this.root = value; 
                }
            }
    
            public void Insert(int data)
            {
                BtNode newNode = new BtNode();
                newNode.Data = data; 
                if (this.root == null)
                {
                    this.root = newNode; 
                }
                else
                {
                    // Add to children nodes.
                    BtNode current = this.root; 
                    BtNode parent = new BtNode(); 
    
                    while(true)
                    {
                        parent = current; 
                        if (data < current.Data)
                        {
                            current = current.Left; 
                            if (current == null )
                            {
                                parent.Left = newNode; 
                                break; 
                            }
                            
                        }
                        else
                        {
                            current = current.Right; 
                            if (current == null)
                            {
                                parent.Right = newNode;
                                break; 
                            }
                        }
                    }
                }
            }
        }

    Level traverse the binary tree.

    思路就是用 Queue (FIFO),从左到右扫描节点,一个节点出队列,就将其节点的左右孩子入队。

            public static void PrintLevelTracversal(BtNode root)
            {
                if (root == null)
                {
                    throw new Exception("Binary true is null!"); 
                }
    
                Queue<BtNode> queueBtNode = new Queue<BtNode>();
                queueBtNode.Enqueue(root);
                BtNode currentNode = root;  
    
                // Dequeue the node from left to right and put its left/right children into queue.
                while(currentNode != null)
                {
                    currentNode = queueBtNode.Dequeue();
                    currentNode.DisplayNode(); 
    
                    if (currentNode.Left != null)
                    {
                        queueBtNode.Enqueue(currentNode.Left); 
                    }
    
                    if (currentNode.Right != null)
                    {
                        queueBtNode.Enqueue(currentNode.Right); 
                    }
                }
            }

    Level traverse and print nodes as Zigzag.

     思路: 用2个Stuck, 一个保存Current Level, 另一个保存Next Level.

    当一层 扫完后,将下一层copy到当前层。

           // Non-Recursive method.  Use two stacks. 
            public static void PrintZigzagLevelTraversal(BtNode root)
            {
                if (root == null)
                {
                    throw new Exception("Binary tree is null!"); 
                }
    
                Stack<BtNode> currentLevelStack = new Stack<BtNode>();
                Stack<BtNode> nextLevelStack = new Stack<BtNode>();
                BtNode currentBtNode = new BtNode(); 
    
                int level = 0;
                currentLevelStack.Push(root); 
               
                while(currentLevelStack.Count > 0)
                {
                    // Display current node data.
                    currentBtNode = currentLevelStack.Pop();
                    currentBtNode.DisplayNode(); 
    
                    if (level % 2 == 0)  // even level 
                    {
                        if (currentBtNode.Left != null)
                        {
                            nextLevelStack.Push(currentBtNode.Left);
                        }
                        if (currentBtNode.Right != null)
                        {
                            nextLevelStack.Push(currentBtNode.Right);
                        }
                    }
                    else
                    {
                        if (currentBtNode.Right != null)
                        {
                            nextLevelStack.Push(currentBtNode.Right);
                        }
                        if (currentBtNode.Left != null)
                        {
                            nextLevelStack.Push(currentBtNode.Left);
                        }
                    }
    
                    // Move data from next level to current level stack. 
                    if (nextLevelStack.Count > 0 && currentLevelStack.Count == 0)
                    {
                        BtNode[] nodes = new BtNode[nextLevelStack.Count];
                        nextLevelStack.CopyTo(nodes, 0);
                        for (int i = nodes.Length - 1; i >= 0; i--)
                        {
                            currentLevelStack.Push(nodes[i]); 
                        }
                        nextLevelStack.Clear(); 
                        level++; 
                    }
                }
            }
     
  • 相关阅读:
    SpringCloud组件
    Lambda遍历Map集合
    转换为base64格式
    控制器注解参数判空
    SpringBoot项目使用环境变量当做端口号
    使用控制台占位符输出日志, 使用占位符Plus
    Java 传无限参数
    将yyyy-MM-dd hh:mm:ss转换为时间戳
    HTML使用svg,定义.svg格式
    演示js异步,同步请求,响应解码
  • 原文地址:https://www.cnblogs.com/fdyang/p/4981175.html
Copyright © 2011-2022 走看看