{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

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

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

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;

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

View Full Document
. 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)
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

### Page1 / 6

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

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

View Full Document
Ask a homework question - tutors are online