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

Slides:



Advertisements
Similar presentations
Chapter 7. Binary Search Trees
Advertisements

Computer Science C++ High School Level By Guillermo Moreno.
Binary Trees, Binary Search Trees CMPS 2133 Spring 2008.
Binary Trees, Binary Search Trees COMP171 Fall 2006.
1 Trees Tree nomenclature Implementation strategies Traversals –Depth-first –Breadth-first Implementing binary trees Reading: L&C 9.1 – 9.7.
© 2006 Pearson Addison-Wesley. All rights reserved11 B-1 Chapter 11 (continued) Trees.
CMPT 225 Priority Queues and Heaps. Priority Queues Items in a priority queue have a priority The priority is usually numerical value Could be lowest.
1 Chapter 6 Priority Queues (Heaps) General ideas of priority queues (Insert & DeleteMin) Efficient implementation of priority queue Uses of priority queues.
Binary Trees Terminology A graph G = is a collection of nodes and edges. An edge (v 1,v 2 ) is a pair of vertices that are directly connected. A path,
1 Trees Definition of a Tree Tree Terminology Importance of Trees Implementation of Trees Binary Trees –Definition –Full and Complete Binary Trees –Implementation.
Binary Trees A binary tree is made up of a finite set of nodes that is either empty or consists of a node called the root together with two binary trees,
1 Trees What is a Tree? Tree terminology Why trees? What is a general tree? Implementing trees Binary trees Binary tree implementation Application of Binary.
1 Trees What is a Tree? Tree terminology Why trees? What is a general tree? Implementing trees Binary trees Binary tree implementation Application of Binary.
Priority Queues. Priority queue A stack is first in, last out A queue is first in, first out A priority queue is least-first-out –The “smallest” element.
1 abstract containers hierarchical (1 to many) graph (many to many) first ith last sequence/linear (1 to 1) set.
1 Trees What is a Tree? Tree terminology Why trees? General Trees and their implementation N-ary Trees N-ary Trees implementation Implementing trees Binary.
Marc Smith and Jim Ten Eyck
1 BST Trees A binary search tree is a binary tree in which every node satisfies the following: the key of every node in the left subtree is.
Version TCSS 342, Winter 2006 Lecture Notes Trees Binary Trees Binary Search Trees.
CSCE 3110 Data Structures & Algorithm Analysis Binary Search Trees Reading: Chap. 4 (4.3) Weiss.
COSC2007 Data Structures II
Trees, Binary Search Trees, Recursion, Project 2 Bryce Boe 2013/08/01 CS24, Summer 2013 C.
1 Trees Tree nomenclature Implementation strategies Traversals –Depth-first –Breadth-first Implementing binary search trees.
Tree. Basic characteristic Top node = root Left and right subtree Node 1 is a parent of node 2,5,6. –Node 2 is a parent of node.
CS Data Structures Chapter 15 Trees Mehmet H Gunes
CS Data Structures Chapter 5 Trees. Chapter 5 Trees: Outline  Introduction  Representation Of Trees  Binary Trees  Binary Tree Traversals 
Information and Computer Sciences University of Hawaii, Manoa
© 2011 Pearson Addison-Wesley. All rights reserved 11 B-1 Chapter 11 (continued) Trees.
Trees CSCI Objectives Define trees as data structures Define the terms associated with trees Discuss the possible implementations of trees Analyze.
1 Trees A tree is a data structure used to represent different kinds of data and help solve a number of algorithmic problems Game trees (i.e., chess ),
CMSC 341 Introduction to Trees. 8/3/2007 UMBC CMSC 341 TreeIntro 2 Tree ADT Tree definition  A tree is a set of nodes which may be empty  If not empty,
AVL Trees CSCI 2720 Fall 2005 Kraemer. Binary Tree Issue  One major problem with the binary trees we have discussed thus far: they can become extremely.
Tree (new ADT) Terminology:  A tree is a collection of elements (nodes)  Each node may have 0 or more successors (called children)  How many does a.
Binary Trees, Binary Search Trees RIZWAN REHMAN CENTRE FOR COMPUTER STUDIES DIBRUGARH UNIVERSITY.
data ordered along paths from root to leaf
Topic 15 The Binary Search Tree ADT Binary Search Tree A binary search tree (BST) is a binary tree with an ordering property of its elements, such.
Outline Binary Trees Binary Search Tree Treaps. Binary Trees The empty set (null) is a binary tree A single node is a binary tree A node has a left child.
 Trees Data Structures Trees Data Structures  Trees Trees  Binary Search Trees Binary Search Trees  Binary Tree Implementation Binary Tree Implementation.
Lec 15 Oct 18 Binary Search Trees (Chapter 5 of text)
Data Structures Trees Phil Tayco Slide version 1.0 Apr. 23, 2015.
Chapter 11 B Trees. © 2004 Pearson Addison-Wesley. All rights reserved 11 B-2 The ADT Binary Search Tree A deficiency of the ADT binary tree which is.
COSC2007 Data Structures II Chapter 11 Trees IV. 2 Topics ADT BST Implementations Efficiency TreeSort Save/Restore into/from file General Trees.
PRIORITY QUEUES AND HEAPS Slides of Ken Birman, Cornell University.
CMSC 341 Introduction to Trees. 2/21/20062 Tree ADT Tree definition –A tree is a set of nodes which may be empty –If not empty, then there is a distinguished.
Trees. 2 Root leaf CHAPTER 5 3 Definition of Tree n A tree is a finite set of one or more nodes such that: n There is a specially designated node called.
COSC 2007 Data Structures II Chapter 13 Advanced Implementation of Tables I.
HEAPS. Review: what are the requirements of the abstract data type: priority queue? Quick removal of item with highest priority (highest or lowest key.
Week 15 – Wednesday.  What did we talk about last time?  Review up to Exam 1.
CS 367 Introduction to Data Structures Lecture 8.
1 Trees What is a Tree? Tree terminology Why trees? What is a general tree? Implementing trees Binary trees Binary tree implementation Application of Binary.
Course: Programming II - Abstract Data Types HeapsSlide Number 1 The ADT Heap So far we have seen the following sorting types : 1) Linked List sort by.
1 CMSC 341 Introduction to Trees Textbook sections:
Trees Chapter 11 (continued)
Trees Chapter 11 (continued)
CISC220 Fall 2009 James Atlas Lecture 13: Binary Trees.
Trees Tree nomenclature Implementation strategies Traversals
COMP 103 Binary Search Trees.
Binary Trees Lecture 36 Wed, Apr 21, /21/2018 Binary Trees.
Binary Trees, Binary Search Trees
Trees and Binary Trees.
Lec 12 March 9, 11 Mid-term # 1 (March 21?)
Find in a linked list? first last 7  4  3  8 NULL
Trees.
Tree Representation Heap.
Binary Trees, Binary Search Trees
Tree.
Tree and its terminologies
Priority Queue and Heap
Binary Trees, Binary Search Trees
Tree (new ADT) Terminology: A tree is a collection of elements (nodes)
Presentation transcript:

COSC 1030 Lecture 9 Binary Trees

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

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

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

Complete Binary Tree Sequential Representation – Array A A:RDKBFJIACGL 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

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

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

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(); }

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; }

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

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; }

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 }

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?

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)

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; }

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; }

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; }

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; }

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

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

Keep Balanced Rotate sub trees if the tree is not balanced Rotating is O(log n)