예제 #1
0
        public void add(int value)
        {
            if (top == null)
            {
                Node newNode = new Node(value);
                top = newNode;
                return;
            }
            Node currNode = top;
            bool added = false;
            do
            {
                if (value < currNode.Value)
                {
                    if (currNode.Left == null)
                    {
                        Node newNode = new Node(value);
                        currNode.Left = newNode;
                        added = true;
                    }
                    else
                    {
                        currNode = currNode.Left;
                    }
                }
                if (value >= currNode.Value)
                {
                    if (currNode.Right == null)
                    {
                        Node newNode = new Node(value);
                        currNode.Right = newNode;
                        added = true;

                    }
                    else
                    {
                        currNode = currNode.Right;
                    }

                }

            } while (!added);
        }
예제 #2
0
 public Tree(int initial)
 {
     top = new Node(initial);
 }
예제 #3
0
 public Tree()
 {
     top = null;
 }
예제 #4
0
 public Node(int initial)
 {
     Value = initial;
     Left = null;
     Right = null;
 }
예제 #5
0
        private void addR(ref Node n, int value)
        {
            if (n == null)
            {
                Node newNode = new Node(value);
                n = newNode;
                return;

            }
            if (value < n.Value)
            {
                addR(ref n.Left, value);
                return;
            }
            if (value >= n.Value)
            {
                addR(ref n.Right, value);
                return;
            }
        }
예제 #6
0
        public void Print_PreOrder(Node N, ref string s)
        {
            if (N == null)
            {
                N = top;
                s = s + N.Value.ToString().PadLeft(3);
            }

            if (N.Left != null)
            {
                Print_InOrder(N.Left, ref s);
                s = s + N.Value.ToString().PadLeft(3);
            }
            if (N.Right != null)
            {
                Print_InOrder(N.Right, ref s);
            }
        }