Binary Tree - B Tree
Binary Tree - B Tree
Binary Tree - B Tree
Chapter 11
Binary Trees and B-Trees
Objectives
• 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
• Inorder traversal
– Traverse the left subtree
– Visit the node
– Traverse the right subtree
• Preorder traversal
– Visit the node
– Traverse the left subtree
– Traverse the right subtree
• Postorder traversal
– Traverse the left subtree
– Traverse the right subtree
– Visit the node
• Each traversal algorithm: recursive
• Listing of nodes
– Inorder sequence
– Preorder sequence
– Postorder sequence
• class binaryTreeType
– Specifies basic operations to implement a binary tree
– See code on page 609
• Contains statement to overload the assignment
operator, copy constructor, destructor
• Contains several member functions that are private
members of the class
• Binary tree empty if root is NULL
– See isEmpty function on page 611
• Default constructor
– Initializes binary tree to an empty state
– See code on page 612
• Other functions for binary trees
– See code on pages 612-613
• Functions: copyTree, destroy, destroyTree
– See code on page 614
• Copy constructor, destructor, and overloaded
assignment operator
– See code on page 615
Data Structures Using C++ 2E 18
Binary Search Trees
FIGURE 11-6 Arbitrary binary tree FIGURE 11-7 Binary search tree
• class bSearchTreeType
– Illustrates basic operations to implement a binary
search tree
– See code on page 618
• Function search
• Function insert
• Function delete
• First search the tree and find the place where the
new item is to be inserted
– Can search using algorithm similar to search
algorithm designed for binary search trees
– If the item is already in tree
• Search ends at a nonempty subtree
• Duplicates are not allowed
– If item is not in AVL tree
• Search ends at an empty subtree; insert the item there
• After inserting new item in the tree
– Resulting tree might not be an AVL tree
Data Structures Using C++ 2E 39
Insertion (cont’d.)
• Function insert
– Creates a node, stores the info in the node, and calls
the function insertIntoAVL to insert the new node
in the AVL tree
• Four cases
– Case 1: The node to be deleted is a leaf
– Case 2: The node to be deleted has no right child,
that is, its right subtree is empty
– Case 3: The node to be deleted has no left child, that
is, its left subtree is empty
– Case 4: The node to be deleted has a left child and a
right child
• Cases 1–3
– Easier to handle than Case 4
• Basic operations
– Search the tree, insert an item, delete an item,
traverse the tree
• Function insertBTree
– Recursively inserts an item into a B-tree
• Function insert uses function insertBTree
• Function insertNode
– Inserts item in the node
• Function splitNode
– Splits node into two nodes
– Inserts new item in the relevant node
– Returns median key and pointer to second half of the
node
Data Structures Using C++ 2E 63
FIGURE 11-26 Item insertion into a B-tree of order 5