m8 - Lecture C8: Trees part II Response to 'Muddiest Part...

Info iconThis preview shows pages 1–3. Sign up to view the full content.

View Full Document Right Arrow Icon
Lecture C8: Trees part II Response to 'Muddiest Part of the Lecture Cards' (16 respondents) 1) Is the tree in the “Depth first” example actually a tree? (It has 6 vertices and 8 edges – a tree has N verticed and N-1 edges) ? The algorithm/example is finding the ‘minimum spanning tree’ for a connected weighted graph (this graph has in example used in lecture slides, 6 vertices and 8 edges). I believe you mean the following Graph (shown in figure below), which has 8 Edges and 6 Vertices? 3 C 1 B G A D 2 4 S We were trying to find a path from start vertex S to goal vertex G using the Depth-First- Search algorithm. The result suggested visiting the vertices in order S , A , D , G . These four vertices, and the three edges in-between them are a Tree, with N (4) vertices, and N- 1 (3) edges. 2) When implementing trees in code, should loops be used to go through the tree? It seems as though there might be special cases where loops would not work… or not ? When you are creating a tree, you can use either recursion or iteration. The recursive solution is concise and abstracts a lot of the implementation detail. In the example shown below, 53 . procedure Insert ( 54 . Root : in out Nodeptr;
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
. Element : in ElementType) is 56 . New_Node : Nodeptr; 57 . begin 58 . if Root = null then 59 . New_Node:= new Node; 60 . New_Node.Element := Element; 61 . Root := New_Node; 62 . else 63 . if Root.Element < Element then 64 . Insert(Root.Right_Child, Element); 65 . else 66 . Insert(Root.Left_Child, Element); 67 . end if ; 68 . end if ; 69 . end Insert; Assume that only the root node exists, with the element = 9 as shown in figure below. Main program Insert(root, 6) . In line 63, the Root.Element > 6 so, line 66, Insert(Root.Right_Child, 6) is called. Recursive Insert(null, 6) Line 58, Root = null Line 59, create a new node Line 60, New_Node.Element := 6 Line 61, Root := New_Node (sets Root.Right_Child to New_Node) Line 68, end if (does nothing) Line 69, end recursive insert (returns control to Insert)
Background image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 01/28/2012 for the course AERO 16.01 taught by Professor Markdrela during the Fall '05 term at MIT.

Page1 / 6

m8 - Lecture C8: Trees part II Response to 'Muddiest Part...

This preview shows document pages 1 - 3. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online