Presentation is loading. Please wait.

Presentation is loading. Please wait.

COSC 1030 Lecture 9 Binary Trees. Topics Basic Concept and Terminology Applications of Binary Tree Complete Tree Representation Traversing Binary Trees.

Similar presentations


Presentation on theme: "COSC 1030 Lecture 9 Binary Trees. Topics Basic Concept and Terminology Applications of Binary Tree Complete Tree Representation Traversing Binary Trees."— Presentation transcript:

1 COSC 1030 Lecture 9 Binary Trees

2 Topics Basic Concept and Terminology Applications of Binary Tree Complete Tree Representation Traversing Binary Trees Search Ordered Binary Trees Insert Delete Heap

3 Concepts Trees – General, n’ary tree, Binary Search tree A linked, non-circular graph (with a node as root) Degree (maximum number of children) – Node Root, Leaf, In-Node Parent, Ancestor, Child, Descendent, Sibling Depth, Level (root at 0) – Sub trees Left sub tree, right sub tree, ith sub tree – Branch Path, path length

4 Binary Tree - Concepts – Degree = 2 – Full Binary Tree All non-leaf nodes have two children All leaves at the same level 2 ^ (k + 1) – 1 nodes – Extended Binary Trees All non-leaf nodes have two children With empty leaves explicitly shown as  – Complete Binary Tree Leaves on at most two adjacent levels All leaves go as left as possible

5 Complete Binary Tree Sequential Representation – Array A A:RDKBFJIACGL 01234567891011 To Find:Use:Provided: The RootA[1]A is nonempty The left child of A[i]A[2 i]2 i <= n The right child of A[i]A[2 i + 1]2 i + 1 <= n The parent of A[i]A[i/2]i > 1 Whether A[i] is a leaftrue2 i > n

6 Applications Heap – a special binary tree – No child is bigger than its parent – Insert – O(log n) Add new element as left as possible While (bigger than its parent) switch with parent – Remove – O(log n) Remove root While (not a leaf) swap bigger child up Place the last node to the hole – Dynamic memory allocation

7 Tree ADT Construct an empty tree Check whether a tree is empty Insert a node Delete a node Search Count, height, traverse Maximum, minimum

8 Interface BinaryTreeSpec { // Constructor boolean isEmpty(); boolean insert(Item newItem); boolean delete(Key aKey); Item search(Key aKey); String toString(); // traverse int height(); int count(); Item minimum(); Item maximum(); }

9 Data Representation ItemNode root; boolean isEmpty() { return root == null; } Class ItemNode extends Item { BinaryTree parent; BinaryTree left; BinaryTree right; } Class BinaryTree implements BinaryTreeSpec { Item item; BinaryTree parent; BinaryTree left; BinaryTree right; boolean isEmpty() { return item == null; }

10 Traverse a Binary Tree Depth First – Pre Order – root, left tree, right tree – In Order – left tree, root, right tree – Post Order – left tree, right tree, root Width First – Level 0, level 1, …, level k

11 Tree Traversing void preOrderVisit() { info.visit(); // say print if(left != null) left.preOrderVisit(); if(right != null) right.preOrderVisit(); } Int count() { int count = 0; if(!isEmpty()) { count ++; if(left != null) count += left.count(); if(right != null) count += right.count(); } return count; }

12 void inOrderVisit() { if(left != null) left.inOrderVisit(); info.visit(); // say print if(right != null) right.inOrderVisit(); } void postOrderVisit() { if(left != null) left.postOrderVisit(); if(right != null) right.postOrderVisit(); info.visit(); // say print }

13 void levelOrderVisit() { Queue q = new Queue(); q.insert(this); while(!q.isEmpty()) { Tree t = (Tree) q.remove(); t.info.visit(); if(t.left != null) q.insert(t.left); if(t.right != null) q.insert(t.right); } Complexity?

14 Binary Search Tree Sorted Tree – Left sub tree < root < right sub tree – Minimum node == left most node – Maximum node == right most node Search (key) Insert (item) Find / Delete maximum or minimum Delete(key)

15 Item search(Item key) { Item result = null; switch(key.compareTo(info)) { case 0: result = info; break; case 1: if(right != null) result = right.search(key); break; case –1: if(left != null) result = left.search(key); break; } return result; }

16 boolean insert(Item anItem) { boolean inerted = false; switch(anItem.compareTo(info)) { case 0: break; case 1: if(right == null) { right = new Tree(anItem); inserted = true; } else inserted = right.insert(anItem); break; case –1: if(left == null) { left = new Tree(anItem); inserted = true; } else inserted = left.insert(anItem); break; } return inserted; }

17 Item remove(Item anItem) { Item temp = null; switch(anItem.compareTo(info)) { case 0: temp = info; // Found. Keep a reference if(height(left) > height(right)) { info = left.removeMax(); } else if (right != null) { // height(right) >= height(left) info = right.removeMin(); } else { // a leaf node breakParentLink(); } break; case 1: if(right != null) temp = right.remove(aKey); break; case –1: if(left != null) temp =left.remove(aKey); break; } return temp; }

18 Item removeMax() { BinaryTree temp = this; while(temp.right != null) temp = temp.right; Item maxItem = temp.info; // the right most temp.parent.right = temp.left; // delete the right most node return maxItem; }

19 void breakParentLink() { if(parent != null) { if(parent.getLeft() == this) { parent.setLeft(null); } else if(parent.getRight() == this) { parent.setRight(null); }

20 Complexity Analysis Average length of all paths A balanced binary tree has height log n – Best Case: leaves on at most two adjacent levels – O(log n) A bad balanced tree has height n – Worst case: stick tree, all in-node have only one child – O(n) Keep a tree almost balanced

21 Keep Balanced Rotate sub trees if the tree is not balanced Rotating is O(log n) 5 37 264 7 4 3 2 1 6 5 5 37 264 1


Download ppt "COSC 1030 Lecture 9 Binary Trees. Topics Basic Concept and Terminology Applications of Binary Tree Complete Tree Representation Traversing Binary Trees."

Similar presentations


Ads by Google