1 / 16

Basic Data Structures - Trees

Basic Data Structures - Trees. Informal : a tree is a structure that looks like a real tree (up-side-down) Formal : a tree is a connected graph with no cycles. Trees. A tree T is a set of nodes storing values in a parent-child relationship with following properties:

fritz-hood
Download Presentation

Basic Data Structures - Trees

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. Basic Data Structures - Trees • Informal: a tree is a structure that looks like a real tree (up-side-down) • Formal: a tree is a connected graph with no cycles.

  2. Trees A tree T is a set of nodes storing values in a parent-child relationship with following properties: • T has a special node called root. • Each node different from root has a parent node. • When there is no node, T is an empty tree.

  3. Trees size=8 subtree root value height=3 leaf Every node must have its value(s) Non-leaf node has subtree(s) Non-root node has a single parent node

  4. Binary Trees • Each node can have at most 2 sub-trees Multi-way Trees (order k) ------------------------------------------------------- • Each node can have at most k sub-trees

  5. Binary Search Trees A binary search tree is a tree satisfying the following properties: • It is a binary tree. • For every node with a value N, all values in its left sub-tree must less than or equal to N, and all values in its right sub-tree must be greater than or equal to N.

  6. This is NOT a binary search tree

  7. Searching in a binary search tree Time per level search( s, t ) { If(s==t’s value) return t; If(t is leaf) return null If(s < t’s value) search(s,t’s left tree) else search(s,t’s right tree)} O(1) O(1) h Total O(h)

  8. Insertion in a binary search treeexamples Insert 6 Insert 11 6 11 6 11 6 6 11 always insert to a leaf Time complexity O(height_of_tree) ? O(log n) if it is balanced n = size of the tree

  9. Insertionin a binary search tree insertInOrder(s, t) { if(t is an empty tree) // insert here return a new tree node with value s else if( s < t’s value) t.left = insertInOrder(s, t.left) else t.right = insertInOrder(s, t.right) return t }

  10. Comparison –Insertion in an ordered list insertInOrder(s, list) { loop1: search from beginning of list, look for an item >= s loop2: shift remaining list to its right, start from the end of list insert s } Insert 6 6 6 6 6 8 9 2 3 4 5 7 6 Time complexity? O(n) n = size of the list

  11. Deleting an item from a list deleteItem(s, list) { loop1: search from beginning of list, look for an item == s loop2: shift remaining list to its left } delete 6 6 6 6 6 2 3 4 5 6 7 8 9 Time complexity? O(n) n = size of the list

  12. Removal in a binary search treecase 1 – deleted item is in a leaf delete 4 4 Easy! 4 6 Time complexity O(height_of_tree)

  13. Removal in a binary search treecase 2 – deleted item is NOT in a leaf delete 5 6

  14. Removal in a binary search treecase 2 – deleted item is NOT in a leaf Where is the value next to 5? delete 5 ? right subtree 6 ! It is in the left most node of “5” s right sub tree. leftmost

  15. Removal in a binary search treecase 2 – deleted item is NOT in a leaf move 6 up Time complexity O(height_of_tree)

  16. Removal in a binary search tree deleteItem(s,t) { x = searchItem(s,t) if(x == null) return // nothing to remove if(x is a leaf) remove node x else if(x.right != null) y = findLeftmost(x.right) else if(x.left != null) y = findRightmost(x.left) move value in y to x, then remove node y }

More Related