The majority of Scottish Monarchs can be fitted into three main dynasties: the House of Alpin, the House of Dunkeld (or Canmore) and the House of Stewart.
But from 1292 to 1371 things were a little less clear cut. The death of Margaret, Maid of Norway, in 1290 terminated the House of Dunkeld and almost led to civil war between the Bruce family and Comyn family as they vied for the crown. The choice between what ended up as 13 competitors for the crown of Scotland fell to Edward I of England.
Edward and his Court of Assessors eventually had to choose between two "finalists" John Balliol and Robert Bruce. Balliol was the grandson of the eldest daughter of David, Earl of Huntingdon, who in turn was the younger brother of William I. Bruce was the son of the second daughter of the same David, Earl of Huntingdon. This meant that though Balliol's claim could be argued to be stronger because it came from an eldest daughter, Bruce's could be argued to be stronger because he was one generation nearer to the Earl of Huntingdon, albeit via a younger daughter of the Earl.
Edward chose John Balliol, probably correctly in law. After Balliol and the Scottish nobles had failed to do his bidding, Edward then removed Balliol, effectively governing Scotland as a province of England. In 1306 the grandson of Robert Bruce the Competitor, also called Robert, took the crown for himself after murdering John III Comyn, despite the fact that in many Scottish eyes, John Balliol remained king. Over the following two decades Robert the Bruce finally re-established Scotland as a sovereign state.
Robert was succeeded by his son, the fairly ineffectual David II. He spent large chunks of his life abroad, in France for refuge as a child, and in England as a prisoner later on. His reign was also punctuated by frequent incursions by Edward Balliol, son of John, who with English backing actually managed to crown himself king of Scotland at one point.
So these two generations of Balliols and Bruces were intertwined, occupying the throne for overlapping periods. David II's death without children brought his uncle, Robert II to the throne, the first king of the House of Stewart.