Topic 2: binary Trees COMP2003J: Data Structures and Algorithms 2

Slides:



Advertisements
Similar presentations
Trees1 More on Trees University Fac. of Sci. & Eng. Bus. School Law School CS Dept. EE Dept. Math. Dept.
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.
Introduction to Trees. Tree example Consider this program structure diagram as itself a data structure. main readinprintprocess sortlookup.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Chapter 26 Binary Search Trees.
More Trees COL 106 Amit Kumar and Shweta Agrawal Most slides courtesy : Douglas Wilhelm Harder, MMath, UWaterloo
Review Binary Tree Binary Tree Representation Array Representation Link List Representation Operations on Binary Trees Traversing Binary Trees Pre-Order.
Spring 2010CS 2251 Trees Chapter 6. Spring 2010CS 2252 Chapter Objectives Learn to use a tree to represent a hierarchical organization of information.
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.
1 Chapter 17 Object-Oriented Data Structures. 2 Objectives F To describe what a data structure is (§17.1). F To explain the limitations of arrays (§17.1).
© 2006 Pearson Education Chapter 10: Non-linear Data Structures Presentation slides for Java Software Solutions for AP* Computer Science A 2nd Edition.
Computer Science 112 Fundamentals of Programming II Introduction to Trees.
© Copyright 2012 by Pearson Education, Inc. All Rights Reserved. 1 Chapter 19 Binary Search Trees.
Lecture - 10 on Data Structures. 6:05:57 PM Prepared by, Jesmin Akhter, Lecturer, IIT,JU.
1 Chapter 7 Objectives Upon completion you will be able to: Create and implement binary search trees Understand the operation of the binary search tree.
M180: Data Structures & Algorithms in Java Trees & Binary Trees Arab Open University 1.
ADT Binary Search Tree Ellen Walker CPSC 201 Data Structures Hiram College.
Rooted Tree a b d ef i j g h c k root parent node (self) child descendent leaf (no children) e, i, k, g, h are leaves internal node (not a leaf) sibling.
Lecture 17: Trees and Networks I Discrete Mathematical Structures: Theory and Applications.
Copyright © 2012 Pearson Education, Inc. Chapter 20: Binary Trees.
Trees Ellen Walker CPSC 201 Data Structures Hiram College.
CH 7 : TREE ACKNOWLEDGEMENT: THESE SLIDES ARE ADAPTED FROM SLIDES PROVIDED WITH DATA STRUCTURES AND ALGORITHMS IN C++, GOODRICH, TAMASSIA AND MOUNT (WILEY.
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.
BINARY TREES Objectives Define trees as data structures Define the terms associated with trees Discuss tree traversal algorithms Discuss a binary.
Chapter 7 Trees_ Part2 TREES. Depth and Height 2  Let v be a node of a tree T. The depth of v is the number of ancestors of v, excluding v itself. 
(c) University of Washington20-1 CSC 143 Java Trees.
What is a Tree? Formally, we define a tree T as a set of nodes storing elements such that the nodes have a parent-child relationship, that satisfies the.
SUYASH BHARDWAJ FACULTY OF ENGINEERING AND TECHNOLOGY GURUKUL KANGRI VISHWAVIDYALAYA, HARIDWAR.
Binary Search Trees Chapter 7 Objectives
CSE 373 Data Structures Lecture 7
Trees Chapter 15.
CC 215 Data Structures Trees
Chapter 25 Binary Search Trees
Fundamentals of Programming II Introduction to Trees
Recursive Objects (Part 4)
Data Structures Binary Trees 1.
Binary Search Tree (BST)
Trees 8/7/2018 2:27 PM Binary Trees © 2010 Goodrich, Tamassia Trees.
Trees.
Binary Search Tree Chapter 10.
Lecture 22 Binary Search Trees Chapter 10 of textbook
Trees.
CSE 373 Data Structures Lecture 7
Chapter 17 Object-Oriented Data Structures
Binary Trees, Binary Search Trees
Chapter 20: Binary Trees.
Chapter 22 : Binary Trees, AVL Trees, and Priority Queues
Chapter 10: Non-linear Data Structures
Binary Trees.
Map interface Empty() - return true if the map is empty; else return false Size() - return the number of elements in the map Find(key) - if there is an.
Data Structures Balanced Trees CSCI
CMSC 341 Lecture 10 B-Trees Based on slides from Dr. Katherine Gibson.
Chapter 21: Binary Trees.
COMP26120: Algorithms and Imperative Programming
Find in a linked list? first last 7  4  3  8 NULL
B- Trees D. Frey with apologies to Tom Anastasio
Week nine-ten: Trees Trees.
B- Trees D. Frey with apologies to Tom Anastasio
Copyright © Aiman Hanna All rights reserved
Trees CMSC 202, Version 5/02.
Lecture 12 CS203 1.
CSC 143 Java Trees.
Trees.
Trees.
Data Structures Using C++ 2E
Chapter 11 Trees © 2011 Pearson Addison-Wesley. All rights reserved.
NATURE VIEW OF A TREE leaves branches root. NATURE VIEW OF A TREE leaves branches root.
Tree (new ADT) Terminology: A tree is a collection of elements (nodes)
Presentation transcript:

Topic 2: binary Trees COMP2003J: Data Structures and Algorithms 2 Dr. David Lillis (david.lillis@ucd.ie) UCD School of Computer Science Beijing-Dublin International College

Binary Tree A Binary Tree is a special type of tree. This means that it has all of the normal properties of a tree that we discussed in the last lecture. However, it also has some additional properties: Every node has at most 2 children (degree 2). Each child node is labelled as being either a left child or a right child. A left child comes before a right child in the ordering of children of a node. i.e. whenever we have to process the children of a node, we do the left one first, then the right. Proper Binary Tree: a Binary Tree in which all nodes have degree 0 or 2.

Binary Tree A Binary Tree, T, is either empty or combines: a node r, called the root of T, which stores an element a binary tree, called the left subtree of T a binary tree, called the right subtree of T Level Property: Level d of a binary tree is the set of all nodes with depth d, of which there are at most 2d nodes.

Example: Arithmetic Operations Level 0: Max 20 nodes = 1 x Level 1: Max 21 nodes = 2 + - Level 2: Max 22 nodes = 4 1 3 7 + Level 3: Max 23 nodes = 8 2 1

Binary Tree ADT Binary Tree ADT = Tree ADT + 4 extra operations: left(n) return the Node of n’s left child right(n) return the Node of n’s right child hasLeft(n) test whether n has a left child hasRight(n) test whether n has a right child The corresponding Java Interface mirrors this: public interface IBinaryTree<T> extends ITree<T> { public INode<T> left(INode<T> n); public INode<T> right(INode<T> n); public boolean hasLeft(INode<T> n); public boolean hasRight(INode<T> n); } We still don’t provide methods to add/remove from the tree – these are left to the implementation strategy.

Link-Based Binary Tree: Structure Similar to how we use link-based implementation for list/stack/queue: Nodes contain data (the element) Key Relationships: parent / child (not previous / next) Entry point: The root node Additional Issues: the number of nodes in the tree (size) root  parent size element (data) “Albert” 3 left child right child “Betty” “Chris”    

Link-Based Binary Tree: Operations Update Operations: addRoot(e) create and return a new root node storing e; an error should occur if the tree is not empty insertLeft(n, e) create and return a new node storing e as the left child of n; an error should occur if n already has a left child. insertRight(n, e) create and return a new node storing e as the right child of n; an error should occur if n already has a right child. remove(n) remove node n and replace it with its child, if any, and return the element stored at n; an error occurs if n has two children. attach(n, T1, T2) Attach T1 and T2 respectively, as the left and right subtrees of the external node n; an error occurs if n is not external.

Proper Linked Binary Trees For a Proper Binary Tree: Every node, n, has degree 0 (external node) or 2 (internal node). Build the tree by expanding external nodes to become internal nodes. Default Implementation: Only internal nodes hold data. Less flexible but can simplify the implementation of data structures. We generally use empty squares to represent the external nodes, which do not hold data. Internal Nodes (store data) a b c d e f External Nodes (do not store data) g

Proper Linked Binary Trees Key Operations: expandExternal(n,e) Create two new empty nodes (i.e. that have no value) and add them as the left and right children of n, and store data e at n. An error occurs if n is not external. remove(n) If the left child is external, remove it and n and replace n with the right child. If the right child is external, remove it and n and replace it with the left child. Error if both children are internal or n is external.

Expanding an External Node 6 Let’s expand the right child of “8” to store the number 3. 2 9 1 4 8

Expanding an External Node 6 The node to be expanded is given two empty child nodes. 2 9 1 4 8

Expanding an External Node 6 The value (3) is stored in the expanded node. Internal nodes cannot be expanded. This is the method used to add nodes to a proper binary tree. 2 9 1 4 8 3

Removing a Node Only an internal node with at least one external child can be removed. In this example, 6 and 2 cannot be removed. Neither can any of the external nodes (it would no longer be a proper binary tree). 6 2 9 1 4 8 3

Removing a Node Let’s remove 9. 9 has an external child (right child) so it can be removed. We will remove 9 and its right child. 9 is replaced with its left child (8). 6 2 9 1 4 8 3

Removing a Node Let’s remove 9. 9 has an external child (right child) so it can be removed. We will remove 9 and its right child. 9 is replaced with its left child (8). 6 2 8 1 4 3

Removing a Node Now let’s remove 8. 8 has an external child (left child) so it can be removed. We will remove 8 and its left child. 8 is replaced with its right child (3). 6 2 8 1 4 3

Removing a Node Now let’s remove 8. 8 has an external child (left child) so it can be removed. We will remove 8 and its left child. 8 is replaced with its right child (3). 6 2 3 1 4

Removing a Node Finally, we remove 3. 3 has an external child (in fact it has 2), so it can be removed. We remove 3 and its left child, replacing it with its right child. 6 2 3 1 4

Removing a Node Finally, we remove 3. 3 has an external child (in fact it has 2), so it can be removed. We remove 3 and its left child, replacing it with its right child. 6 2 1 4

Tree Traversals and the Visitor Pattern

Visitor Pattern Design Pattern: a general reusable solution to a commonly occurring problem within a given context in software design. Visitor Pattern: a way of separating an algorithm from an object structure on which it operates. Traversal Algorithms are used to visit nodes in a tree. Same algorithm applies to general trees and binary trees. Visitor Pattern allows us to implement once and reuse.

Traversing a Tree When visiting nodes in a tree structure, it can be important to decide what order to visit the nodes in. The act of travelling through a tree visiting nodes is known as a traversal. Depending on the order you want to visit the nodes in, you can choose an appropriate traversal type. There are several options. The choice of traversal type will depend on what you are using the tree to represent.

“Visiting” a node. The act of “visiting” a node generally involves some kind of processing. It will depend on what you are trying to do. Some examples: Print the object stored in the node. Make a calculation based on the node’s value. Find a particular value in the tree. Some more complex processing.

Preorder Traversal of a Binary Tree Algorithm binaryPreorder(T,v): perform the "visit" action for node v if v has a left child u in T then binaryPreorder(T,u) if v has a right child w in T then binaryPreorder(T,w) A preorder traversal visits a node before it recursively visits its subtrees (left first, then right). When we recursively visit a subtree, it means that we visit all the descendants in that subtree before we go further.

Preorder Traversal Next we visit the left child of 7 (which is 3). We must recursively finish the traversal of this sub-tree before we do the right child (12). After visiting the left subtree, we can now start on the right. 1 2 9 3 6 10 14 NOTE: This slide has animations to help to explain the ordering. Traversal order: 7, 3, 1, 0, 2, 6, 4, 5, 12, 9, 8, 11, 10, 13, 15, 14 4 5 7 11 12 15 8 13 16

Why preorder traversal? The type of traversal depends on what kind of data your tree represents. Preorder traversal is useful for: Making a copy of a tree. Since parent nodes are visited before children, we can create the parents in the copy before adding their children.

Inorder Traversal Algorithm binaryInorder(T,v): if v has a left child u in T then binaryInorder(T,u) perform the "visit" action for node v if v has a right child w in T then binaryInorder(T,w) An inorder traversal visits a node after recursively visiting its left subtree, but before recursively visiting the right subtree. A node cannot be visited until all of its descendants in the left subtree have been visited (but before any in the right subtree).

Inorder Traversal This time, we must recursively visit the left subtree first, before visiting the root node (7). But: when we recursively visit the left subtree, we must also visit 3’s left child before 3 itself. The same is true at 1. 8 4 13 Traversal order: 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15 2 7 10 14 NOTE: This slide has animations to help to explain the ordering. 1 3 5 9 12 16 6 11 15

Why Inorder traversal? When using a binary search tree (which we will look at in the next lecture), performing an inorder traversal means that we can visit the values stored in the tree in sorted order.

Postorder Traversal of a Binary Tree Algorithm binaryPostorder(T,v): if v has a left child u in T then binaryPostorder(T,u) if v has a right child w in T then binaryPostorder(T,w) perform the "visit" action for node v A postorder traversal visits a node after recursively visiting all its child subtrees (left first, then right). A node will not be visited until all its descendants have been visited.

Postorder Traversal The root node will always be last in the traversal, as we must visit all its descendants first. The left subtree is first, but again we cannot visit 3 (or 1) until after all its descendants. Now that all of 3’s descendants have been visited, we can visit 3. We must do the entire right subtree before visiting 7. Traversal order: 0, 2, 1, 5, 4, 6, 3, 8, 10, 11, 9, 14, 15, 13, 12, 7 16 7 15 3 6 11 14 NOTE: This slide has animations to help to explain the ordering. 1 2 5 8 10 13 4 9 12

Why Postorder Traversal? When freeing memory in a tree, deleting in postorder order means that we don’t delete parent nodes before their children. Deleting parent nodes would mean that we no longer have references to the child nodes. When using a tree to represent a filesystem, calculating the size of a directory/folder is best done in a postorder fashion. Directory size is defined as the sum of the size of all files in a directory or its subdirectories. Postorder traversal means that we can calculate the size of all of the subdirectories first (child subtrees), and then sum those to get the size of each directory. Note that this would not be a binary tree: preorder and postorder traversals apply to all types of trees.

A Problem to Think About Suppose there is a binary tree storing letters. The tree does not contain duplicate values. It is not a binary search tree. If you were given any two of the outputs from the three traversals (preorder, postorder, inorder), can you recreate the binary tree? If so, how? Which 2 do you need? Example: Preorder: F B A D C E G I H Inorder: A B C D E F G H I Postorder: A C E D B H I G F