Download - Trees Types and Operations
![Page 1: Trees Types and Operations](https://reader036.vdocuments.site/reader036/viewer/2022062323/568162a1550346895dd318d7/html5/thumbnails/1.jpg)
TreesTypes and Operations
![Page 2: Trees Types and Operations](https://reader036.vdocuments.site/reader036/viewer/2022062323/568162a1550346895dd318d7/html5/thumbnails/2.jpg)
1. General Trees2. Binary Search Trees3. AVL Trees4. Heap Trees
Agenda
![Page 3: Trees Types and Operations](https://reader036.vdocuments.site/reader036/viewer/2022062323/568162a1550346895dd318d7/html5/thumbnails/3.jpg)
Insertion◦FIFO◦LIFO◦Key-sequenced Insertion
DeletionChanging a General Tree into a Binary Tree
1. General Trees
![Page 4: Trees Types and Operations](https://reader036.vdocuments.site/reader036/viewer/2022062323/568162a1550346895dd318d7/html5/thumbnails/4.jpg)
Given the parent Node a new node may be inserted as
FIFO
Insertion
![Page 5: Trees Types and Operations](https://reader036.vdocuments.site/reader036/viewer/2022062323/568162a1550346895dd318d7/html5/thumbnails/5.jpg)
Data Structures: A Pseudocode Approach with C 5
First in-first out (FIFO) insertion
![Page 6: Trees Types and Operations](https://reader036.vdocuments.site/reader036/viewer/2022062323/568162a1550346895dd318d7/html5/thumbnails/6.jpg)
Given the parent Node a new node may be inserted as
FIFO LIFO
Insertion
![Page 7: Trees Types and Operations](https://reader036.vdocuments.site/reader036/viewer/2022062323/568162a1550346895dd318d7/html5/thumbnails/7.jpg)
Data Structures: A Pseudocode Approach with C 7
Last in-first out (LIFO) insertion
![Page 8: Trees Types and Operations](https://reader036.vdocuments.site/reader036/viewer/2022062323/568162a1550346895dd318d7/html5/thumbnails/8.jpg)
Given the parent Node a new node may be inserted as
FIFO LIFO Key-sequenced Insertion
Insertion
![Page 9: Trees Types and Operations](https://reader036.vdocuments.site/reader036/viewer/2022062323/568162a1550346895dd318d7/html5/thumbnails/9.jpg)
Data Structures: A Pseudocode Approach with C 9
Key-sequenced insertion
![Page 10: Trees Types and Operations](https://reader036.vdocuments.site/reader036/viewer/2022062323/568162a1550346895dd318d7/html5/thumbnails/10.jpg)
Insertion◦FIFO◦LIFO◦Key-sequenced Insertion
DeletionChanging a General Tree into a Binary Tree
1. General Trees
![Page 11: Trees Types and Operations](https://reader036.vdocuments.site/reader036/viewer/2022062323/568162a1550346895dd318d7/html5/thumbnails/11.jpg)
For general trees nodes to be deleted are restricted to be “leaves”
Otherwise a node maybe “purged”, i.e. a node is deleted along with all its children
Deletion
![Page 12: Trees Types and Operations](https://reader036.vdocuments.site/reader036/viewer/2022062323/568162a1550346895dd318d7/html5/thumbnails/12.jpg)
Insertion◦FIFO◦LIFO◦Key-sequenced Insertion
DeletionChanging a General Tree into a Binary Tree
1. General Trees
![Page 13: Trees Types and Operations](https://reader036.vdocuments.site/reader036/viewer/2022062323/568162a1550346895dd318d7/html5/thumbnails/13.jpg)
Changing the meaning of the two pointers:
Leftchild …..first childRightchild ….. Next siblings
Changing into Binary Trees
![Page 14: Trees Types and Operations](https://reader036.vdocuments.site/reader036/viewer/2022062323/568162a1550346895dd318d7/html5/thumbnails/14.jpg)
Data Structures: A Pseudocode Approach with C 14
Changing a General Tree to a Binary Tree
![Page 15: Trees Types and Operations](https://reader036.vdocuments.site/reader036/viewer/2022062323/568162a1550346895dd318d7/html5/thumbnails/15.jpg)
Data Structures: A Pseudocode Approach with C 15
Changing a General Tree to a Binary Tree
![Page 16: Trees Types and Operations](https://reader036.vdocuments.site/reader036/viewer/2022062323/568162a1550346895dd318d7/html5/thumbnails/16.jpg)
Data Structures: A Pseudocode Approach with C 16
Changing a General Tree to a Binary Tree
![Page 17: Trees Types and Operations](https://reader036.vdocuments.site/reader036/viewer/2022062323/568162a1550346895dd318d7/html5/thumbnails/17.jpg)
1. General Trees2. Binary Search Trees3. AVL Trees4. Heap Trees
Agenda
![Page 18: Trees Types and Operations](https://reader036.vdocuments.site/reader036/viewer/2022062323/568162a1550346895dd318d7/html5/thumbnails/18.jpg)
Basic ConceptsBST OperationsThreaded Trees
2. Binary Search Tree
![Page 19: Trees Types and Operations](https://reader036.vdocuments.site/reader036/viewer/2022062323/568162a1550346895dd318d7/html5/thumbnails/19.jpg)
All items in left subtree < rootAll items in right subtree > root
Basic Concepts
![Page 20: Trees Types and Operations](https://reader036.vdocuments.site/reader036/viewer/2022062323/568162a1550346895dd318d7/html5/thumbnails/20.jpg)
Binary Search Trees
A binary search tree Not a binary search tree
![Page 21: Trees Types and Operations](https://reader036.vdocuments.site/reader036/viewer/2022062323/568162a1550346895dd318d7/html5/thumbnails/21.jpg)
Binary Search TreeTwo binary search trees representing the same set:
![Page 22: Trees Types and Operations](https://reader036.vdocuments.site/reader036/viewer/2022062323/568162a1550346895dd318d7/html5/thumbnails/22.jpg)
Basic ConceptsBST OperationsThreaded Trees
2. Binary Search Tree
![Page 23: Trees Types and Operations](https://reader036.vdocuments.site/reader036/viewer/2022062323/568162a1550346895dd318d7/html5/thumbnails/23.jpg)
TraversalSearch
◦Smallest ……….. ?◦Largest …………?◦Specific element
InsertionDeletion
BST Operations
![Page 24: Trees Types and Operations](https://reader036.vdocuments.site/reader036/viewer/2022062323/568162a1550346895dd318d7/html5/thumbnails/24.jpg)
Inorder traversal of BST Print out all the keys in sorted order
Inorder: 2, 3, 4, 6, 7, 9, 13, 15, 17, 18, 20
![Page 25: Trees Types and Operations](https://reader036.vdocuments.site/reader036/viewer/2022062323/568162a1550346895dd318d7/html5/thumbnails/25.jpg)
TraversalSearch
◦Smallest ……….. ?◦Largest …………?◦Specific element
InsertionDeletion
BST Operations
![Page 26: Trees Types and Operations](https://reader036.vdocuments.site/reader036/viewer/2022062323/568162a1550346895dd318d7/html5/thumbnails/26.jpg)
findMin/ findMax Return the node containing the smallest element in the tree
Start at the root and goes left/right as long as there is a left/right child. The stopping point is the smallest/largest element
Time complexity = O(height of the tree)
![Page 27: Trees Types and Operations](https://reader036.vdocuments.site/reader036/viewer/2022062323/568162a1550346895dd318d7/html5/thumbnails/27.jpg)
Searching BST (specific elem) If we are searching for 15, then we are
done. If we are searching for a key < 15, then we
should search in the left subtree. If we are searching for
a key > 15, then we should search in the right subtree.
![Page 28: Trees Types and Operations](https://reader036.vdocuments.site/reader036/viewer/2022062323/568162a1550346895dd318d7/html5/thumbnails/28.jpg)
Searching BST
![Page 29: Trees Types and Operations](https://reader036.vdocuments.site/reader036/viewer/2022062323/568162a1550346895dd318d7/html5/thumbnails/29.jpg)
TraversalSearch
◦Smallest ……….. ?◦Largest …………?◦Specific element
InsertionDeletion
BST Operations
![Page 30: Trees Types and Operations](https://reader036.vdocuments.site/reader036/viewer/2022062323/568162a1550346895dd318d7/html5/thumbnails/30.jpg)
insert Proceed down the tree as you would with a find If X is found, do nothing (or update something) Otherwise, insert X at the last spot on the path traversed
Time complexity = O(height of the tree)
![Page 31: Trees Types and Operations](https://reader036.vdocuments.site/reader036/viewer/2022062323/568162a1550346895dd318d7/html5/thumbnails/31.jpg)
TraversalSearch
◦Smallest ……….. ?◦Largest …………?◦Specific element
InsertionDeletion
BST Operations
![Page 32: Trees Types and Operations](https://reader036.vdocuments.site/reader036/viewer/2022062323/568162a1550346895dd318d7/html5/thumbnails/32.jpg)
delete When we delete a node, we need to
consider how we take care of the children of the deleted node.
This has to be done such that the property of the search tree is maintained.
![Page 33: Trees Types and Operations](https://reader036.vdocuments.site/reader036/viewer/2022062323/568162a1550346895dd318d7/html5/thumbnails/33.jpg)
deleteThree cases:(1) the node is a leaf
◦ Delete it immediately(2) the node has one sub-tree (right or left)
◦ Adjust a pointer from the parent to bypass that node
![Page 34: Trees Types and Operations](https://reader036.vdocuments.site/reader036/viewer/2022062323/568162a1550346895dd318d7/html5/thumbnails/34.jpg)
delete(3) the node has 2 children
◦ replace the key of that node with the minimum element at the right subtree (or the maximum element at the left 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. So invoke case 1 or 2.
Time complexity = O(height of the tree)
![Page 35: Trees Types and Operations](https://reader036.vdocuments.site/reader036/viewer/2022062323/568162a1550346895dd318d7/html5/thumbnails/35.jpg)
delete
![Page 36: Trees Types and Operations](https://reader036.vdocuments.site/reader036/viewer/2022062323/568162a1550346895dd318d7/html5/thumbnails/36.jpg)
Basic ConceptsBST OperationsThreaded Trees
2. Binary Search Tree
![Page 37: Trees Types and Operations](https://reader036.vdocuments.site/reader036/viewer/2022062323/568162a1550346895dd318d7/html5/thumbnails/37.jpg)
Sparing recursion and stackMaking use of null right child of leaves to point to next node
Threaded Trees
![Page 38: Trees Types and Operations](https://reader036.vdocuments.site/reader036/viewer/2022062323/568162a1550346895dd318d7/html5/thumbnails/38.jpg)
1. General Trees2. Binary Search Trees3. AVL Trees4. Heap Trees
Agenda
![Page 39: Trees Types and Operations](https://reader036.vdocuments.site/reader036/viewer/2022062323/568162a1550346895dd318d7/html5/thumbnails/39.jpg)
PropertiesOperations
3. AVL Trees
![Page 40: Trees Types and Operations](https://reader036.vdocuments.site/reader036/viewer/2022062323/568162a1550346895dd318d7/html5/thumbnails/40.jpg)
It is a balanced binary tree (definition of Russian mathematicians Adelson-Velskii and Landis)
The height of its sub-trees differs by no more than one (its balance factor is -1, 0, or 1), and its subtrees are also balanced.
Properties of AVL Trees
![Page 41: Trees Types and Operations](https://reader036.vdocuments.site/reader036/viewer/2022062323/568162a1550346895dd318d7/html5/thumbnails/41.jpg)
A sub tree is called Left high (LH) if its balance is 1Equally high (EH) if it is 0Right high (RH) if it is -1
Properties of AVL Trees
![Page 42: Trees Types and Operations](https://reader036.vdocuments.site/reader036/viewer/2022062323/568162a1550346895dd318d7/html5/thumbnails/42.jpg)
Insertion and deletion are same as in BST
If unbalance occurs corresponding rotations must be performed to restore balance
Operations on AVL Trees
![Page 43: Trees Types and Operations](https://reader036.vdocuments.site/reader036/viewer/2022062323/568162a1550346895dd318d7/html5/thumbnails/43.jpg)
Balanced trees: AVL tree rotations Steps:
◦ Check if case is case 1 or 2 of the following and act accordingly
◦Case 1: tree is left high & out-of-balance is created by a adding node to the left
of the left sub-tree
◦ …… One right rotation is needed Rotate out-of-balance node right
![Page 44: Trees Types and Operations](https://reader036.vdocuments.site/reader036/viewer/2022062323/568162a1550346895dd318d7/html5/thumbnails/44.jpg)
Case 1: single R-rotation
hh hhh
h+1h+2 h+
1h+1
•Case 1 * Tree is left balanced unbalance is caused by node on the left of left sub-tree
![Page 45: Trees Types and Operations](https://reader036.vdocuments.site/reader036/viewer/2022062323/568162a1550346895dd318d7/html5/thumbnails/45.jpg)
Balanced trees: AVL tree rotations
◦Case 2: tree is left highout-of-balance is created by a adding node to
the right of the left sub-tree
◦ …… Two rotations are needed:Move from bottom of left sub-tree upwards
till an unbalanced node is found and rotate it left
Rotate left sub-tree right
![Page 46: Trees Types and Operations](https://reader036.vdocuments.site/reader036/viewer/2022062323/568162a1550346895dd318d7/html5/thumbnails/46.jpg)
Case 2: Double LR-rotation
h+2h+1
Add node to right of left balanced subtree
First rotation .. Left rotation of unbalanced node c
Second rotation … Right rotation of left sub-tree g
h
h
h h
hh+1
h+2
h+2h+
1h+1
![Page 47: Trees Types and Operations](https://reader036.vdocuments.site/reader036/viewer/2022062323/568162a1550346895dd318d7/html5/thumbnails/47.jpg)
End