1 / 42

Search Tree

Search Tree. Chapter 7 Trees_Part3. Search Trees. Two standard search trees : Binary Search Trees (non-balanced ) All items in left sub-tree are less than root All items in right sub-tree are greater than or equal to the root Each sub-tree is a binary search tree AVL trees (balanced ).

linda-reese
Download Presentation

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. Search Tree Chapter 7 Trees_Part3

  2. Search Trees • Two standard search trees: • Binary Search Trees (non-balanced) • All items in left sub-tree are less than root • All items in right sub-tree are greater than or equal to the root • Each sub-tree is a binary search tree • AVL trees (balanced)

  3. Binary Search Trees • All items in left sub-tree are less than root • All items in right sub-tree are greater than or equal to the root • Each sub-tree is a binary search tree

  4. Searching a Binary Tree • For every node, compare the key to be located with the value stored in the node currently pointed at. • If key is less than the value, go to the left subtree and try again. • If it is greater than that value, try the right subtree. • If it is the same, the search stops. • The search is aborted if there is no way to go – the key is not in the tree.

  5. Finding a Node • Finding a node with a specific key is the simplest of the major tree operations. Remember that the nodes in a binary search tree correspond to objects containing information, one of them can be considered as a key.

  6. Example • (a) A binary search tree T representing a dictionary D with integer keys; (b) nodes of T visited when executing operations find(76) (successful) and find(25) (unsuccessful) on D. For simplicity, we show keys but entry values.

  7. Inserting a Node • Find the place to insert it. This is much the same process as trying to find a node which turns out not to exist. • Follow the path from the root to the appropriate node, which will be the parent of the new node. • After this parent is found, the new node is connected as its left or right child, depending on whether the new node's key is less or greater than that of the parent.

  8. Example • Insertion of an entry with key 78 into the search tree . Finding the position to insert is shown in (a), and the resulting tree is shown in (b).

  9. Insert

  10. Insert

  11. Deleting a Node • Deleting a node is the most complicated common operation required for binary search trees. However, deletion is important in many tree applications. • There are three cases to consider:- 1- The node to be deleted is a leaf node (has no children). 2- The node to be deleted has one child. 3- The node to be deleted has two children.

  12. Case1 : The node to be deleted has no children To delete a leaf node, change the appropriate child field in the node's parent to point to null, instead of to the node.

  13. Case 2 : The node to be deleted has one child The node has only two connections: to its parent and to its only child. You want to "snip" the node out of this sequence by connecting its parent directly to its child.

  14. Case 3 : The node to be deleted has two children the node has 2 children • replace the key of that node with the minimum element at the right subtree • delete the minimum element • Has either no child or only right child because if it has a left child, that left child would be smaller and would have been chosen.

  15. 2- AVL Trees • AVL trees are height-balanced binary search trees • Height-Balance Property: For every internal node v of T, the heights of the children of v differ by at most 1.

  16. 2- AVL Trees • Height-balanced binary search tree where the heights of sub-trees differ by no more than 1: • | HL – HR | <= 1 • Search effort for an AVL tree is O(log2n) • Each node has a balance factor • Balance factors may be: • Left High (LH) = +1 (left sub-tree higher than right sub-tree) • Even High (EH) = 0 (left and right sub-trees same height) • Right High (RH) = -1 (right sub-tree higher than left sub-tree)

  17. Figure 8-12

  18. Figure 8-13 An AVL tree

  19. Balancing Trees • Insertions and deletions potentially cause a tree to be imbalanced • When a tree is detected as unbalanced, nodes are balanced by rotating nodes to the left or right • Four imbalance cases: • Left of left: the sub-tree of a left high tree has become left high • Right of right: the sub-tree of a right high tree has become right high • Right of left: the sub-tree of a left high tree has become right high • Left of right: the sub-tree of a right high tree has become left high • Each imbalance case has simple and complex rotation cases

  20. Case 1: Left of Left • The sub-tree of a left high tree has become left high • Simple right rotation: Rotate the out of balance node (the root) to the right • Complex right rotation: Rotate root to the right, so the old root is the right sub-tree of the new root; the new root's right sub-tree is connected to the old root's left sub-tree

  21. Case 2: Right of Right • Mirror of Case 1: The sub-tree of a right high tree has become right high • Simple left rotation: Rotate the out of balance node (the root) to the left • Complex left rotation: Rotate root to the left, so the old root is the left sub-tree of the new root; the new root's left sub-tree is connected to the old root's right sub-tree

  22. Case 3: Right of Left • The sub-tree of a left high tree has become right high • Simple double rotation right: Rotate left sub-tree to the left; rotate root to the right, so the old root's left node is the new root • Complex double rotation right: Rotate the right-high left sub-tree to the left; rotate the left-high left sub-tree to the right

  23. Case 4: Left of Right • Mirror of Case 3: The sub-tree of a right high tree has become left high • Simple double rotation right: Rotate right sub-tree to the right; rotate root to the left, so the old root's right node is the new root • Complex double rotation right: Rotate the left-high right sub-tree to the right; rotate the right-high right sub-tree to the left

  24. AVL Node Structure Node key <keyType> data <dataType> Left <pointer to Node> right <pointer to Node> bal <LH, EH, RH> // Balance factor End Node

  25. Multiway Search Tree • A tree was defined as either an empty structure or a structure whose children are disjoint trees. • This means that each node in some kind of trees can have more than two children. • This tree is called a multiway tree of order m, or m-way tree

  26. Multiway Search Tree con.. • A multiway search tree of order m is a multiway tree in which : • Each node has m children and m-1 keys. • The keys in each node are in ascending order. • The keys in the first i children are smaller than the ith key. • The keys in the last m-i children are larger than the ith key.

  27. B-Tree • B- Trees operates closely with secondary storage. • Property: the size of a node can be made as large as a block. • A B- tree of order m is a multi way search tree with the following properties: • The root has at least two subtrees unless it is a leaf. • Each nonroot and each nonleaf node holds k-1 keys and k pointers to subtrees where [m/2] ≤ k ≤ m. • Each leaf node holds k-1 keys where [m/2] ≤ k ≤ m. • All leaves are usually on the same level.

  28. B-Tree

  29. End Of Chapter References: Text book, chapter 8: Binary Trees

More Related