Exemplo n.º 1
0
        public void ekle(int x)
        {
            BST_Node nn = null;
            BST_Node n  = root;

            while (n != null)
            {
                nn = n;

                if (x == n.data)
                {
                    return;
                }

                else if (x < n.data)
                {
                    n = n.left;
                }

                else
                {
                    n = n.right;
                }
            }

            BST_Node node = new BST_Node();

            node.data = x;
            node.left = node.right = null;

            if (root == null)
            {
                root = node;
            }

            else if (x < nn.data)
            {
                nn.left = node;
            }

            else
            {
                nn.right = node;
            }
        }
Exemplo n.º 2
0
        public void levelBul(BST_Node root, int level)
        {
            if (root == null)
            {
                return;
            }

            if (level == 1)
            {
                Console.Write(root.data + " ");
            }

            else if (level > 1)
            {
                levelBul(root.left, level - 1);
                levelBul(root.right, level - 1);
            }
        }
Exemplo n.º 3
0
        public int yukseklik(BST_Node root)
        {
            if (root == null)
            {
                return(0);
            }

            else
            {
                int lheight = yukseklik(root.left);
                int rheight = yukseklik(root.right);

                if (lheight > rheight)
                {
                    return(lheight + 1);
                }

                return(rheight + 1);
            }
        }
Exemplo n.º 4
0
        public BST_Node arama(int x)
        {
            BST_Node n = root;

            while (n != null)
            {
                if (x == n.data)
                {
                    return(n);
                }

                else if (x < n.data)
                {
                    n = n.left;
                }

                else
                {
                    n = n.right;
                }
            }

            return(null);
        }
Exemplo n.º 5
0
        public BST_Node min()
        {
            if (root == null)
            {
                Console.WriteLine("Ağaç boş...");
                return(null);
            }

            if (root.left == null)
            {
                Console.WriteLine("Minimum değer: " + root.data);
                Console.WriteLine("Atası: Yok");
                Console.WriteLine("Kardeşi: Yok");
                return(root);
            }

            if (root.left.left == null)
            {
                Console.WriteLine("Minimum değer: " + root.left.data);
                Console.WriteLine("Atası: Yok");
                Console.Write("Kardeşi: ");
                if (root.right == null)
                {
                    Console.WriteLine("Yok");
                }

                else
                {
                    Console.WriteLine(root.right.data);
                }

                return(root.left);
            }

            BST_Node  n   = root;
            ArrayList ata = new ArrayList();

            while (n.left.left != null)
            {
                ata.Add(n.data);
                n = n.left;
            }

            Console.WriteLine("Minimum değer: " + n.left.data);
            Console.Write("Atası: ");

            foreach (int i in ata)
            {
                Console.Write(i + " ");
            }

            Console.Write("\nKardeşi: ");
            if (n.right == null)
            {
                Console.WriteLine("Yok");
            }
            else
            {
                Console.WriteLine(n.right.data);
            }

            return(n.left);
        }
Exemplo n.º 6
0
        public void silme(int x)
        {
            if (root == null)
            {
                Console.WriteLine("Hata...\nAğaç boş");
                return;
            }

            BST_Node n  = root;
            BST_Node nn = null;

            while (n.left != null || n.right != null)
            {
                nn = n;

                if (x < n.data)
                {
                    n = n.left;
                }

                else if (x > n.data)
                {
                    n = n.right;
                }

                else if (x == n.data)
                {
                    break;
                }
            }

            if (n.data != x)
            {
                Console.WriteLine(x + " değeri bulunamadı...");
                return;
            }

            if (n.left == null && n.right == null)
            {
                if (x == root.data)
                {
                    root = null;
                    Console.WriteLine(x + " değeri silindi");
                    return;
                }

                if (nn.left == n)
                {
                    nn.left = null;
                }

                else if (nn.right == n)
                {
                    nn.right = null;
                }

                return;
            }

            if (n.left == null && n.right != null)
            {
                if (x == root.data)
                {
                    BST_Node rmin = root.right;

                    if (rmin.right == null)
                    {
                        n.data  = rmin.data;
                        n.right = rmin.right;
                        return;
                    }

                    while (rmin.left.left != null)
                    {
                        rmin = rmin.left;
                    }

                    n.data    = rmin.left.data;
                    rmin.left = null;
                    return;
                }

                n.data  = n.right.data;
                n.right = null;

                return;
            }

            if (n.left != null && n.right == null)
            {
                if (x == root.data)
                {
                    BST_Node lmax = root.left;

                    if (lmax.left == null)
                    {
                        n.data = lmax.data;
                        n.left = lmax.left;
                        return;
                    }

                    while (lmax.right.right != null)
                    {
                        lmax = lmax.right;
                    }

                    n.data     = lmax.right.data;
                    lmax.right = null;
                    return;
                }

                n.data = n.left.data;
                n.left = null;

                return;
            }

            if (n.left != null && n.right != null)
            {
                BST_Node lmax = n.left;

                if (lmax.right == null)
                {
                    n.data = lmax.data;
                    n.left = lmax.left;
                    return;
                }


                while (lmax.right.right != null)
                {
                    lmax = lmax.right;
                }

                n.data     = lmax.right.data;
                lmax.right = null;
                return;
            }
        }