示例#1
0
        public Treenode DeleteNode_BST(int key)
        {
            if (this == null || this.ValidateBST() == false)
            {
                return(this);
            }

            if (key > this.data)
            {
                this.right = this.right.DeleteNode_BST(key);
            }

            else if (key < this.data)
            {
                this.left = this.left.DeleteNode_BST(key);
            }

            else
            {
                if (this.left == null)
                {
                    return(this.right);
                }

                if (this.right == null)
                {
                    return(this.left);
                }

                this.data = this.left.FindBiggestOnLeft();

                this.left = this.left.DeleteNode_BST(this.data);
            }
            return(this);
        }
示例#2
0
        ///////////////////////////////Trees/////////////////////////////////////////
        //↓Find Path: find the path between root and a given node in binary tree, empty list if doesn't exist
        static bool FindPath(Treenode root, int val, List <Treenode> path)
        {
            if (root == null)
            {
                return(false);
            }

            path.Add(root);

            if (root.data == val)
            {
                return(true);
            }

            //if (root.left != null)
            if (FindPath(root.left, val, path))
            {
                return(true);
            }
            //if (root.right != null)
            if (FindPath(root.right, val, path))
            {
                return(true);
            }

            path.RemoveAt(path.Count - 1);
            return(false);
        }
示例#3
0
 /// <summary>
 /// Returns hegiht of a given Treenode.
 /// </summary>
 /// <param name="root"></param>
 /// <returns></returns>
 public static int Height(Treenode root)
 {
     if (root == null)
     {
         return(0);
     }
     return(Math.Max(Height(root.left), Height(root.right)) + 1);
 }
示例#4
0
        private static int FindBiggest(Treenode root)
        {
            if (root.right == null)
            {
                return(root.data);
            }

            return(FindBiggest(root.right));
        }
示例#5
0
 private void GenerateList(Treenode root)
 {
     if (root == null)
     {
         return;
     }
     GenerateList(root.left);
     list.Add(root.data);
     GenerateList(root.right);
 }
示例#6
0
        // Path Sum2
        static List <List <int> > PathSum2(Treenode root, int sum)
        {
            if (root == null)
            {
                return(new List <List <int> >());
            }
            List <List <int> > total_path = new List <List <int> >();

            PathSum2(root, sum, new List <int>(), total_path);
            return(total_path);
        }
示例#7
0
 static bool ValidateBST(Treenode root, int minValue, int maxValue)
 {
     if (root == null)
     {
         return(true);
     }
     if (root.data >= maxValue || root.data < minValue)
     {
         return(false);
     }
     return(ValidateBST(root.left, minValue, root.data) && ValidateBST(root.right, root.data, maxValue));
 }
示例#8
0
 //////////////////////////////////////////////////////////////////////////////////////////////
 static int minimumdepth(Treenode root)
 {
     if (root == null)
     {
         return(0);
     }
     if (root.left == null && root.right == null)
     {
         return(1);
     }
     return(Math.Min(minimumdepth(root.left), minimumdepth(root.right)) + 1);
 }
示例#9
0
        public void PrintComplete(Treenode broot, PerfectTreeNode proot) //Print BinaryTree elements with corresponding spots from complete tree
        {
            if (broot == null)
            {
                return;
            }

            //proot.value = broot.data.ToString();

            Console.SetCursorPosition(proot.col, proot.row);
            Console.Write(broot.data);

            PrintComplete(broot.left, proot.right);
            PrintComplete(broot.right, proot.right);
        }
示例#10
0
        // Decodes your encoded data to tree.
        public Treenode deserialize(string data)
        {
            if (data == null)
            {
                return(null);
            }

            string[] nodes = data.Split(',');

            Treenode root = new Treenode(int.Parse(nodes[0]));

            Queue <Treenode> q = new Queue <Treenode>();

            q.Enqueue(root);

            int i = 1;

            while (q.Any())
            {
                Treenode curr = q.Dequeue();

                if (i < nodes.Length) //check left
                {
                    string left = nodes[i];
                    if (left != "n")
                    {
                        Treenode l = new Treenode(int.Parse(left));
                        curr.left = l;
                        q.Enqueue(l);
                    }
                    i++;
                }
                if (i < nodes.Length) //check right
                {
                    string right = nodes[i];
                    if (right != "n")
                    {
                        Treenode r = new Treenode(int.Parse(right));
                        curr.right = r;
                        q.Enqueue(r);
                    }
                    i++;
                }
            }
            return(root);
        }
