6_trees - Binary Search Trees ! Understand ! Understand...

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

View Full Document Right Arrow Icon
Binary Search Trees
Background image of page 1

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

View Full DocumentRight Arrow Icon
! Understand tree terminology ! Understand and implement tree traversals ! Define the binary search tree property ! Implement binary search trees ! Implement the TreeSort algorithm October 2004 John Edgar 2
Background image of page 2
Background image of page 3

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

View Full DocumentRight Arrow Icon
! A set of nodes (or vertices) with a single starting point ! called the root ! Each node is connected by an edge to another node ! A tree is a connected graph ! There is a path to every node in the tree ! A tree has one fewer edge than the number of nodes October 2004 John Edgar 4
Background image of page 4
October 2004 John Edgar 5 yes! NO! All the nodes are not connected NO! There is an extra edge (5 nodes and 5 edges) yes! (but not a binary tree) yes! (it’s actually the same graph as the blue one)
Background image of page 5

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

View Full DocumentRight Arrow Icon
A B C D G E F ! Node v is said to be a child of u , and u the parent of v if ! There is an edge between the nodes u and v , and ! u is above v in the tree, ! This relationship can be generalized ! E and F are descendants of A ! D and A are ancestors of G ! B, C and D are siblings ! F and G are? October 2004 John Edgar 6 root edge parent of B, C, D
Background image of page 6
! A leaf is a node with no children ! A path is a sequence of nodes v 1 v n ! where v i is a parent of v i +1 (1 i n-1 ) ! A subtree is any node in the tree along with all of its descendants ! A binary tree is a tree with at most two children per node ! The children are referred to as left and right ! We can also refer to left and right subtrees October 2004 John Edgar 7
Background image of page 7

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

View Full DocumentRight Arrow Icon
October 2004 John Edgar 8 C A B C D G E F leaves : C,E,F,G path from A to D to G subtree rooted at B
Background image of page 8
October 2004 John Edgar 9 A B C G D E left subtree of A H I J F right subtree of C right child of A
Background image of page 9

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

View Full DocumentRight Arrow Icon
! The height of a node v is the length of the longest path from v to a leaf ! The height of the tree is the height of the root ! The depth of a node v is the length of the path from v to the root ! This is also referred to as the level of a node ! Note that there is a slightly different formulation of the height of a tree ! Where the height of a tree is said to be the number of different levels of nodes in the tree (including the root) October 2004 John Edgar 10
Background image of page 10
October 2004 John Edgar 11 A B C G D E H I J F height of node B is ? height of the tree is ? depth of node E is ? level 1 level 2 level 3 2 3 2
Background image of page 11

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

View Full DocumentRight Arrow Icon
Background image of page 12
October 2004 John Edgar 13 yes! yes! However, these trees are not “beautiful” (for some applications)
Background image of page 13

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

View Full DocumentRight Arrow Icon
! A binary tree is perfect , if ! No node has only one child ! And all the leaves have the same depth ! A perfect binary tree of height h has how many nodes? ! 2 h +1 – 1 nodes, of which 2 h are leaves October 2004 John Edgar 14 A B C G D E F
Background image of page 14
October 2004 John Edgar 15 12 22 31 23 24 33 34 35 36 38 01 11 21 32 37 " Each level doubles the number of nodes " Level 1 has 2 nodes (2 1 ) " Level 2 has 4 nodes (2 2 ) or 2 times the number in Level 1 " Therefore a tree with h levels has 2 h+1 - 1nodes " The root level has 1 node the bottom level has 2 h nodes, that is, just over ½ the nodes are leaves
Background image of page 15

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

View Full DocumentRight Arrow Icon
! A binary tree is
Background image of page 16
Image of page 17
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 04/17/2010 for the course CMPT 11151 taught by Professor Gregorymori during the Spring '10 term at Simon Fraser.

Page1 / 61

6_trees - Binary Search Trees ! Understand ! Understand...

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

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