Data Structures Using C++ 2E

74
Data Structures Using C++ 2E Chapter 11 Binary Trees and B-Trees

description

Data Structures Using C++ 2E. Chapter 11 Binary Trees and B-Trees. Objectives. Learn about binary trees Explore various binary tree traversal algorithms Learn how to organize data in a binary search tree Discover how to insert and delete items in a binary search tree. Objectives (cont’d.). - PowerPoint PPT Presentation

Transcript of Data Structures Using C++ 2E

  • Data Structures Using C++ 2E Chapter 11Binary Trees and B-Trees

  • Data Structures Using C++ 2E*ObjectivesLearn about binary treesExplore various binary tree traversal algorithmsLearn how to organize data in a binary search treeDiscover how to insert and delete items in a binary search tree

    Data Structures Using C++ 2E

  • Data Structures Using C++ 2E*Objectives (contd.)Explore nonrecursive binary tree traversal algorithmsLearn about AVL (height-balanced) treesLearn about B-trees

    Data Structures Using C++ 2E

  • Data Structures Using C++ 2E*Binary TreesDefinition: a binary tree, T, is either empty or such thatT has a special node called the root nodeT has two sets of nodes, LT and RT, called the left subtree and right subtree of T, respectivelyLT and RT are binary treesCan be shown pictoriallyParent, left child, right childNode represented as a circleCircle labeled by the node

    Data Structures Using C++ 2E

  • Data Structures Using C++ 2E*Binary Trees (contd.)Root node drawn at the topLeft child of the root node (if any)Drawn below and to the left of the root nodeRight child of the root node (if any)Drawn below and to the right of the root nodeDirected edge (directed branch): arrow

    Data Structures Using C++ 2E

  • Data Structures Using C++ 2E*Binary Trees (contd.)

    Data Structures Using C++ 2E

  • Data Structures Using C++ 2E*Binary Trees (contd.)Every node in a binary treeHas at most two childrenstruct defining node of a binary treeFor each nodeThe data stored in infoA pointer to the left child stored in llinkA pointer to the right child stored in rlink

    Data Structures Using C++ 2E

  • Data Structures Using C++ 2E*Binary Trees (contd.)Pointer to root node is stored outside the binary treeIn pointer variable called the rootOf type binaryTreeNode

    Data Structures Using C++ 2E

  • *

  • Data Structures Using C++ 2E*Binary Trees (contd.)Level of a node Number of branches on the pathHeight of a binary treeNumber of nodes on the longest path from the root to a leafSee code on page 604Root: (A) level 0 is parent for B, CLeaves : G, E, HTree Height = 4D is on level 2

    Data Structures Using C++ 2E

  • *

  • Copy TreeShallow copy of the dataObtained when value of the pointer of the root node used to make a copy of a binary treeIdentical copy of a binary treeNeed to create as many nodes as there are in the binary tree to be copiedNodes must appear in the same order as in the original binary treeFunction copyTreeMakes a copy of a given binary treeSee code on pages 604-605 Data Structures Using C++ 2E*

    Data Structures Using C++ 2E

  • Data Structures Using C++ 2E*Binary Tree TraversalMust start with the root, and thenVisit the node first orVisit the subtrees firstThree different traversalsInorderPreorderPostorder

    Data Structures Using C++ 2E

  • Data Structures Using C++ 2E*Binary Tree Traversal (contd.)Inorder traversalTraverse the left subtreeVisit the nodeTraverse the right subtree

    Preorder traversalVisit the nodeTraverse the left subtreeTraverse the right subtree

    D G B E A C H FA B D G E C F H

    Data Structures Using C++ 2E

  • Data Structures Using C++ 2E*Binary Tree Traversal (contd.)Postorder traversalTraverse the left subtreeTraverse the right subtreeVisit the node

    Each traversal algorithm: recursiveListing of nodesInorder sequencePreorder sequencePostorder sequenceG D E B H F C A

    Data Structures Using C++ 2E

  • Data Structures Using C++ 2E*Binary Tree Traversal (contd.)

    Data Structures Using C++ 2E

  • Data Structures Using C++ 2E*Binary Tree Traversal (contd.)Functions to implement the preorder and postorder traversals

    Data Structures Using C++ 2E

  • Data Structures Using C++ 2E*Implementing Binary TreesOperations typically performed on a binary treeDetermine if binary tree is emptySearch binary tree for a particular itemInsert an item in the binary treeDelete an item from the binary treeFind the height of the binary treeFind the number of nodes in the binary treeFind the number of leaves in the binary treeTraverse the binary treeCopy the binary tree

    Data Structures Using C++ 2E

  • Data Structures Using C++ 2E*Implementing Binary Trees (contd.)class binaryTreeTypeSpecifies basic operations to implement a binary treeSee code on page 609Contains statement to overload the assignment operator, copy constructor, destructorContains several member functions that are private members of the classBinary tree empty if root is NULLSee isEmpty function on page 611

    Data Structures Using C++ 2E

  • Data Structures Using C++ 2E*Implementing Binary Trees (contd.)Default constructorInitializes binary tree to an empty stateSee code on page 612Other functions for binary treesSee code on pages 612-613Functions: copyTree, destroy, destroyTreeSee code on page 614Copy constructor, destructor, and overloaded assignment operatorSee code on page 615

    Data Structures Using C++ 2E

  • Data Structures Using C++ 2E*Binary Search TreesData in each nodeLarger than the data in its left childSmaller than the data in its right child

    Data Structures Using C++ 2E

  • 50 60 77 33 26 45 80 655060773326458065

  • How to Delete 8050607733264580482046

  • How to Delete 7750607733264580482046

  • How to Delete 5050607733264580482046Search for the previous number that precedes 50

  • How to Delete 5048607733264580482046Replace 50 with 48

  • How to Delete 5048607733264580482046Replace 50 with 48Now delete the original 48

  • Data Structures Using C++ 2E*Binary Search Trees (contd.)A binary search tree, T, is either empty or the following is true:T has a special node called the root nodeT has two sets of nodes, LT and RT , called the left subtree and right subtree of T, respectivelyThe key in the root node is larger than every key in the left subtree and smaller than every key in the right subtreeLT and RT are binary search trees

    Data Structures Using C++ 2E

  • Data Structures Using C++ 2E*Binary Search Trees (contd.)Operations performed on a binary search treeSearch the binary search tree for a particular itemInsert an item in the binary search treeDelete an item from the binary search treeFind the height of the binary search treeFind the number of nodes in the binary search treeFind the number of leaves in the binary search treeTraverse the binary search treeCopy the binary search tree

    Data Structures Using C++ 2E

  • Data Structures Using C++ 2E*Binary Search Trees (contd.)Every binary search tree is a binary treeHeight of a binary search treeDetermined the same way as the height of a binary treeOperations to find number of nodes, number of leaves, to do inorder, preorder, postorder traversals of a binary search treeSame as those for a binary treeCan inherit functions

    Data Structures Using C++ 2E

  • Data Structures Using C++ 2E*Binary Search Trees (contd.)class bSearchTreeTypeIllustrates basic operations to implement a binary search treeSee code on page 618Function searchFunction insertFunction delete

    Data Structures Using C++ 2E

  • Data Structures Using C++ 2E*AVL (Height-Balanced) TreesAVL tree (height-balanced tree) Resulting binary search is nearly balancedPerfectly balanced binary treeHeights of left and right subtrees of the root: equalLeft and right subtrees of the root are perfectly balanced binary trees

    Data Structures Using C++ 2E

  • Data Structures Using C++ 2E*AVL (Height-Balanced) Trees (contd.)An AVL tree (or height-balanced tree) is a binary search tree such thatThe heights of the left and right subtrees of the root differ by at most oneThe left and right subtrees of the root are AVL trees

    Data Structures Using C++ 2E

  • AVL treeHeight of a nodeThe height of a leaf is 1. The height of a null pointer is zero.The height of an internal node is the maximum height of its children plus 1

  • Data Structures Using C++ 2E*AVL (Height-Balanced) Trees (contd.)

    Data Structures Using C++ 2E

  • Data Structures Using C++ 2E*AVL (Height-Balanced) Trees (contd.)Definition of a node in the AVL tree

    Data Structures Using C++ 2E

  • Data Structures Using C++ 2E*AVL (Height-Balanced) Trees (contd.)AVL binary search tree search algorithmSame as for a binary search treeOther operations on AVL treesImplemented exactly the same way as binary treesItem insertion and deletion operations on AVL treesSomewhat different from binary search trees operations

    Data Structures Using C++ 2E

  • InsertionFirst search the tree and find the place where the new item is to be insertedCan search using algorithm similar to search algorithm designed for binary search treesIf the item is already in treeSearch ends at a nonempty subtreeDuplicates are not allowedIf item is not in AVL treeSearch ends at an empty subtree; insert the item thereAfter inserting new item in the treeResulting tree might not be an AVL tree Data Structures Using C++ 2E*

    Data Structures Using C++ 2E

  • Data Structures Using C++ 2E*Insertion (contd.)FIGURE 11-15 AVL tree before and after inserting 75

    Data Structures Using C++ 2E

  • Data Structures Using C++ 2E*Insertion (contd.)

    Data Structures Using C++ 2E

  • AVL Tree RotationsRotating tree: reconstruction procedureLeft rotation and right rotationSuppose that the rotation occurs at node xLeft rotation: certain nodes from the right subtree of x move to its left subtree; the root of the right subtree of x becomes the new root of the reconstructed subtreeRight rotation at x: certain nodes from the left subtree of x move to its right subtree; the root of the left subtree of x becomes the new root of the reconstructed subtree Data Structures Using C++ 2E*

    Data Structures Using C++ 2E

  • Single rotationSingle rotation takes O(1) time.Insertion takes O(log N) time.The new key is inserted in the subtree C. The AVL-property is violated at x.

  • 5Insert 0.8 AVL Tree80.8xyABCAfter rotation

  • Double rotationThe new key is inserted in the subtree B1 or B2. The AVL-property is violated at x.x-y-z forms a zig-zag shapealso called left-right rotate

  • Double rotationThe new key is inserted in the subtree B1 or B2. The AVL-property is violated at x.also called right-left rotate

  • Data Structures Using C++ 2E*

    Data Structures Using C++ 2E

  • Data Structures Using C++ 2E*

    Data Structures Using C++ 2E

  • Data Structures Using C++ 2E*AVL Tree Rotations (contd.)

    Data Structures Using C++ 2E

  • 5Insert 3.5 AVL Tree81After Rotation

  • An Extended ExampleInsert 3,2,1,4,5,6,7, 16,15,14

  • Insert 3,2,1,4,5,6,7, 16,15,14

  • Insert 3,2,1,4,5,6,7, 16,15,14

  • Insert 3,2,1,4,5,6,7, 16,15,14

  • Data Structures Using C++ 2E*

    Data Structures Using C++ 2E

  • Data Structures Using C++ 2E*

    Data Structures Using C++ 2E

  • Data Structures Using C++ 2E*

    Data Structures Using C++ 2E

  • Data Structures Using C++ 2E*AVL Tree Rotations (contd.)Steps describing the function insertIntoAVLCreate node and copy item to be inserted into the newly created nodeSearch the tree and find the place for the new node in the treeInsert new node in the treeBacktrack the path, which was constructed to find the place for the new node in the tree, to the root nodeIf necessary, adjust balance factors of the nodes, or reconstruct the tree at a node on the path

    Data Structures Using C++ 2E

  • Data Structures Using C++ 2E*40302040 30 20 60 50 80 15 28 25

    Data Structures Using C++ 2E

  • Data Structures Using C++ 2E*30204040 30 20 60 50 80 15 28 25 6050

  • Data Structures Using C++ 2E*30204040 30 20 60 50 80 15 28 25 5060

  • Data Structures Using C++ 2E*30205040 30 20 60 50 80 15 28 25 604080

  • Data Structures Using C++ 2E*50306040 30 20 60 50 80 15 28 25 802040152825

  • Data Structures Using C++ 2E*50306040 30 20 60 50 80 15 28 25 802840202515

  • Data Structures Using C++ 2E*50286040 30 20 60 50 80 15 28 25 802030154025

  • Data Structures Using C++ 2E*

  • *void insertIntoAVL(AVLNode* &root, AVLNode *newNode, bool& isTaller) { if(root == NULL) { root = newNode;isTaller = true; } else if(root->info == newNode->info) cerrllink, newNode, isTaller); if(isTaller) //after insertion, the subtree grew in height switch(root->bfactor) { case -1: balanceFromLeft(root);isTaller = false;break; case 0: root->bfactor = -1;isTaller = true;break; case 1: root->bfactor = 0;isTaller = false; }//end switch }//end if else { insertIntoAVL(root->rlink, newNode, isTaller); if(isTaller) //after insertion, the subtree grew in height switch(root->bfactor) { case -1: root->bfactor = 0;isTaller = false;break; case 0: root->bfactor = 1;isTaller = true;break; case 1: balanceFromRight(root);isTaller = false; }//end switch }//end else}//end insertIntoAVL

  • Data Structures Using C++ 2E*AVL Tree Rotations (contd.)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

    Data Structures Using C++ 2E

  • Data Structures Using C++ 2E*Deletion from AVL TreesFour casesCase 1: The node to be deleted is a leafCase 2: The node to be deleted has no right child, that is, its right subtree is emptyCase 3: The node to be deleted has no left child, that is, its left subtree is emptyCase 4: The node to be deleted has a left child and a right childCases 13Easier to handle than Case 4

    Data Structures Using C++ 2E

  • Data Structures Using C++ 2E*502860Delete 40 802030154025Still AVL

  • Data Structures Using C++ 2E*502860Delete 30 8020301525

  • Data Structures Using C++ 2E*502060Delete 30 80152528

  • Data Structures Using C++ 2E*502860Another example Delete 30 80203025

  • Data Structures Using C++ 2E*502860Another example Delete 30 802520

  • Data Structures Using C++ 2E*502560Another example Delete 30 802028

    ****