Пример #1
0
        /*
         * To use BKTree:
         * 1. Create a class dervied from BKTreeNode
         * 2. Add a member variable of your data to be sorted / retrieved
         * 3. Override the calculateDistance method to calculate the distance metric
         *    between two nodes for the data to be sorted / retrieved.
         * 4. Instantiate a BKTree with the type name of the class created in (1).
         */

        static void Main(string[] args)
        {
            /*
             * NOTE: More comprehensive examples of BK-Tree methods in unit tests
             */

            // Exercise static distance metric methods -- just because
            Console.WriteLine(
                DistanceMetric.calculateHammingDistance(
                    new byte[] { 0xEF, 0x35, 0x20 },
                    new byte[] { 0xAD, 0x13, 0x87 }));

            Console.WriteLine(
                DistanceMetric.calculateLeeDistance(
                    new int[] { 196, 105, 48 },
                    new int[] { 201, 12, 51 }));

            Console.WriteLine(
                DistanceMetric.calculateLevenshteinDistance(
                    "kitten",
                    "sitting"));


            // Create BKTree with derived node class from top of file
            BKTree <ExampleNodeRecord> tree = new BKTree <ExampleNodeRecord>();

            // Add some nodes
            tree.add(new ExampleNodeRecord(1, new int[] { 100, 200, 300 }));
            tree.add(new ExampleNodeRecord(2, new int[] { 110, 210, 310 }));
            tree.add(new ExampleNodeRecord(3, new int[] { 120, 220, 320 }));
            tree.add(new ExampleNodeRecord(4, new int[] { 130, 230, 330 }));
            tree.add(new ExampleNodeRecord(5, new int[] { 140, 240, 340 }));

            // Get best node from our tree with best distance
            Dictionary <ExampleNodeRecord, Int32> results =
                tree.findBestNodeWithDistance(
                    new ExampleNodeRecord(new int[] { 103, 215, 303 }));

            // Get best nodes below threshold
            results = tree.query(
                new ExampleNodeRecord(new int[] { 103, 215, 303 }),
                10);  // arbitrary threshold

            // Dictionaries don't print well; so invent your own handy print routine
        }
Пример #2
0
        public void BKTree_should_FindBestNodeWithDistance()
        {
            BKTree <TestNode> tree = new BKTree <TestNode>();

            TestNode search = new TestNode(new int[] { 365, 422, 399 });
            TestNode best   = new TestNode(4, new int[] { 400, 400, 400 });

            tree.add(new TestNode(1, new int[] { 100, 100, 100 }));
            tree.add(new TestNode(2, new int[] { 200, 200, 200 }));
            tree.add(new TestNode(3, new int[] { 300, 300, 300 }));
            tree.add(best);
            tree.add(new TestNode(5, new int[] { 500, 500, 500 }));

            Dictionary <TestNode, Int32> result = tree.findBestNodeWithDistance(search);

            Assert.Equal(1, result.Count);
            Assert.Equal(58, DistanceMetric.calculateLeeDistance(search.Data, best.Data));
            Assert.Equal(58, result.Values.ElementAt(0));
            Assert.Equal(4, result.Keys.ElementAt(0).Id);
            Assert.Equal(best.Data, result.Keys.ElementAt(0).Data);
        }
Пример #3
0
        /*
         * To use BKTree:
         * 1. Create a class dervied from BKTreeNode
         * 2. Add a member variable of your data to be sorted / retrieved
         * 3. Override the calculateDistance method to calculate the distance metric 
         *    between two nodes for the data to be sorted / retrieved.
         * 4. Instantiate a BKTree with the type name of the class created in (1).
         */

        static void Main(string[] args)
        {
            /*
             * NOTE: More comprehensive examples of BK-Tree methods in unit tests
             */

            // Exercise static distance metric methods -- just because
            Console.WriteLine(
                DistanceMetric.calculateHammingDistance(
                    new byte[] { 0xEF, 0x35, 0x20 },
                    new byte[] { 0xAD, 0x13, 0x87 }));

            Console.WriteLine(
                DistanceMetric.calculateLeeDistance(
                    new int[] { 196, 105, 48 },
                    new int[] { 201, 12, 51 }));

            Console.WriteLine(
                DistanceMetric.calculateLevenshteinDistance(
                    "kitten",
                    "sitting"));


            // Create BKTree with derived node class from top of file
            BKTree<ExampleNodeRecord> tree = new BKTree<ExampleNodeRecord>();

            // Add some nodes
            tree.add( new ExampleNodeRecord( 1, new int[] {100,200,300}) );
            tree.add( new ExampleNodeRecord( 2, new int[] {110,210,310}) );
            tree.add( new ExampleNodeRecord( 3, new int[] {120,220,320}) );
            tree.add( new ExampleNodeRecord( 4, new int[] {130,230,330}) );
            tree.add( new ExampleNodeRecord( 5, new int[] {140,240,340}) );

            // Get best node from our tree with best distance
            Dictionary<ExampleNodeRecord, Int32> results = 
                tree.findBestNodeWithDistance(
                    new ExampleNodeRecord( new int[] { 103, 215, 303 }) );

            // Get best nodes below threshold
            results = tree.query(
                new ExampleNodeRecord(new int[] { 103, 215, 303 }),
                10 ); // arbitrary threshold
        
            // Dictionaries don't print well; so invent your own handy print routine
        }
Пример #4
0
        public void BKTree_should_FindBestNodeWithDistance()
        {
            BKTree<TestNode> tree = new BKTree<TestNode>();

            TestNode search = new TestNode(new int[] { 365, 422, 399 });
            TestNode best = new TestNode(4, new int[] { 400, 400, 400 });

            tree.add(new TestNode(1, new int[] { 100, 100, 100 }));
            tree.add(new TestNode(2, new int[] { 200, 200, 200 }));
            tree.add(new TestNode(3, new int[] { 300, 300, 300 }));
            tree.add(best);
            tree.add(new TestNode(5, new int[] { 500, 500, 500 }));

            Dictionary<TestNode,Int32> result = tree.findBestNodeWithDistance(search);

            Assert.Equal(1, result.Count);
            Assert.Equal(58, DistanceMetric.calculateLeeDistance(search.Data, best.Data));
            Assert.Equal(58, result.Values.ElementAt(0));
            Assert.Equal(4, result.Keys.ElementAt(0).Id);
            Assert.Equal(best.Data, result.Keys.ElementAt(0).Data);
        }