示例#11
0
        static bool PathSum1(Treenode root, int sum, int cur_sum)
        {
            if (root == null)
            {
                return(false);
            }
            if (root.left == null && root.right == null)
            {
                cur_sum += root.data;
                if (cur_sum == sum)
                {
                    return(true);
                }
            }

            return(PathSum1(root.left, sum, cur_sum + root.data) || PathSum1(root.right, sum, cur_sum + root.data));
        }
示例#12
0
 static void PathSum2(Treenode root, int sum, List <int> cur_path, List <List <int> > total_path)
 {
     cur_path.Add(root.data);
     if (root.left == null && root.right == null)
     {
         if (sum == root.data)
         {
             total_path.Add(cur_path);
         }
     }
     if (root.left != null)
     {
         PathSum2(root.left, sum - root.data, cur_path, total_path);
     }
     if (root.right != null)
     {
         PathSum2(root.right, sum - root.data, cur_path, total_path);
     }
     cur_path.Remove(cur_path.Count - 1);
 }
示例#13
0
        /////////////////////////////////Remove a node from BST/////////////////////////////////////////////
        static Treenode DeleteNode(Treenode root, int key) //removes the given node and return the new head
        {
            if (root == null)
            {
                return(null);
            }

            if (key > root.data)
            {
                root.right = DeleteNode(root.right, key);
            }
            else if (key < root.data)
            {
                root.left = DeleteNode(root.left, key);
            }

            else
            {
                if (root.left == null)
                {
                    return(root.right);
                }
                else if (root.right == null)
                {
                    return(root.left);
                }

                //if the code reaches here, it means the root has two children!
                //we can either find biggest node of left or smallest node on right, and upadte root's value with that and remove it.
                //here we do it with left's biggest
                root.data = FindBiggest(root.left);

                //recursively remove the biggest node on left
                root.left = DeleteNode(root.left, root.data);
            }
            return(root);
        }
示例#14
0
 public BSTIterator(Treenode root)
 {
     list = new List <int>();
     GenerateList(root);
     next = 0;
 }
示例#15
0
        // Encodes a tree to a single string.
        public string serialize(Treenode root)
        {
            if (root == null)
            {
                return(null);
            }
            int height         = Height(root);
            Queue <Treenode> q = new Queue <Treenode>();

            q.Enqueue(root);
            int           level = 1;
            List <string> nodes = new List <string>();

            while (q.Any())          //while q is not empty
            {
                int count = q.Count; //count of elements in this new level
                while (count > 0)
                {
                    Treenode curr = q.Dequeue();
                    if (curr == null)
                    {
                        nodes.Add("n");
                    }
                    else
                    {
                        nodes.Add(curr.data.ToString());

                        if (curr.left == null)
                        {
                            if (level < height)
                            {
                                q.Enqueue(null);
                            }
                        }
                        else
                        {
                            q.Enqueue(curr.left);
                        }

                        if (curr.right == null)
                        {
                            if (level < height)
                            {
                                q.Enqueue(null);
                            }
                        }
                        else
                        {
                            q.Enqueue(curr.right);
                        }
                    }
                    count--;
                }
                level++;
            }

            while (nodes[nodes.Count - 1] == "n")
            {
                nodes.RemoveAt(nodes.Count - 1);
            }

            string result = "";

            for (int i = 0; i < nodes.Count; i++)
            {
                result += nodes[i];
                if (i != nodes.Count - 1)
                {
                    result += ",";
                }
            }
            return(result);
        }
示例#16
0
 public int CompareTo(Treenode obj)
 {
     return(data.CompareTo(obj.data));
 }
示例#17
0
 ////////////////// Path Sums /////////////////////
 // Path Sum 1 (https://leetcode.com/problems/path-sum/)
 static bool PathSum1(Treenode root, int sum)
 {
     return(PathSum1(root, sum, 0));
 }
示例#18
0
 /////////////////////////////////////////////////////////////////////////////////
 static bool ValidateBST(Treenode root)
 {
     return(ValidateBST(root, int.MinValue, int.MaxValue));
 }
示例#19
0
 public Treenode(int d)
 {
     data  = d;
     left  = null;
     right = null;
 }
示例#20
0
 public int Height()
 {
     return(Treenode.Height(this));
 }