1 / 100

Binary Search Tree

Arbitrary binary tree. Binary search tree. Binary Search Tree. A Binary Search Tree is a binary tree that is either an empty or in which every node contain a key and satisfies the following conditions :

clouise
Télécharger la présentation

Binary Search Tree

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Arbitrary binary tree Binary search tree Binary Search Tree • A Binary Search Treeis a binary tree that is either an empty or in which every node contain a key and satisfies the following conditions : • The key in the left child of a node (if it exists) is less than the key in its parent node . • The key in the right child of a node (if it exists) is greater than or equal to the key in its parent node . • The left and right sub trees of the root are again binary search tree.

  2. Binary Search Tree (ADT) ADT Implementation of BST: struct BSTNode { int data; BSTNode *left; BSTNode *right; } struct BSTnode*left=NULL ,*right=NULL; BSTs can be implemented using either arrays or linked lists. However, linked list structures are more common and more efficient. The implementation uses nodes with two pointers namely, left and right.

  3. Binary Search Tree (ADT) A BST implementation

  4. Operations- Traversal, Searching, Insertion and Deletion • There are four basic BST operations: traversal, search, insert, and delete. • Traversals: Traverse the items in a binary search tree in preorder, inorder, or postorder • Searches: Retrieve the item with a given search key from a binary search tree • Insertion: Insert a new item into a binary search tree • Deletion Delete the item with a given search key from a binary search tree

  5. Traversals Traversals:Traverse the items in a binary search tree in preorder, inorder, or postorder • There are 3 types of traversals: • 1. Pre-Order • 2. In-Order • 3. Post-Order

  6. Inorder Traversal To traverse a non-empty binary tree using  in order, perform the following operations recursively at each node, starting with the root node: • Traverse the left sub tree. • Visit the root. • Traverse the right sub tree. Visit the nodes in the left subtree, then visit the root of the tree, then visit the nodes in the right subtree • Inorder(tree) • If tree is not NULL • Inorder(Left(tree)) • Visit Info(tree) • Inorder(Right(tree)) • ("visit" means that the algorithm does something with the values in the node, e.g., printthevalue)

  7. ‘J’ Inorder Traversal : A E H J M T Y Visit second tree ‘T’ ‘E’ ‘A’ ‘H’ ‘M’ ‘Y’ Visit left subtree first Visit right subtree last

  8. Preorder Traversal • To traverse a non-empty binary tree in preorder, perform the following operations recursively at each node, starting with the root node: • Visit the root. • Traverse the left sub tree. • Traverse the right sub tree. • Visit the root of the tree first, then visit the nodes in the left subtree, then visit the nodes in the right subtree Preorder(tree) If tree is not NULL Visit Info(tree) Preorder(Left(tree)) Preorder(Right(tree))

  9. ‘J’ Preorder Traversal: J E A H T M Y Visit first tree ‘T’ ‘E’ ‘A’ ‘H’ ‘M’ ‘Y’ Visit left subtree second Visit right subtree last

  10. Postorder Traversal • To traverse a non-empty binary tree in post order, perform the following operations recursively at each node, starting with the root node: • Traverse the left sub tree. • Traverse the right sub tree. • Visit the root.  • Visit the nodes in the left subtree first, then visit the nodes in the right subtree, then visit the root of the tree Postorder(tree) If tree is not NULL Postorder(Left(tree)) Postorder(Right(tree)) Visit Info(tree)

  11. ‘J’ Visit last tree ‘T’ ‘E’ ‘A’ ‘H’ ‘M’ ‘Y’ Visit left subtree first Visit right subtree second Postorder Traversal: A H E M Y T J

  12. SEARCH Searches: Retrieve the item with a given search key from a binary search tree. Three BST search algorithms: Find the smallest node Find the largest node Find a requested node

  13. BST INSERTION • To insert data, all we need to do is follow the branches to an empty sub tree and then insert the new node. • In other words, all inserts take place at a leaf or at a leaf like node – a node that has only one null sub tree.

  14. Algorithm addBST (root, newNode) Insert node containing new data into BST using recursion . Pre root is address of current node in a BST newNode is address of node containing data Post neewNode inserted into the tree Return address of potential new tree root if (empty tree) set root to newNode return newNode end if Locate null subtree for insertion if(newNode<root) return addBST(left subtree,newNode) else return addBST(right subtree, newNode) end if end addBST

  15. Deletion The following possible cases are expected during a node deletion: • The node to be deleted has no children. In this case, all we need to do is delete the node. • The node to be deleted has only a right sub tree. We delete the node and attach the right sub tree to the deleted node’s parent. • The node to be deleted has only a left sub tree. We delete the node and attach the left sub tree to the deleted node’s parent. • The node to be deleted has two sub trees. It is possible to delete a node from the middle of a tree, but the result tends to create very unbalanced trees.

  16. Node to be removed has no children.

  17. Node to be removed has one child.

  18. Node to be removed has two children

  19. Deletion from the middle of a tree: • Rather than simply delete the node, we try to maintain the existing structure as much as possible by finding data to take the place of the deleted data. This can be done in one of the two ways. • We can find the largest node in the deleted node’s left subtree and move its data to replace the deleted node’s data. • We can find the smallest node on the deleted node’s right subtree and move its data to replace the deleted node’s data. • Either of these moves preserves the integrity of the binary search tree.

  20. Delete the Node from BST

  21. Program 1:BST Implementation

  22. AVL TREES • Definition: balanced tree is a binary search tree, which is either (a) empty or (b) its left and right sub-trees are “more or less” of equal height • Definition: AVL (Adelson-Velski, Landis) tree is a binary search tree T, which is either (a) empty or (b) difference between heights of its left and right sub-trees is not greater than one and both sub-trees are also AVL trees • |hleft-hright| <=1 • A tree is said to be balanced if for each node, the number of nodes in the left subtree and the number of nodes in the right subtree differ by at most one.

  23. Height of an AVL TREE • A tree is said to be height-balanced or AVL if for each node, the height of the left subtree and height of the right subtree differ by at most one. • The height of the left subtree minus the height of the right subtree of a node is called the balance of the node. For an AVL tree, the balances of the nodes are always -1, 0 or 1. • Given an AVL tree, if insertions or deletions are performed, the AVL tree may not remain height balanced.

  24. -1 10 1 1 7 40 0 0 1 -1 3 8 30 45 0 0 0 -1 0 1 5 20 35 60 0 25 AVL Tree with Balance Factors

  25. AVL TREES SEARCH • To search an AVL search tree, the code for searching a binary search tree can be used without any change

  26. Rebalancing Suppose the node to be rebalanced is X. There are 4 cases that we might have to fix (two are the mirror images of the other two): • An insertion in the left subtree of the left child of X, • An insertion in the right subtree of the left child of X, • An insertion in the left subtree of the right child of X, or • An insertion in the right subtree of the right child of X. Balance is restored by tree rotations.

  27. Single Rotation • A single rotation switches the roles of the parent and child while maintaining the search order. • Single rotation handles the outside cases (i.e. 1 and 4). • We rotate between a node and its child. • Child becomes parent. Parent becomes right child in case 1, left child in case 4. • The result is a binary search tree that satisfies the AVL property.

  28. Single rotation to fix case 1: Rotate right

  29. Symmetric single rotation to fix case 4 : Rotate left

  30. Single rotation fixes an AVL tree after insertion of 1.

  31. Double Rotation • Single rotation does not fix the inside cases (2 and 3). • These cases require a double rotation, involving three nodes and four subtrees.

  32. Single rotation does not fix case 2.

  33. Left–right double rotation to fix case 2 Lift this up: first rotate left between (k1,k2), then rotate right betwen (k3,k2)

  34. Left-Right Double Rotation • A left-right double rotation is equivalent to a sequence of two single rotations: • 1st rotation on the original tree: a left rotation between X’s left-child and grandchild • 2nd rotation on the new tree: a right rotation between X and its new left child.

  35. Double rotation fixes AVL tree after the insertion of 5.

  36. Right–Left double rotation to fix case 3.

  37. AVL TREE INSERTION • 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

  38. AVL tree before and after inserting 90 AVL tree before and after inserting 75

  39. AVL tree before and after inserting 95 AVL tree before and after inserting 88

  40. Item insertion into an initially empty AVL tree

  41. 2 2 3 2 3 4 4 1 1 1 2 2 3 3 Fig 1 5 3 3 Fig 4 Fig 2 Fig 3 1 Fig 5 Fig 6 Example Insert 3,2,1,4,5,6,7, 16,15,14

More Related