Binary Trees: (And Big "O" Notation)
Binary Trees: (And Big "O" Notation)
CS-2303
System Programming Concepts
(Slides include materials from The C Programming Language, 2nd edition, by Kernighan and Ritchie and
from C: How to Program, 5th and 6th editions, by Deitel and Deitel)
• Linked List
• A data structure in which each element is
dynamically allocated and in which elements point
to each other to define a linear relationship
• Singly- or doubly-linked
• Stack, queue, circular list
• Tree
• A data structure in which each element is
dynamically allocated and in which each element
has more than one potential successor
• Defines a partial order
CS-2303, C-Term 2010 Binary Trees 2
Binary Tree
struct treeItem {
type payload; payload payload
treeItem *left; left right left right
treeItem *right;
}; payload
left right payload payload
left right left right
payload
left right
payload
left right payload payload
left right left right
payload
left right
payload
left right payload payload
left right left right
payload
left right
• Note:–
– 210 = 1024 103
– Therefore 106 220
– Therefore it takes approximately 20 two-way branches
to cover 106 items!
• Multiply-branched trees
• Like binary trees, but with more than two links per
node
• Multiply-branched trees
• Like binary trees, but with more than two links per
node
CS-2303, C-Term 2010 Binary Trees 16
Order of Traversing Binary Trees
• In-order
• Traverse left sub-tree (in-order)
• Visit node itself
• Traverse right sub-tree (in-order)
• Pre-order
• Visit node first
• Traverse left sub-tree
• Traverse right sub-tree
• Post-order
• Traverse left sub-tree
• Traverse right sub-tree
• Visit node last
CS-2303, C-Term 2010 Binary Trees 17
Question
x /
- sqrt
* * -
…
. . . .
New Topic