Binary Trees.

Slides:



Advertisements
Similar presentations
Binary Search Trees Azhar Maqsood School of Electrical Engineering and Computer Sciences (SEECS-NUST)
Advertisements

Binary Trees. DCS – SWC 2 Binary Trees Sets and Maps in Java are also available in tree-based implementations A Tree is – in this context – a data structure.
Binary Trees, Binary Search Trees CMPS 2133 Spring 2008.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved L12 (Chapter 20) Lists, Stacks,
Chapter 13 Binary Search Trees. Copyright © 2005 Pearson Addison-Wesley. All rights reserved Chapter Objectives Define a binary search tree abstract.
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.
CS 146: Data Structures and Algorithms June 18 Class Meeting Department of Computer Science San Jose State University Summer 2015 Instructor: Ron Mak
Maps A map is an object that maps keys to values Each key can map to at most one value, and a map cannot contain duplicate keys KeyValue Map Examples Dictionaries:
Geoff Holmes and Bernhard Pfahringer COMP206-08S General Programming 2.
1 HEAPS & PRIORITY QUEUES Array and Tree implementations.
Lecture Objectives  To learn how to use a tree to represent a hierarchical organization of information  To learn how to use recursion to process 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.
BINARY SEARCH TREE. Binary Trees A binary tree is a tree in which no node can have more than two children. In this case we can keep direct links to the.
Binary Search Trees Binary Search Trees (BST)  the tree from the previous slide is a special kind of binary tree called a binary.
1 Chapter 10 Trees. 2 Definition of Tree A tree is a set of linked nodes, such that there is one and only one path from a unique node (called the root.
CS 367 – Introduction to Data Structures
1 Binary Trees and Binary Search Trees Based on Dale & Co: Object-Oriented Data Structures using C++ (graphics)
ADT Binary Search Tree Ellen Walker CPSC 201 Data Structures Hiram College.
Lecture - 11 on Data Structures. Prepared by, Jesmin Akhter, Lecturer, IIT,JU Threaded Trees Binary trees have a lot of wasted space: the leaf nodes each.
AVL Trees and Heaps. AVL Trees So far balancing the tree was done globally Basically every node was involved in the balance operation Tree balancing can.
Copyright © 2012 Pearson Education, Inc. Chapter 20: Binary Trees.
CMSC 202, Version 5/02 1 Trees. CMSC 202, Version 5/02 2 Tree Basics 1.A tree is a set of nodes. 2.A tree may be empty (i.e., contain no nodes). 3.If.
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.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 20: Binary Trees.
SUYASH BHARDWAJ FACULTY OF ENGINEERING AND TECHNOLOGY GURUKUL KANGRI VISHWAVIDYALAYA, HARIDWAR.
Topic 2: binary Trees COMP2003J: Data Structures and Algorithms 2
Data Structures and Design in Java © Rick Mercer
File Organization and Processing Week 3
Binary Trees and Binary Search Trees
A Binary Search Tree Implementation
BST Trees
Priority Queues and Heaps
Chapter 16 – Advanced Data Structures
Lecture 15 AVL Trees Slides modified from © 2010 Goodrich, Tamassia & by Prof. Naveen Garg’s Lectures.
Binary Search Tree (BST)
Multiway search trees and the (2,4)-tree
Lecture 22 Binary Search Trees Chapter 10 of textbook
Trees.
COMP 103 Binary Search Trees.
Tonga Institute of Higher Education
ITEC 2620M Introduction to Data Structures
Chapter 17 Object-Oriented Data Structures
Binary Tree Applications
Binary Trees, Binary Search Trees
Chapter 20: Binary Trees.
Chapter 22 : Binary Trees, AVL Trees, and Priority Queues
Binary Search Trees.
(edited by Nadia Al-Ghreimil)
CMSC 341 Lecture 10 Binary Search Trees
Chapter 21: Binary Trees.
Search Sorted Array: Binary Search Linked List: Linear Search
Sets, Maps and Hash Tables
Trees CMSC 202, Version 5/02.
A Robust Data Structure
Lecture 12 CS203 1.
CMSC 202 Trees.
Binary Search Trees.
Data Structures and Algorithms
Binary Trees, Binary Search Trees
(edited by Nadia Al-Ghreimil)
CSE 143 Lecture 25 Set ADT implementation; hashing read 11.2
CSC 143 Binary Search Trees.
Tree.
Data Structures II AP Computer Science
Trees.
Binary Search Trees CS 580U Fall 17.
Binary Trees, Binary Search Trees
Data Structures Using C++ 2E
Heaps.
Presentation transcript:

Binary Trees

Binary Trees Sets and Maps in Java are also available in tree-based implementations A Tree is – in this context – a data structure which allows fast O(log(n)) insertion, deletion and lookup of elements Furthermore, a Tree maintains an ordering of the elements RHS – SWC

Binary Trees A Tree can be perceived as a generali-sation of a Linked List: RHS – SWC

Binary Trees A Tree consists of nodes, which contain data, and references to (up to) n other nodes n = 1: Linked List n = 2: Binary Tree If node A refers to node B, we say that Node A is the parent of node B Node B is a child of node A RHS – SWC

Binary Trees The node at the top of the tree is called the root node, and does not have a parent The nodes at the edge of the tree are called leaves, and do not have any children Root Leaf Leaf RHS – SWC

Binary Trees For a node A, we call the set of nodes consisting of the children of A, and the children of the children of A, and so forth, the descendants of A Descendants of 5 RHS – SWC

Binary Search Trees A Binary Tree is thus a special type of Tree, where each node has (up to) 2 children A Binary Search Tree has some additio-nal properties The data in each node must be of the type Comparable (i.e. implement the interface) The nodes must obey certain ordering rules RHS – SWC

Binary Search Trees Ordering rules for nodes in a binary search tree: The data values of all descendants to the left of any node are less than the data value stored in that node The data values of all descendants to the right of any node are larger than the data value stored in that node No duplicates allowed RHS – SWC

Binary Search Trees All these nodes are larger than 10 All these nodes are smaller than 10 RHS – SWC

Binary Search Trees Searching in a Binary Search Tree is quite fast: O(log(n)) How do we check if a value v is found in the tree? Set current node N = root node If value(N.data) = v, we are done, else If value(N.data) < v, set N = left child (stop if null) If value(N.data) > v, set N = right child (stop if null) Go to 2 RHS – SWC

Binary Search Trees public class BinarySearchTree { private Node root; private class Node {...} public BinarySearchTree() {root = null; } public boolean contains(Comparable data) {...} public void add(Comparable data) {...} public void delete(Comparable data) {...} } RHS – SWC

Binary Search Trees // NOTE: Inner class, public instance fields OK private class Node { public Comparable data; public Node left; public Node right; public Node(Comparable data) {...} public boolean contains(Comparable data) {...} public void add(Comparable data) {...} public void delete(Comparable data) {...} } RHS – SWC

Binary Search Trees // BinarySearchTree implementations public boolean contains(Comparable data) { if (root == null) return false; else return root.contains(); } public void add(Comparable data) if (root == null) root = new Node(data); else root.add(data); public void delete(Comparable data) ... RHS – SWC

Binary Search Trees } public boolean contains(Comparable v) { if (data.compareTo(v) == 0) return true; Node next; if (data.compareTo(v) < 0) next = left; else next = right; if (next == null) return false; return (next.contains()); } RHS – SWC

