Ejemplo n.º 1
0
    private IntervalTreeNode BuildTree(List <Line1D> lines)
    {
        if (lines.Count == 0)
        {
            return(null);
        }
        var tmpNode = new IntervalTreeNode();

        tmpNode.Mid = Median(lines);
        var rightList = new List <Line1D>();
        var leftList  = new List <Line1D>();
        var hitList   = new List <Line1D>();

        Split(lines, tmpNode.Mid, hitList, leftList, rightList);
        tmpNode.Hit   = hitList;
        tmpNode.Left  = BuildTree(leftList);
        tmpNode.Right = BuildTree(rightList);
        return(tmpNode);
    }
Ejemplo n.º 2
0
        private void button7_Click(object sender, EventArgs e)
        {
            //http://www.geeksforgeeks.org/interval-tree/
            //http://www.cs.toronto.edu/~krueger/cscB63h/lectures/tut06.txt
            //Consider a situation where we have a set of intervals and we need following operations to be implemented efficiently.
            //1) Add an interval
            //2) Remove an interval
            //3) Given an interval x, find if x overlaps with any of the existing intervals.
            //Then Interval Tree is the right sln. All the above operation is done in o(logn) time
            //Does my lifeguarding shift overlap with anyone else's shift?
            //If so, which one?
            //Given: I am a lifeguard from 9:00 to 12:00.
            //The other shifts are: 6:00 to 8:00, 10:00 to 13:00, 12:00 to 15:00
            //and 14:00 to 17:00

            //Given:{{1, 3}, {2, 4}, {10, 12}, {5, 6} {5.30,7}}
            List <Puzzles.TechCompanyPage4.Appointment> appointments = new List <Puzzles.TechCompanyPage4.Appointment>();

            appointments.Add(new Appointment(ParseStringDate("06/05/2014 01:00 PM"), ParseStringDate("06/05/2014 03:00 PM")));
            appointments.Add(new Appointment(ParseStringDate("06/05/2014 02:00 PM"), ParseStringDate("06/05/2014 04:00 PM")));
            appointments.Add(new Appointment(ParseStringDate("06/05/2014 10:00 AM"), ParseStringDate("06/05/2014 12:00 PM")));
            appointments.Add(new Appointment(ParseStringDate("06/05/2014 05:00 PM"), ParseStringDate("06/05/2014 06:00 PM")));
            appointments.Add(new Appointment(ParseStringDate("06/05/2014 05:30 PM"), ParseStringDate("06/05/2014 07:00 PM")));



            //convert everything to interval with low and hight

            List <Interval <DateTime> > intervals = new List <Interval <DateTime> >();

            foreach (var apt in appointments)
            {
                Interval <DateTime> interval = new Interval <DateTime>();
                interval.Low  = apt.StartTime;
                interval.High = apt.EndTime;
                intervals.Add(interval);
            }

            //Imagine i need to book a appointment {11am - 12 am}..check whether there will be a conflict and then pick the one
            KarthicIntervalTree <DateTime> tree = new KarthicIntervalTree <DateTime>();

            foreach (var i in intervals)
            {
                tree.Root = tree.InsertInterval(tree.Root, i);
            }

            //search the interval
            Interval <DateTime> needtobook = new Interval <DateTime>();

            needtobook.Low  = ParseStringDate("06/05/2014 11:00 AM");
            needtobook.High = ParseStringDate("06/05/2014 12:00 PM");


            Interval <DateTime> needtobook2 = new Interval <DateTime>();

            needtobook2.Low  = ParseStringDate("06/05/2014 4:00 AM");
            needtobook2.High = ParseStringDate("06/05/2014 5:30 AM");


            IntervalTreeNode <DateTime> conflict = tree.SearchInterval(tree.Root, needtobook);



            string result = string.Empty;

            if (conflict == null)
            {
                result = "No conflict";
            }
            else
            {
                result = "Conflicts with the apt" + conflict.ToString();
            }
        }
Ejemplo n.º 3
0
 public SimpleIntervalTree(List <Line1D> lines)
 {
     root = this.BuildTree(lines);
 }