C# Display a Binary Search Tree in Console

前端 未结 3 1486
我寻月下人不归
我寻月下人不归 2021-01-31 04:27

I have simple binary search tree

public class BNode
{
    public int item;
    public BNode right;
    public BNode left;

    public BNode(int item)
    {
             


        
3条回答
  •  青春惊慌失措
    2021-01-31 04:53

    This is my take at it:

    I've added PrintPretty to BNode, and I've removed the second Print function you had in BTree.

    (Edit: I made the tree more lisible by changing the original chars to draw the branches of the tree)

        static void Main(string[] args)
        {
            BTree btr = new BTree();
            btr.Add(6);
            btr.Add(2);
            btr.Add(3);
            btr.Add(11);
            btr.Add(30);
            btr.Add(9);
            btr.Add(13);
            btr.Add(18);
    
            btr.Print();
    
        }
    
        public class BNode
        {
            public int item;
            public BNode right;
            public BNode left;
    
            public BNode(int item)
            {
                this.item = item;
            }
    
            public void PrintPretty(string indent, bool last)
            {
    
                Console.Write(indent);
                if (last)
                {
                    Console.Write("└─");
                    indent += "  ";
                }
                else
                {
                    Console.Write("├─");
                    indent += "| ";
                }
                Console.WriteLine(item);
    
                var children = new List();
                if (this.left != null)
                    children.Add(this.left);
                if (this.right != null)
                    children.Add(this.right);
    
                for (int i = 0; i < children.Count; i++)
                    children[i].PrintPretty(indent, i == children.Count - 1);
    
            }
    
        }
    
        public class BTree
        {
            private BNode _root;
            private int _count;
            private IComparer _comparer = Comparer.Default;
    
    
            public BTree()
            {
                _root = null;
                _count = 0;
            }
    
    
            public bool Add(int Item)
            {
                if (_root == null)
                {
                    _root = new BNode(Item);
                    _count++;
                    return true;
                }
                else
                {
                    return Add_Sub(_root, Item);
                }
            }
    
            private bool Add_Sub(BNode Node, int Item)
            {
                if (_comparer.Compare(Node.item, Item) < 0)
                {
                    if (Node.right == null)
                    {
                        Node.right = new BNode(Item);
                        _count++;
                        return true;
                    }
                    else
                    {
                        return Add_Sub(Node.right, Item);
                    }
                }
                else if (_comparer.Compare(Node.item, Item) > 0)
                {
                    if (Node.left == null)
                    {
                        Node.left = new BNode(Item);
                        _count++;
                        return true;
                    }
                    else
                    {
                        return Add_Sub(Node.left, Item);
                    }
                }
                else
                {
                    return false;
                }
            }
    
            public void Print()
            {
                _root.PrintPretty("", true);
            }
    
        }
    

    This is the result (more compact, as I mentioned):


    Edit: the following code has been modified in order to show the info about left-right:

        static void Main(string[] args)
        {
            BTree btr = new BTree();
            btr.Add(6);
            btr.Add(2);
            btr.Add(3);
            btr.Add(11);
            btr.Add(30);
            btr.Add(9);
            btr.Add(13);
            btr.Add(18);
    
            btr.Print();
    
        }
    
        public enum NodePosition
        {
            left,
            right,
            center
        }
    
        public class BNode
        {
            public int item;
            public BNode right;
            public BNode left;
    
            public BNode(int item)
            {
                this.item = item;
            }
    
            private void PrintValue(string value, NodePosition nodePostion)
            {
                switch (nodePostion)
                {
                    case NodePosition.left:
                        PrintLeftValue(value);
                        break;
                    case NodePosition.right:
                        PrintRightValue(value);
                        break;
                    case NodePosition.center:
                        Console.WriteLine(value);
                        break;
                    default:
                        throw new NotImplementedException();
                }
            }
    
            private void PrintLeftValue(string value)
            {
                Console.ForegroundColor = ConsoleColor.Magenta;
                Console.Write("L:");
                Console.ForegroundColor = (value == "-") ? ConsoleColor.Red : ConsoleColor.Gray;
                Console.WriteLine(value);
                Console.ForegroundColor = ConsoleColor.Gray;
            }
    
            private void PrintRightValue(string value)
            {
                Console.ForegroundColor = ConsoleColor.Green;
                Console.Write("R:");
                Console.ForegroundColor = (value == "-") ? ConsoleColor.Red : ConsoleColor.Gray;
                Console.WriteLine(value);
                Console.ForegroundColor = ConsoleColor.Gray;
            }
    
            public void PrintPretty(string indent, NodePosition nodePosition, bool last, bool empty)
            {
    
                Console.Write(indent);
                if (last)
                {
                    Console.Write("└─");
                    indent += "  ";
                }
                else
                {
                    Console.Write("├─");
                    indent += "| ";
                }
    
                var stringValue = empty ? "-" : item.ToString();
                PrintValue(stringValue, nodePosition);
    
                if(!empty && (this.left != null || this.right != null))
                {
                    if (this.left != null)
                        this.left.PrintPretty(indent, NodePosition.left, false, false);
                    else
                        PrintPretty(indent, NodePosition.left, false, true);
    
                    if (this.right != null)
                        this.right.PrintPretty(indent, NodePosition.right, true, false);
                    else
                        PrintPretty(indent, NodePosition.right, true, true);
                }
            }
    
        }
    
        public class BTree
        {
            private BNode _root;
            private int _count;
            private IComparer _comparer = Comparer.Default;
    
    
            public BTree()
            {
                _root = null;
                _count = 0;
            }
    
    
            public bool Add(int Item)
            {
                if (_root == null)
                {
                    _root = new BNode(Item);
                    _count++;
                    return true;
                }
                else
                {
                    return Add_Sub(_root, Item);
                }
            }
    
            private bool Add_Sub(BNode Node, int Item)
            {
                if (_comparer.Compare(Node.item, Item) < 0)
                {
                    if (Node.right == null)
                    {
                        Node.right = new BNode(Item);
                        _count++;
                        return true;
                    }
                    else
                    {
                        return Add_Sub(Node.right, Item);
                    }
                }
                else if (_comparer.Compare(Node.item, Item) > 0)
                {
                    if (Node.left == null)
                    {
                        Node.left = new BNode(Item);
                        _count++;
                        return true;
                    }
                    else
                    {
                        return Add_Sub(Node.left, Item);
                    }
                }
                else
                {
                    return false;
                }
            }
    
            public void Print()
            {
                _root.PrintPretty("", NodePosition.center, true, false);
            }
    
        }
    

    The result:

提交回复
热议问题