Binary Search Trees We can search a Binary Search Tree in O(log(n)), which is fast However, the condition for this ability is that the tree is always ”sorted”, i.e. obeys the ordering rules Adding or deleting an element must preserve this ordering! RHS – SWC

Binary Search Trees Adding a new element E with value v is done using a recursive algorithm Set current node N = root node If N = null, replace it with E, else If value(N.data) = v, we are done, else If value(N.data) < v, set N = left child If value(N.data) > v, set N = right child Go to 2 RHS – SWC

Binary Search Trees public void add(Comparable v) { if (data.compareTo(v) < 0) if (left == null) left = new Node(v); else left.add(v); } else if (data.compareTo(v) > 0) if (right == null) right = new Node(v); right.add(v); RHS – SWC

Binary Search Trees Deletion is actually the hardest part What happens to the children of some deleted node N? We must handle the cases where N has 0, 1 or 2 children 0 children: Easy. Just find N and delete it. Parent reference (if any) to N must be set to null RHS – SWC

Binary Search Trees N Before After RHS – SWC

Binary Search Trees 1 child: Fairly easy. Find N and delete it. Parent refe-rence to N must be rerouted to the child of N Note that if N is the root node, then the child of N becomes the new root node! RHS – SWC

Binary Search Trees N C C Before After Note that C may have children itself… C Before After RHS – SWC

