Exemplo n.º 1
0
        private BoltNode assembleTree(List<Nut> nuts, List<Bolt> bolts)
        {
            // Termination condition
            if (nuts.Count == 0)
                return null;
            if (nuts.Count == 1)
            {
                return new BoltNode(bolts[0], nuts[0]);
            }

            Bolt pivotBolt = bolts[0];
            Nut pivotNut = nuts.Where(nut => nut.Size == pivotBolt.Size).ToArray()[0];

            nuts.Remove(pivotNut);
            bolts.Remove(pivotBolt);

            List<Nut> smallerNuts = nuts.Where(nut => nut.Size < pivotBolt.Size).ToList();
            List<Nut> largerNuts = nuts.Where(nut => nut.Size > pivotBolt.Size).ToList();
            List<Bolt> smallerBolts = bolts.Where(bolt => bolt.Size < pivotNut.Size).ToList();
            List<Bolt> largerBolts = bolts.Where(bolt => bolt.Size > pivotNut.Size).ToList();


            BoltNode node = new BoltNode(pivotBolt, pivotNut);

            node.left = assembleTree(smallerNuts, smallerBolts);
            node.right = assembleTree(largerNuts, largerBolts);


            return node;
        }
Exemplo n.º 2
0
 private void printTree(BoltNode node)
 {
     if (node != null)
     {
         printTree(node.left);
         Console.WriteLine(node.bolt.ToString() + " "  + node.nut.ToString());
         printTree(node.right);
     }
 }