220 likes | 541 Views
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:
E N D
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: • 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.
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
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
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.
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)
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
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 }
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
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
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)
Removal in a binary search treecase 2 – deleted item is NOT in a leaf delete 5 6
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
Removal in a binary search treecase 2 – deleted item is NOT in a leaf move 6 up Time complexity O(height_of_tree)
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 }