Binary Search Trees 2 children: Complicated… Idea is to move the node with the next larger value in the tree up to take the position of N Next larger value is found as the leftmost node in the right subtree of N Keep going left in that subtree, until a node with no left child is found. Call this node L Replace N with L RHS – SWC

Binary Search Trees Replace N with L Copy data from L into N (not links!) Delete original L, following the procedure for deleting a node with zero or a single child (L cannot have a left child) These operations will preserve the ordering properties of the tree RHS – SWC

Tree Traversal The fact that a Binary Search Tree is ordered, make certain tasks quite easy For instance, printing out the content of the tree in sorted order RHS – SWC

Tree Traversal Printing out a tree in sorted order: Print out the left subtree Print out data in the root node Print out the right subtree Again, a highly recursive algorithm… RHS – SWC

Tree Traversal public void printNodes() // Node class { if (left != null) left.printNodes(); System.out.print(data + ” ”); if (right != null) right.printNodes(); } RHS – SWC

Tree Traversal // BinarySearchTree class public void printTree() { if (root != null) root.printNodes(); } RHS – SWC

Tree Traversal 5 2 7 1 4 6 8 3 RHS – SWC

Tree Traversal This is known as in-order tree traversal: Apply operation to left subtree Apply basic operation to root Apply operation to right subtree RHS – SWC

Tree Traversal We also have pre-order tree traversal: Apply basic operation to root Apply operation to left subtree Apply operation to right subtree RHS – SWC

Tree Traversal 1 2 6 3 4 7 8 5 RHS – SWC

Tree Traversal And finally post-order tree traversal: Apply operation to left subtree Apply operation to right subtree Apply basic operation to root RHS – SWC

Tree Traversal 8 4 7 1 3 5 6 2 RHS – SWC

Tree Traversal What tree traversal method to use depends entirely on your application In-order outputs the content of the tree in sorted order Pre- and post-order do not, but are used for other types of algorithms RHS – SWC

Choosing a proper container We have now learned about many types of containers for data Which one is best…? It depends… Usage scenarios Data types RHS – SWC

Choosing a proper container Issues in choosing a proper container How do you access the elements? Does element order matter? Which operations must be fast? Choosing between hash tables and trees (when using sets and maps) Should I supply a comparator (when using trees)? RHS – SWC

Choosing a proper container How do you access the elements? If you need access by a key, you should use a map If you need access by an index, you should use an array or array list If you only need to check if an element is already present in your container, you can use a set RHS – SWC

Choosing a proper container Does element order matter? If elements should remain sorted, use a TreeSet If the order of insertion should be preserved, use a linked list, array or array list If it does not matter, let other criteria decide your choice RHS – SWC

Choosing a proper container Which operations must be fast? Add and remove at the end of the container, use a linked list Looking up a value quickly, use a set or map If it does not matter, let other criteria decide your choice RHS – SWC

Choosing a proper container Choosing between hash tables and trees (when using sets and maps) If your elements/keys are strings, use a hash table If your elements/keys are defined by yourself, define proper hashCode and equals methods and use hash tables If your elements/keys are defined by someone else, use hash tables if the hashCode and equals methods are properly defined, otherwise use trees RHS – SWC

Choosing a proper container Should I supply a comparator (when using trees)? If the data type in your tree implements the Comparable interface properly, then no need for further action If not, you can still use a tree anyway, by supplying a class that implements the Comparator interface, that can compare objects of the type used in the tree RHS – SWC