Quantcast
Channel: Windows Presentation Foundation (WPF) forum
Viewing all articles
Browse latest Browse all 18858

unable to add a node in avl_tree.

$
0
0

Hello everybody!
I have written a code in C# for the implementation of AVL_trees. I am having some problems with nodes that's why I am unable to insert data in the nodes. Below is my whole code.

using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Windows;
    using System.Windows.Controls;
    using System.Windows.Data;
    using System.Windows.Documents;
    using System.Windows.Input;
    using System.Windows.Media;
    using System.Windows.Media.Imaging;
    using System.Windows.Shapes;
    using System.ServiceModel;

    namespace DataProject
    {
    /// <summary>
    /// Interaction logic for Student_database.xaml
    /// </summary>
    /// 
       public class Studentdb
       {
        public int student_id;
        public char[] Student_name=new char[20];
        public char[] dob=new char[10];
        public char gender;
        public char[] department=new char[5];
       }

   
       public class avl_node:Studentdb
       {
        public Studentdb data;
        public avl_node left;
        public avl_node right;
        public int height;
        
       }
        

       public class avl_methods : avl_node
       {
        
        
        
        public int Height(avl_node a_node)
        {
            a_node = new avl_node();
            if (a_node == null)
            {
                return -1;
            }
            else
            {
                return a_node.height;
            }
        }


        
        public avl_node insert(Studentdb x, avl_node T)
        {
            if(T==null)
            {
                T = new avl_node();
                T.data = x;
                T.left = T.right = null;
                T.height = 0;
               
            }

            else if(x.student_id<T.student_id)
            {
                T.left=insert(x,T.left);
                if ((Height(T.left) - Height(T.right)) == 2)
                {
                    if (x.student_id < T.left.student_id)
                    {
                        SingleRotateWithLeft(T);
                    }
                    else
                    {
                        DoubleRotateWithLeft(T);
                    }

                }
                
            }


            else if(x.student_id>T.student_id)
            {
                T.right = insert(x, T.right);
                if ((Height(T.right) - Height(T.left)) == 2)
                {
                    if (x.student_id > T.right.student_id)
                    {
                        SingleRotateWithRight(T);
                    }
                    else
                    {
                        DoubleRotateWithRight(T);
                    }

                }
            }

            return T;

        }


        public avl_node SingleRotateWithLeft(avl_node k2)
        {
            avl_node k1;
            k1 = k2.left;
            k2.left = k1.right;
            k1.right = k2;
            k2.height = max(Height(k2.left), Height(k2.right)) + 1;
            k1.height = max(Height(k1.left), k2.height) + 1;
            return k1;

        }

        
        
        public avl_node SingleRotateWithRight(avl_node k1)
        {
            avl_node k2;
            k2 = k1.right;
            k1.right = k2.left;
            k2.left = k1;
            k1.height = max(Height(k1.left), Height(k1.right)) + 1;
            k2.height = max(k1.height, Height(k2.right)) + 1;
            return k2;
        }



        public avl_node DoubleRotateWithLeft(avl_node k3)
        {
            k3.left = SingleRotateWithRight(k3.left);
            return (SingleRotateWithLeft(k3)); 

        }


        public avl_node DoubleRotateWithRight(avl_node k1)
        {
            k1.right = SingleRotateWithLeft( k1.right );
            return (SingleRotateWithRight(k1));
        }

        
        public avl_node Search(int x,avl_node T)
        {
            if (T == null)
            {
                return null;
            }
            if (x < T.student_id)
            {
                return (Search(x, T.left));
            }
            else if (x > T.student_id)
            {
                return (Search(x, T.right));
            }
            else
            {
                return T;
            }

        }

        
        
        public avl_node Delete(Studentdb x, avl_node T)
        {
            avl_node tmp_cell;
            avl_node child= new avl_node();
            if (T==null)
            {
                MessageBox.Show("Element not found!", "Alert", MessageBoxButton.OK,                    MessageBoxImage.Exclamation);
            }
            else if(x.student_id < T.student_id)
            {
                T.left = Delete(x, T.left);
            }
            else if(x.student_id > T.student_id)
            {
                T.right = Delete(x, T.right);
            }
            else
                if (Convert.ToBoolean(T.left.student_id) && Convert.ToBoolean(T.right.student_id))
                {
                    tmp_cell = find_min(T.right);
                    T.data = tmp_cell.data;
                    T.right = Delete(T.data, T.right); 

                }
            else
                {
                    tmp_cell = T;
                    if (T.left == null)         
                        child = T.right;
                    if (T.right == null)      
                        child = T.left;
                    int a = Convert.ToInt32(tmp_cell);
                    GC.Collect(a);
                    return child; 

                }
            return T;
        }


        
        
        public int max(int a, int b)
        {
            if(a>b)
            {
                return a;
            }
            else
            {
                return b;
            }
        }

        
        public int min(int a, int b)
        {
            if (a<b)
            {
                return b;
            }
            else
            {
                return a;
            }
        }

        
        
        public avl_node find_min(avl_node T)
        {
            if(T==null)
            {
                return null;
            }
            else if(T.left==null)
            {
                return T;
            }
            else
            {
                return (find_min(T.left));
            }
        }

        
        
        public avl_node find_max(avl_node T)
        {
            if (T == null)
            {
                return null;
            }
            else if (T.right == null)
            {
                return T;
            }
            else
            {
                return (find_min(T.right));
            }
        }

       

        
            
    }



    public partial class Student_database : Window
    {

        private MainWindow m_parent;
        public void Login(MainWindow parent)
        {
            m_parent = parent;
            return;
        }

        private void Success()   //Show the new Window
        {
            m_parent.Show();
           
        }
        
        
        public Student_database()
        {
            InitializeComponent();
        }

        public avl_node root;

        public void AVLroot()
        {
            root.data = null;
            root.left = null;
            root.right = null;
            root.height = 0;
        }

        private void button1_Click(object sender, RoutedEventArgs e)
        {
            Studentdb stdb = new Studentdb();
            char[] arr;
            
            stdb.student_id =Convert.ToInt32(textBox1.Text);
            arr= textBox2.Text.ToCharArray();
            stdb.Student_name = arr;
            
            arr= textBox3.Text.ToCharArray();
            stdb.dob = arr;
            stdb.gender = Convert.ToChar(textBox4.Text);
            arr = textBox5.Text.ToCharArray();
            stdb.department = arr;

            avl_methods avlm = new avl_methods();
            avl_node a=avlm.insert(stdb,root);

            textBox1.Clear();
            textBox2.Clear();
            textBox3.Clear();
            textBox4.Clear();
            textBox5.Clear();

            MessageBox.Show(String.Format("{0}",a.gender));
        }

        private void button2_Click(object sender, RoutedEventArgs e)
        {
           
            Studentdb stdb = new Studentdb();
            avl_methods avlm = new avl_methods();
            int a = Convert.ToInt32(textBox6.Text);
            avl_node avln = avlm.Search(a, root);

            if (avln.data.student_id == a)
            {
                MessageBox.Show("hi");
                //textBox1.Text = avln.student_id.ToString();
                //textBox2.Text = avln.Student_name.ToString();
                //textBox3.Text = avln.gender.ToString();
                //textBox4.Text = avln.dob.ToString();
                //textBox5.Text = avln.department.ToString();
            }

            
        }
        
    }
}

When I am inserting a node in the insert function, I am receiving a node in return from the function. I have stored it in variable 'a' having the data type avl_node, then I used the messagebox to view an item from that node. (The purpose of all this was to check whether my data entered correctly or not but, nothing is viewed...All I can see is an empty messagebox)....

Hoping for a better response!

Regards

Umer


Viewing all articles
Browse latest Browse all 18858

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>