Presentation is loading. Please wait.

Presentation is loading. Please wait.

Basic Data Structures - Trees

Similar presentations


Presentation on theme: "Basic Data Structures - Trees"— 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 Every node must have its value(s) Non-leaf node has subtree(s)
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 Multi-way Trees (order k)
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
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)} Time per level O(1) O(1) h Total O(h)

8 Insertion in a binary search tree examples
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 Insertion in 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 2 3 4 5 6 7 8 9 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 tree case 1 – deleted item is in a leaf
4 Easy! 4 6 Time complexity O(height_of_tree)

13 Removal in a binary search tree case 2 – deleted item is NOT in a leaf
6

14 Removal in a binary search tree case 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 tree case 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 }


Download ppt "Basic Data Structures - Trees"

Similar presentations


Ads by Google