Chapt 5 Tree II
Chapt 5 Tree II
Chapter 11
Binary Trees and B-Trees
Binary Trees (cont’d.)
• Tree is a hierarchical data structure. Main uses of
trees include maintaining hierarchical data,
providing moderate access and insert/delete
operations. Binary trees are special cases of tree
where every node has at most two children.
2
3
4
types of binary
5
6
• Complete Binary Tree: A Binary Tree is complete
Binary Tree if all levels are completely filled except
possibly the last level and the last level has all keys
as left as possible
7
8
• Perfect Binary Tree A Binary tree is Perfect Binary
Tree in which all internal nodes have two children
and all leaves are at the same level.
9
• A degenerate (or pathological) tree A Tree
where every internal node has one child. Such
trees are performance-wise same as linked list.
10
Binary Trees (cont’d.)
• Level of a node
– Number of branches on the path
• Height of a binary tree
– Number of nodes on the longest path from the root to a leaf
– See code on page 604
Root: (A) level 0 is parent for B, C
Leaves : G, E, H
Tree Height = 4
D is on level 2
13
Depth First Traversals:
14
Binary Tree Traversal (cont’d.)
• Inorder traversal
– Traverse the left subtree
– Visit the node
– Traverse the right subtree
D G B E A C H F
• Preorder traversal
– Visit the root FIGURE 11-1 Binary tree
G D E B H F C A
FIGURE 11-1 Binary tree
• Each traversal algorithm: recursive
• Listing of nodes
– Inorder sequence
– Preorder sequence
– Postorder sequence
Data Structures Using C++ 2E 16
17
18
19
20
21
22
23
24