关于我们

质量为本、客户为根、勇于拼搏、务实创新

< 返回

(C#BinaryTree)基本概念和算法

发布时间:2022-07-27 13:55:09
A binary tree is defined as a tree where each node can have no more than two children.(C#BinaryTree)基本
概念和算法

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

/template/Home/DawnNew/PC/Static

立即注册风纳云账号,免费体验多款产品

立即注册