Principles of Computer Science

Size: px
Start display at page:

Download "Principles of Computer Science"

Transcription

1 Principles of Computer Science Binary Trees 08/11/2013 CSCI Binary Trees - F.Z. Qureshi 1

2 Today s Topics Extending LinkedList with Fast Search Sorted Binary Trees Tree Concepts Traversals of a Binary Tree Java Interfaces for Trees Operations on binary trees maketree() search( ) 08/11/2013 CSCI Binary Trees - F.Z. Qureshi 2

3 What is not great about using Arrays? Very difficult to maintain when the data is dynamic: How do you insert an element after the array is already sorted? How do you delete an element after the array is already sorted? Not easy (can be done using heaps). 08/11/2013 CSCI Binary Trees - F.Z. Qureshi 3

4 Trees Tree data structures provide a natural way of managing hierarchical organizations 08/11/2013 CSCI Binary Trees - F.Z. Qureshi 4

5 Hierarchical Organization Example: A university's organization A university's administrative structure. 08/11/2013 CSCI Binary Trees - F.Z. Qureshi 5

6 Tree Terminology A tree is A set of nodes Connected by edges The edges indicate relationships among nodes Nodes are arranged in levels Indicate the nodes' hierarchy Top level is a single node called the root 08/11/2013 CSCI Binary Trees - F.Z. Qureshi 6

7 Tree Terminology Nodes at a given level are children of nodes at the previous level A node with children is the parent node of those children Nodes having the same parent are siblings A node with no children is a leaf node The only node with no parent is the root node All others have one parent each 08/11/2013 CSCI Binary Trees - F.Z. Qureshi 7

8 Tree Terminology A node is reached from the root by a path The length of the path is the number of edges that compose it The height of a tree is the number of levels in the tree The subtree of a node is a tree rooted at a child of that node 08/11/2013 CSCI Binary Trees - F.Z. Qureshi 8

9 Binary Trees Each node has at most two children Three binary trees. 08/11/2013 CSCI Binary Trees - F.Z. Qureshi 9

10 Why Binary Trees? Binary Trees: Support fast insert and delete Support fast sort Support fast search 08/11/2013 CSCI Binary Trees - F.Z. Qureshi 10

11 Binary Trees A binary tree is either empty or has the following form T left and T right are themselves binary trees 08/11/2013 CSCI Binary Trees - F.Z. Qureshi 11

12 Binary Trees Every nonleaf in a full binary tree has exactly two children A complete binary tree is full to its next-to-last level Leaves on last level are filled from left to right The height of a binary tree with n nodes that is either complete or full is log 2 (n + 1) 08/11/2013 CSCI Binary Trees - F.Z. Qureshi 12

13 Binary Trees The number of nodes in a full binary tree as a function of the tree's height. 08/11/2013 CSCI Binary Trees - F.Z. Qureshi 13

14 Binary Trees Full Tree Height Number of Nodes The number of nodes in a full binary tree as a function of the tree's height. 08/11/2013 CSCI Binary Trees - F.Z. Qureshi 14

15 Traversals of a Tree Visiting a node Processing the data within a node This is the action performed on each node during the traversal of a tree A traversal can pass through a node without visiting it at that moment For a binary tree Visit the root Visit all nodes in the root's left subtree Visit all nodes in the root's right subtree 08/11/2013 CSCI Binary Trees - F.Z. Qureshi 15

16 Traversals of a Tree Preorder traversal: visit root before the subtrees 08/11/2013 CSCI Binary Trees - F.Z. Qureshi 16

17 Traversals of a Tree Inorder traversal: visit root between visiting the subtrees 08/11/2013 CSCI Binary Trees - F.Z. Qureshi 17

18 Traversals of a Tree Postorder traversal: visit root after visiting the subtrees These are examples of a depth-first traversal. 08/11/2013 CSCI Binary Trees - F.Z. Qureshi 18

19 Traversals of a Tree Level-order traversal: begin at the root, visit nodes one level at a time This is an example of a breadth-first traversal. 08/11/2013 CSCI Binary Trees - F.Z. Qureshi 19

20 Traversals of a General Tree A general tree has traversals that are Level order Preorder Postorder Inorder traversal is not well defined for a general tree 08/11/2013 CSCI Binary Trees - F.Z. Qureshi 20

21 Traversals of a General Tree (a) preorder. 08/11/2013 CSCI Binary Trees - F.Z. Qureshi 21

22 Traversals of a General Tree (b) postorder. 08/11/2013 CSCI Binary Trees - F.Z. Qureshi 22

23 Java Interfaces for Trees An interface that specifies operations common to all trees 08/11/2013 CSCI Binary Trees - F.Z. Qureshi 23

24 Java Interfaces for Trees Interface of traversal methods for a tree 08/11/2013 CSCI Binary Trees - F.Z. Qureshi 24

25 Implement BinaryTree in Java class BinaryTreeNode<T> { public T content; public BinaryTreeNode<T> leftchild; public BinaryTreeNode<T> rightchild; } <lots of methods> class BinaryTree<T> { public BinaryTreeNode<T> root; } <lots of methods> 08/11/2013 CSCI Binary Trees - F.Z. Qureshi 25

26 Binary Tree Consists of a root node. If a node is a leaf node if: leftchild = null, and rightchild = null. 08/11/2013 CSCI Binary Trees - F.Z. Qureshi 26

27 Binary Tree Traversal In Order class BinaryTree { BTNode root;... public void printinorder() { _printinorder(root); } private void _printinorder(btnode n) { if (n == null) return; } _printinorder(n.left); // print left subtree first System.out.println(n); // print the current node _printinorder(n.right); // print right subtree next }... 08/11/2013 CSCI Binary Trees - F.Z. Qureshi 27

28 Binary Tree Traversal Pre Order class BinaryTree { BTNode root;... public void printpreorder() { _printpreorder(root); } private void _printpreorder(btnode n) { if (n == null) return; } System.out.println(n); // print the current node first _printpreorder(n.left); // print left subtree next _printpreorder(n.right); // print right subtree last }... 08/11/2013 CSCI Binary Trees - F.Z. Qureshi 28

29 Binary Tree Traversal Post Order class BinaryTree { BTNode root;... public void printpostorder() { _printpostorder(root); } private void _printpostorder(btnode n) { if (n == null) return; } _printpostorder(n.left); // print left subtree first _printpostorder(n.right); // print right subtree second System.out.println(n); // print the current node last }... 08/11/2013 CSCI Binary Trees - F.Z. Qureshi 29

30 Sorted Binary Tree A sorted binary tree is a binary tree with the property: for every node n, Left descendants n <= n < Right descendants of n 08/11/2013 CSCI Binary Trees - F.Z. Qureshi 30

31 Sorted Binary Trees Sorted binary trees are sometimes called Binary Search Trees These provide log(n) search, insertion and deletion In practice if you think that you need to search through a binary tree, ensure that it is sorted 08/11/2013 CSCI Binary Trees - F.Z. Qureshi 31

32 In Order Traversal of Sorted Binary Trees In order traversal of a sorted binary tree reaches nodes in increasing (sorted) order In order traversal Sorted 08/11/2013 CSCI Binary Trees - F.Z. Qureshi 32

33 Sorted Binary Tree - Insertions public void insertsorted(int value) { if (root == null) { // if this is the first value to be stored, root = new BTNode(value); // set it as the root return; } } _insertsorted(root, value); // else call the helper routine to store this // value at its appropriate location 08/11/2013 CSCI Binary Trees - F.Z. Qureshi 33

34 Sorted Binary Tree - Insertions private void _insertsorted(btnode n, int value) { if (n.value >= value) { // (1) if value less than current node, go left if (n.left!= null) { // if left child exists, recur _insertsorted(n.left, value); } else { BTNode nn = new BTNode(value); // if nothing at left child, store value as left n.left = nn; // child of this node } } else { // (1) else go right if (n.right!= null) { // if right child exists, recur } } _insertsorted(n.right, value); } else { BTNode nn = new BTNode(value); // if nothing at right child, store value as n.right = nn; // right child of this node } 08/11/2013 CSCI Binary Trees - F.Z. Qureshi 34

35 Binary Search Trees A search tree organizes its data so that a search is more efficient Binary search tree Nodes contain Comparable objects A node's data is greater than the data in the node's left subtree A node's data is less than the data in the node's right subtree 08/11/2013 CSCI Binary Trees - F.Z. Qureshi 35

36 Searching Through Sorted Binary Tree Given a sorted binary tree, we need to find the node that contains the value of interest: class BinaryTree { BinaryTreeNode<T> search(t value); } 08/11/2013 CSCI Binary Trees - F.Z. Qureshi 36

37 Binary Search Through Sorted Tree class BinaryTree<T> {... public BinaryTreeNode<T> search(t value) { return searchatnode(value, this.root); } public BinaryTreeNode<T> searchatnode( T value, BinaryTreeNode<T> node) { if(node == null) return null; } } if(node.content == value) return node; else if(node.content > value) return searchatnode(value, node.leftchild); else return searchatnode(value, node.rightchild); 08/11/2013 CSCI Binary Trees - F.Z. Qureshi 37

38 Sorted Binary Trees - Deletions Case 1: Deleting a leaf node parent node (parent.left) 08/11/2013 CSCI Binary Trees - F.Z. Qureshi 38

39 Sorted Binary Trees - Deletions Case 1: Deleting a leaf node This case is easy. Just remove the node. parent.left = null 08/11/2013 CSCI Binary Trees - F.Z. Qureshi 39

40 Sorted Binary Trees - Deletions Case 2: Deleting a node with a single child (i.e., having only left or right subtree but not both) parent node (parent.left) child (node.right) 08/11/2013 CSCI Binary Trees - F.Z. Qureshi 40

41 Sorted Binary Trees - Deletions Case 2: Deleting a node with a single child (i.e., having only left or right subtree but not both) Replace the node with its child parent.left = node.left 08/11/2013 CSCI Binary Trees - F.Z. Qureshi 41

42 Sorted Binary Trees - Deletions Case 3: Deleting a node with two children (i.e., having both left and right subtrees) parent node (parent.left) in order predecessor of parent 08/11/2013 CSCI Binary Trees - F.Z. Qureshi 42

43 Sorted Binary Trees - Deletions Case 3: Deleting a node with two children (i.e., having both left and right subtrees) If the current node is the left child of the parent, replace node with the in order predecessor of parent node (parent.left) parent 08/11/2013 CSCI Binary Trees - F.Z. Qureshi 43

44 Sorted Binary Trees - Deletions Case 3: Deleting a node with two children (i.e., having both left and right subtrees) If the current node is the right child of the parent, replace node with the in order successor of parent 08/11/2013 CSCI Binary Trees - F.Z. Qureshi 44

45 Sorted Binary Trees - Deletions Case 3: Deleting a node with two children (i.e., having both left and right subtrees) Or simply replace the node with either its inorder successor or inorder predecessor 08/11/2013 CSCI Binary Trees - F.Z. Qureshi 45

46 In Order Predecessor and Successor In order traversal Sorted In the example above, 4 is in order predecessor of 6 and 8 is in order successor of 6. Simply check how these nodes are printed in in order predecessor 08/11/2013 CSCI Binary Trees - F.Z. Qureshi 46

47 In Order Successor Input: node Output: succ Case 1: if node.left is not null, succ is the smallest element in the left subtree of node Case 2: otherwise, travel up using parent pointers until you reach a node that is the left child of its parent, the parent node is the succ Don t forget to take care of the special cases, e.g., what if the node has no succ? 08/11/2013 CSCI Binary Trees - F.Z. Qureshi 47

48 In Order Predecessor Input: node Output: pred Case 1: if node.right is not null, pred is the smallest element in the left subtree of node Case 2: otherwise, travel up using parent pointers until you reach a node that is the right child of its parent, the parent node is the pred Don t forget to take care of the special cases, e.g., what if the node has no pred? 08/11/2013 CSCI Binary Trees - F.Z. Qureshi 48

49 Summary Tree terminology Tree Traversals Binary Search Tree 08/11/2013 CSCI Binary Trees - F.Z. Qureshi 49

50 Readings Ch /11/2013 CSCI Binary Trees - F.Z. Qureshi 50

Binary Trees

Binary Trees Binary Trees 4-7-2005 Opening Discussion What did we talk about last class? Do you have any code to show? Do you have any questions about the assignment? What is a Tree? You are all familiar with what

More information

Binary Trees, Binary Search Trees

Binary Trees, Binary Search Trees Binary Trees, Binary Search Trees Trees Linear access time of linked lists is prohibitive Does there exist any simple data structure for which the running time of most operations (search, insert, delete)

More information

Programming II (CS300)

Programming II (CS300) 1 Programming II (CS300) Chapter 11: Binary Search Trees MOUNA KACEM mouna@cs.wisc.edu Fall 2018 General Overview of Data Structures 2 Introduction to trees 3 Tree: Important non-linear data structure

More information

A set of nodes (or vertices) with a single starting point

A set of nodes (or vertices) with a single starting point Binary Search Trees Understand tree terminology Understand and implement tree traversals Define the binary search tree property Implement binary search trees Implement the TreeSort algorithm 2 A set of

More information

a graph is a data structure made up of nodes in graph theory the links are normally called edges

a graph is a data structure made up of nodes in graph theory the links are normally called edges 1 Trees Graphs a graph is a data structure made up of nodes each node stores data each node has links to zero or more nodes in graph theory the links are normally called edges graphs occur frequently in

More information

Topic 14. The BinaryTree ADT

Topic 14. The BinaryTree ADT Topic 14 The BinaryTree ADT Objectives Define trees as data structures Define the terms associated with trees Discuss tree traversal algorithms Discuss a binary tree implementation Examine a binary tree

More information

Binary Trees. Height 1

Binary Trees. Height 1 Binary Trees Definitions A tree is a finite set of one or more nodes that shows parent-child relationship such that There is a special node called root Remaining nodes are portioned into subsets T1,T2,T3.

More information

TREES. Trees - Introduction

TREES. Trees - Introduction TREES Chapter 6 Trees - Introduction All previous data organizations we've studied are linear each element can have only one predecessor and successor Accessing all elements in a linear sequence is O(n)

More information

Chapter 20: Binary Trees

Chapter 20: Binary Trees Chapter 20: Binary Trees 20.1 Definition and Application of Binary Trees Definition and Application of Binary Trees Binary tree: a nonlinear linked list in which each node may point to 0, 1, or two other

More information

CS 206 Introduction to Computer Science II

CS 206 Introduction to Computer Science II CS 206 Introduction to Computer Science II 10 / 10 / 2016 Instructor: Michael Eckmann Today s Topics Questions? Comments? A few comments about Doubly Linked Lists w/ dummy head/tail Trees Binary trees

More information

Trees. (Trees) Data Structures and Programming Spring / 28

Trees. (Trees) Data Structures and Programming Spring / 28 Trees (Trees) Data Structures and Programming Spring 2018 1 / 28 Trees A tree is a collection of nodes, which can be empty (recursive definition) If not empty, a tree consists of a distinguished node r

More information

Tree Structures. A hierarchical data structure whose point of entry is the root node

Tree Structures. A hierarchical data structure whose point of entry is the root node Binary Trees 1 Tree Structures A tree is A hierarchical data structure whose point of entry is the root node This structure can be partitioned into disjoint subsets These subsets are themselves trees and

More information

Binary Trees. College of Computing & Information Technology King Abdulaziz University. CPCS-204 Data Structures I

Binary Trees. College of Computing & Information Technology King Abdulaziz University. CPCS-204 Data Structures I Binary Trees College of Computing & Information Technology King Abdulaziz University CPCS-204 Data Structures I Outline Tree Stuff Trees Binary Trees Implementation of a Binary Tree Tree Traversals Depth

More information

Advanced Java Concepts Unit 5: Trees. Notes and Exercises

Advanced Java Concepts Unit 5: Trees. Notes and Exercises Advanced Java Concepts Unit 5: Trees. Notes and Exercises A Tree is a data structure like the figure shown below. We don t usually care about unordered trees but that s where we ll start. Later we will

More information

CSE 230 Intermediate Programming in C and C++ Binary Tree

CSE 230 Intermediate Programming in C and C++ Binary Tree CSE 230 Intermediate Programming in C and C++ Binary Tree Fall 2017 Stony Brook University Instructor: Shebuti Rayana shebuti.rayana@stonybrook.edu Introduction to Tree Tree is a non-linear data structure

More information

Why Do We Need Trees?

Why Do We Need Trees? CSE 373 Lecture 6: Trees Today s agenda: Trees: Definition and terminology Traversing trees Binary search trees Inserting into and deleting from trees Covered in Chapter 4 of the text Why Do We Need Trees?

More information

tree nonlinear Examples

tree nonlinear Examples The Tree ADT Objectives Define trees as data structures Define the terms associated with trees Discuss tree traversal algorithms Discuss a binary tree implementation Examine a binary tree example 10-2

More information

Binary Trees. Examples:

Binary Trees. Examples: Binary Trees A tree is a data structure that is made of nodes and pointers, much like a linked list. The difference between them lies in how they are organized: In a linked list each node is connected

More information

Trees. Introduction & Terminology. February 05, 2018 Cinda Heeren / Geoffrey Tien 1

Trees. Introduction & Terminology. February 05, 2018 Cinda Heeren / Geoffrey Tien 1 Trees Introduction & Terminology Cinda Heeren / Geoffrey Tien 1 Review: linked lists Linked lists are constructed out of nodes, consisting of a data element a pointer to another node Lists are constructed

More information

Programming II (CS300)

Programming II (CS300) 1 Programming II (CS300) Chapter 10: Search and Heaps MOUNA KACEM mouna@cs.wisc.edu Spring 2018 Search and Heaps 2 Linear Search Binary Search Introduction to trees Priority Queues Heaps Linear Search

More information

CS 206 Introduction to Computer Science II

CS 206 Introduction to Computer Science II CS 206 Introduction to Computer Science II 02 / 24 / 2017 Instructor: Michael Eckmann Today s Topics Questions? Comments? Trees binary trees two ideas for representing them in code traversals start binary

More information

Lec 17 April 8. Topics: binary Trees expression trees. (Chapter 5 of text)

Lec 17 April 8. Topics: binary Trees expression trees. (Chapter 5 of text) Lec 17 April 8 Topics: binary Trees expression trees Binary Search Trees (Chapter 5 of text) Trees Linear access time of linked lists is prohibitive Heap can t support search in O(log N) time. (takes O(N)

More information

BBM 201 Data structures

BBM 201 Data structures BBM 201 Data structures Lecture 11: Trees 2018-2019 Fall Content Terminology The Binary Tree The Binary Search Tree Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, 2013

More information

Trees. Truong Tuan Anh CSE-HCMUT

Trees. Truong Tuan Anh CSE-HCMUT Trees Truong Tuan Anh CSE-HCMUT Outline Basic concepts Trees Trees A tree consists of a finite set of elements, called nodes, and a finite set of directed lines, called branches, that connect the nodes

More information

CS350: Data Structures Tree Traversal

CS350: Data Structures Tree Traversal Tree Traversal James Moscola Department of Engineering & Computer Science York College of Pennsylvania James Moscola Defining Trees Recursively Trees can easily be defined recursively Definition of a binary

More information

CISC 235 Topic 3. General Trees, Binary Trees, Binary Search Trees

CISC 235 Topic 3. General Trees, Binary Trees, Binary Search Trees CISC 235 Topic 3 General Trees, Binary Trees, Binary Search Trees Outline General Trees Terminology, Representation, Properties Binary Trees Representations, Properties, Traversals Recursive Algorithms

More information

INF2220: algorithms and data structures Series 1

INF2220: algorithms and data structures Series 1 Universitetet i Oslo Institutt for Informatikk A. Maus, R.K. Runde, I. Yu INF2220: algorithms and data structures Series 1 Topic Trees & estimation of running time (Exercises with hints for solution) Issued:

More information

Binary Tree. Preview. Binary Tree. Binary Tree. Binary Search Tree 10/2/2017. Binary Tree

Binary Tree. Preview. Binary Tree. Binary Tree. Binary Search Tree 10/2/2017. Binary Tree 0/2/ Preview Binary Tree Tree Binary Tree Property functions In-order walk Pre-order walk Post-order walk Search Tree Insert an element to the Tree Delete an element form the Tree A binary tree is a tree

More information

Advanced Java Concepts Unit 5: Trees. Notes and Exercises

Advanced Java Concepts Unit 5: Trees. Notes and Exercises dvanced Java Concepts Unit 5: Trees. Notes and Exercises Tree is a data structure like the figure shown below. We don t usually care about unordered trees but that s where we ll start. Later we will focus

More information

Data and File Structures Laboratory

Data and File Structures Laboratory Binary Trees Assistant Professor Machine Intelligence Unit Indian Statistical Institute, Kolkata September, 2018 1 Basics 2 Implementation 3 Traversal Basics of a tree A tree is recursively defined as

More information

Trees. Tree Structure Binary Tree Tree Traversals

Trees. Tree Structure Binary Tree Tree Traversals Trees Tree Structure Binary Tree Tree Traversals The Tree Structure Consists of nodes and edges that organize data in a hierarchical fashion. nodes store the data elements. edges connect the nodes. The

More information

Binary Trees Fall 2018 Margaret Reid-Miller

Binary Trees Fall 2018 Margaret Reid-Miller Binary Trees 15-121 Fall 2018 Margaret Reid-Miller Trees Fall 2018 15-121 (Reid-Miller) 2 Binary Trees A binary tree is either empty or it contains a root node and left- and right-subtrees that are also

More information

Trees, Binary Trees, and Binary Search Trees

Trees, Binary Trees, and Binary Search Trees COMP171 Trees, Binary Trees, and Binary Search Trees 2 Trees Linear access time of linked lists is prohibitive Does there exist any simple data structure for which the running time of most operations (search,

More information

Trees. Q: Why study trees? A: Many advance ADTs are implemented using tree-based data structures.

Trees. Q: Why study trees? A: Many advance ADTs are implemented using tree-based data structures. Trees Q: Why study trees? : Many advance DTs are implemented using tree-based data structures. Recursive Definition of (Rooted) Tree: Let T be a set with n 0 elements. (i) If n = 0, T is an empty tree,

More information

18. Binary Search Trees

18. Binary Search Trees Trees. Binary Search Trees [Ottman/Widmayer, Kap..1, Cormen et al, Kap. 12.1-12.] Trees are Generalized lists: nodes can have more than one successor Special graphs: graphs consist of nodes and edges.

More information

Lecture Notes 16 - Trees CSS 501 Data Structures and Object-Oriented Programming Professor Clark F. Olson

Lecture Notes 16 - Trees CSS 501 Data Structures and Object-Oriented Programming Professor Clark F. Olson Lecture Notes 16 - Trees CSS 501 Data Structures and Object-Oriented Programming Professor Clark F. Olson Reading: Carrano, Chapter 15 Introduction to trees The data structures we have seen so far to implement

More information

Abstract Data Structures IB Computer Science. Content developed by Dartford Grammar School Computer Science Department

Abstract Data Structures IB Computer Science. Content developed by Dartford Grammar School Computer Science Department Abstract Data Structures IB Computer Science Content developed by Dartford Grammar School Computer Science Department HL Topics 1-7, D1-4 1: System design 2: Computer Organisation 3: Networks 4: Computational

More information

Trees. Estruturas de Dados / Programação 2 Árvores. Márcio Ribeiro twitter.com/marciomribeiro. Introduc)on. Hierarchical structure

Trees. Estruturas de Dados / Programação 2 Árvores. Márcio Ribeiro twitter.com/marciomribeiro. Introduc)on. Hierarchical structure Introduc)on Linear structures Removing this idea, treasure of applicaons Estruturas de Dados / Programação 2 Árvores Márcio Ribeiro marcio@ic.ufal.br twitter.com/marciomribeiro Hierarchical structure Companies

More information

Tree: non-recursive definition. Trees, Binary Search Trees, and Heaps. Tree: recursive definition. Tree: example.

Tree: non-recursive definition. Trees, Binary Search Trees, and Heaps. Tree: recursive definition. Tree: example. Trees, Binary Search Trees, and Heaps CS 5301 Fall 2013 Jill Seaman Tree: non-recursive definition Tree: set of nodes and directed edges - root: one node is distinguished as the root - Every node (except

More information

Trees : Part 1. Section 4.1. Theory and Terminology. A Tree? A Tree? Theory and Terminology. Theory and Terminology

Trees : Part 1. Section 4.1. Theory and Terminology. A Tree? A Tree? Theory and Terminology. Theory and Terminology Trees : Part Section. () (2) Preorder, Postorder and Levelorder Traversals Definition: A tree is a connected graph with no cycles Consequences: Between any two vertices, there is exactly one unique path

More information

Tree Structures. Definitions: o A tree is a connected acyclic graph. o A disconnected acyclic graph is called a forest

Tree Structures. Definitions: o A tree is a connected acyclic graph. o A disconnected acyclic graph is called a forest Tree Structures Definitions: o A tree is a connected acyclic graph. o A disconnected acyclic graph is called a forest o A tree is a connected digraph with these properties: There is exactly one node (Root)

More information

We have the pointers reference the next node in an inorder traversal; called threads

We have the pointers reference the next node in an inorder traversal; called threads Leaning Objective: In this Module you will be learning the following: Threaded Binary Tree Introduction: Threaded Binary Tree is also a binary tree in which all left child pointers that are NULL (in Linked

More information

Sorted Arrays. Operation Access Search Selection Predecessor Successor Output (print) Insert Delete Extract-Min

Sorted Arrays. Operation Access Search Selection Predecessor Successor Output (print) Insert Delete Extract-Min Binary Search Trees FRIDAY ALGORITHMS Sorted Arrays Operation Access Search Selection Predecessor Successor Output (print) Insert Delete Extract-Min 6 10 11 17 2 0 6 Running Time O(1) O(lg n) O(1) O(1)

More information

Chapter Contents. Trees. Tree Concepts. Hierarchical Organization. Hierarchical Organization. Hierarchical Organization.

Chapter Contents. Trees. Tree Concepts. Hierarchical Organization. Hierarchical Organization. Hierarchical Organization. Chapter Contents Chapter 18 Tree Concepts Hierarchical Organizations Tree Terminology Traversals of a Tree Traversals of a Binary Tree Traversals of a General Tree Java Interfaces for Interfaces for All

More information

CSC148 Week 6. Larry Zhang

CSC148 Week 6. Larry Zhang CSC148 Week 6 Larry Zhang 1 Announcements Test 1 coverage: trees (topic of today and Wednesday) are not covered Assignment 1 slides posted on the course website. 2 Data Structures 3 Data Structures A data

More information

Data Structure - Binary Tree 1 -

Data Structure - Binary Tree 1 - Data Structure - Binary Tree 1 - Hanyang University Jong-Il Park Basic Tree Concepts Logical structures Chap. 2~4 Chap. 5 Chap. 6 Linear list Tree Graph Linear structures Non-linear structures Linear Lists

More information

Algorithms. Deleting from Red-Black Trees B-Trees

Algorithms. Deleting from Red-Black Trees B-Trees Algorithms Deleting from Red-Black Trees B-Trees Recall the rules for BST deletion 1. If vertex to be deleted is a leaf, just delete it. 2. If vertex to be deleted has just one child, replace it with that

More information

Associate Professor Dr. Raed Ibraheem Hamed

Associate Professor Dr. Raed Ibraheem Hamed Associate Professor Dr. Raed Ibraheem Hamed University of Human Development, College of Science and Technology Computer Science Department 2015 2016 Department of Computer Science _ UHD 1 What this Lecture

More information

Spring 2018 Mentoring 8: March 14, Binary Trees

Spring 2018 Mentoring 8: March 14, Binary Trees CSM 6B Binary Trees Spring 08 Mentoring 8: March 4, 08 Binary Trees. Define a procedure, height, which takes in a Node and outputs the height of the tree. Recall that the height of a leaf node is 0. private

More information

March 20/2003 Jayakanth Srinivasan,

March 20/2003 Jayakanth Srinivasan, Definition : A simple graph G = (V, E) consists of V, a nonempty set of vertices, and E, a set of unordered pairs of distinct elements of V called edges. Definition : In a multigraph G = (V, E) two or

More information

Discussion 2C Notes (Week 8, February 25) TA: Brian Choi Section Webpage:

Discussion 2C Notes (Week 8, February 25) TA: Brian Choi Section Webpage: Discussion 2C Notes (Week 8, February 25) TA: Brian Choi (schoi@cs.ucla.edu) Section Webpage: http://www.cs.ucla.edu/~schoi/cs32 Trees Definitions Yet another data structure -- trees. Just like a linked

More information

CSI33 Data Structures

CSI33 Data Structures Outline Department of Mathematics and Computer Science Bronx Community College November 13, 2017 Outline Outline 1 C++ Supplement.1: Trees Outline C++ Supplement.1: Trees 1 C++ Supplement.1: Trees Uses

More information

CS302 - Data Structures using C++

CS302 - Data Structures using C++ CS302 - Data Structures using C++ Topic: Trees Kostas Alexis Trees List, stacks, and queues are linear in their organization of data. Items are one after another In this section, we organize data in a

More information

Algorithms and Data Structures (INF1) Lecture 8/15 Hua Lu

Algorithms and Data Structures (INF1) Lecture 8/15 Hua Lu Algorithms and Data Structures (INF1) Lecture 8/15 Hua Lu Department of Computer Science Aalborg University Fall 2007 This Lecture Trees Basics Rooted trees Binary trees Binary tree ADT Tree traversal

More information

BINARY TREES, THE SEARCH TREE ADT BINARY SEARCH TREES, RED BLACK TREES, TREE TRAVERSALS, B TREES WEEK - 6

BINARY TREES, THE SEARCH TREE ADT BINARY SEARCH TREES, RED BLACK TREES, TREE TRAVERSALS, B TREES WEEK - 6 Ashish Jamuda Week 6 CS 331 DATA STRUCTURES & ALGORITHMS BINARY TREES, THE SEARCH TREE ADT BINARY SEARCH TREES, RED BLACK TREES, TREE TRAVERSALS, B TREES OBJECTIVES: Binary Trees Binary Search Trees Tree

More information

Stacks, Queues and Hierarchical Collections

Stacks, Queues and Hierarchical Collections Programming III Stacks, Queues and Hierarchical Collections 2501ICT Nathan Contents Linked Data Structures Revisited Stacks Queues Trees Binary Trees Generic Trees Implementations 2 Copyright 2002- by

More information

Section 5.5. Left subtree The left subtree of a vertex V on a binary tree is the graph formed by the left child L of V, the descendents

Section 5.5. Left subtree The left subtree of a vertex V on a binary tree is the graph formed by the left child L of V, the descendents Section 5.5 Binary Tree A binary tree is a rooted tree in which each vertex has at most two children and each child is designated as being a left child or a right child. Thus, in a binary tree, each vertex

More information

8. Binary Search Tree

8. Binary Search Tree 8 Binary Search Tree Searching Basic Search Sequential Search : Unordered Lists Binary Search : Ordered Lists Tree Search Binary Search Tree Balanced Search Trees (Skipped) Sequential Search int Seq-Search

More information

Lecture 32. No computer use today. Reminders: Homework 11 is due today. Project 6 is due next Friday. Questions?

Lecture 32. No computer use today. Reminders: Homework 11 is due today. Project 6 is due next Friday. Questions? Lecture 32 No computer use today. Reminders: Homework 11 is due today. Project 6 is due next Friday. Questions? Friday, April 1 CS 215 Fundamentals of Programming II - Lecture 32 1 Outline Introduction

More information

INF2220: algorithms and data structures Series 1

INF2220: algorithms and data structures Series 1 Universitetet i Oslo Institutt for Informatikk I. Yu, D. Karabeg INF2220: algorithms and data structures Series 1 Topic Function growth & estimation of running time, trees Issued: 24. 08. 2016 Exercise

More information

Topic Binary Trees (Non-Linear Data Structures)

Topic Binary Trees (Non-Linear Data Structures) Topic Binary Trees (Non-Linear Data Structures) CIS210 1 Linear Data Structures Arrays Linked lists Skip lists Self-organizing lists CIS210 2 Non-Linear Data Structures Hierarchical representation? Trees

More information

Multi-Way Search Tree

Multi-Way Search Tree Multi-Way Search Tree A multi-way search tree is an ordered tree such that Each internal node has at least two and at most d children and stores d -1 data items (k i, D i ) Rule: Number of children = 1

More information

CS 231 Data Structures and Algorithms Fall Recursion and Binary Trees Lecture 21 October 24, Prof. Zadia Codabux

CS 231 Data Structures and Algorithms Fall Recursion and Binary Trees Lecture 21 October 24, Prof. Zadia Codabux CS 231 Data Structures and Algorithms Fall 2018 Recursion and Binary Trees Lecture 21 October 24, 2018 Prof. Zadia Codabux 1 Agenda ArrayQueue.java Recursion Binary Tree Terminologies Traversal 2 Administrative

More information

CS 151. Binary Trees. Friday, October 5, 12

CS 151. Binary Trees. Friday, October 5, 12 CS 151 Binary Trees 1 Binary Tree Examples Without telling you what a binary tree is, here are some examples (that I will draw on the board): The dots/circles are called nodes, or vertices (singular: one

More information

CS350: Data Structures Binary Search Trees

CS350: Data Structures Binary Search Trees Binary Search Trees James Moscola Department of Engineering & Computer Science York College of Pennsylvania James Moscola Introduction to Binary Search Trees A binary search tree is a binary tree that

More information

Trees! Ellen Walker! CPSC 201 Data Structures! Hiram College!

Trees! Ellen Walker! CPSC 201 Data Structures! Hiram College! Trees! Ellen Walker! CPSC 201 Data Structures! Hiram College! ADTʼs Weʼve Studied! Position-oriented ADT! List! Stack! Queue! Value-oriented ADT! Sorted list! All of these are linear! One previous item;

More information

Tree Data Structures CSC 221

Tree Data Structures CSC 221 Tree Data Structures CSC 221 Specialized Trees Binary Tree: A restriction of trees such that the maximum degree of a node is 2. Order of nodes is now relevant May have zero nodes (emtpy tree) Formal Definition:

More information

CS24 Week 8 Lecture 1

CS24 Week 8 Lecture 1 CS24 Week 8 Lecture 1 Kyle Dewey Overview Tree terminology Tree traversals Implementation (if time) Terminology Node The most basic component of a tree - the squares Edge The connections between nodes

More information

Topic 18 Binary Trees "A tree may grow a thousand feet tall, but its leaves will return to its roots." -Chinese Proverb

Topic 18 Binary Trees A tree may grow a thousand feet tall, but its leaves will return to its roots. -Chinese Proverb Topic 18 "A tree may grow a thousand feet tall, but its leaves will return to its roots." -Chinese Proverb Definitions A tree is an abstract data type one entry point, the root Each node is either a leaf

More information

BST Implementation. Data Structures. Lecture 4 Binary search trees (BST) Dr. Mahmoud Attia Sakr University of Ain Shams

BST Implementation. Data Structures. Lecture 4 Binary search trees (BST) Dr. Mahmoud Attia Sakr University of Ain Shams Lecture 4 Binary search trees (BST) Dr. Mahmoud Attia Sakr mahmoud.sakr@cis.asu.edu.eg Cairo, Egypt, October 2012 Binary Search Trees (BST) 1. Hierarchical data structure with a single reference to root

More information

! Tree: set of nodes and directed edges. ! Parent: source node of directed edge. ! Child: terminal node of directed edge

! Tree: set of nodes and directed edges. ! Parent: source node of directed edge. ! Child: terminal node of directed edge Trees (& Heaps) Week 12 Gaddis: 20 Weiss: 21.1-3 CS 5301 Spring 2015 Jill Seaman 1 Tree: non-recursive definition! Tree: set of nodes and directed edges - root: one node is distinguished as the root -

More information

TREES cs2420 Introduction to Algorithms and Data Structures Spring 2015

TREES cs2420 Introduction to Algorithms and Data Structures Spring 2015 TREES cs2420 Introduction to Algorithms and Data Structures Spring 2015 1 administrivia 2 -assignment 7 due Thursday at midnight -asking for regrades through assignment 5 and midterm must be complete by

More information

Chapter 5. Binary Trees

Chapter 5. Binary Trees Chapter 5 Binary Trees Definitions and Properties A binary tree is made up of a finite set of elements called nodes It consists of a root and two subtrees There is an edge from the root to its children

More information

CSCI212 Computer Science. Binary Trees/Heaps/Binary Search Trees

CSCI212 Computer Science. Binary Trees/Heaps/Binary Search Trees CSCI212 Computer Science Binary Trees/Heaps/Binary Search Trees Tree Terminology 0 A tree is a non-linear abstract data type that stores elements hierarchically. 0 With the exception of the top element

More information

Trees. CSE 373 Data Structures

Trees. CSE 373 Data Structures Trees CSE 373 Data Structures Readings Reading Chapter 7 Trees 2 Why Do We Need Trees? Lists, Stacks, and Queues are linear relationships Information often contains hierarchical relationships File directories

More information

Tree. A path is a connected sequence of edges. A tree topology is acyclic there is no loop.

Tree. A path is a connected sequence of edges. A tree topology is acyclic there is no loop. Tree A tree consists of a set of nodes and a set of edges connecting pairs of nodes. A tree has the property that there is exactly one path (no more, no less) between any pair of nodes. A path is a connected

More information

CS61B Lecture #20: Trees. Last modified: Mon Oct 8 21:21: CS61B: Lecture #20 1

CS61B Lecture #20: Trees. Last modified: Mon Oct 8 21:21: CS61B: Lecture #20 1 CS61B Lecture #20: Trees Last modified: Mon Oct 8 21:21:22 2018 CS61B: Lecture #20 1 A Recursive Structure Trees naturally represent recursively defined, hierarchical objects with more than one recursive

More information

Garbage Collection: recycling unused memory

Garbage Collection: recycling unused memory Outline backtracking garbage collection trees binary search trees tree traversal binary search tree algorithms: add, remove, traverse binary node class 1 Backtracking finding a path through a maze is an

More information

Stacks, Queues and Hierarchical Collections. 2501ICT Logan

Stacks, Queues and Hierarchical Collections. 2501ICT Logan Stacks, Queues and Hierarchical Collections 2501ICT Logan Contents Linked Data Structures Revisited Stacks Queues Trees Binary Trees Generic Trees Implementations 2 Queues and Stacks Queues and Stacks

More information

TREES. Tree Overview 9/28/16. Prelim 1 tonight! Important Announcements. Tree terminology. Binary trees were in A1!

TREES. Tree Overview 9/28/16. Prelim 1 tonight! Important Announcements. Tree terminology. Binary trees were in A1! //16 Prelim 1 tonight! :3 prelim is very crowded. You HAVE to follow these directions: 1. Students taking the normal :3 prelim (not the quiet room) and whose last names begin with A through Da MUST go

More information

Data Structures and Algorithms

Data Structures and Algorithms Data Structures and Algorithms CS245-2017S-06 Binary Search Trees David Galles Department of Computer Science University of San Francisco 06-0: Ordered List ADT Operations: Insert an element in the list

More information

binary tree empty root subtrees Node Children Edge Parent Ancestor Descendant Path Depth Height Level Leaf Node Internal Node Subtree

binary tree empty root subtrees Node Children Edge Parent Ancestor Descendant Path Depth Height Level Leaf Node Internal Node Subtree 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 called the left and right subtrees which are disjoint

More information

Announcements. Midterm exam 2, Thursday, May 18. Today s topic: Binary trees (Ch. 8) Next topic: Priority queues and heaps. Break around 11:45am

Announcements. Midterm exam 2, Thursday, May 18. Today s topic: Binary trees (Ch. 8) Next topic: Priority queues and heaps. Break around 11:45am Announcements Midterm exam 2, Thursday, May 18 Closed book/notes but one sheet of paper allowed Covers up to stacks and queues Today s topic: Binary trees (Ch. 8) Next topic: Priority queues and heaps

More information

8. Write an example for expression tree. [A/M 10] (A+B)*((C-D)/(E^F))

8. Write an example for expression tree. [A/M 10] (A+B)*((C-D)/(E^F)) DHANALAKSHMI COLLEGE OF ENGINEERING DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING EC6301 OBJECT ORIENTED PROGRAMMING AND DATA STRUCTURES UNIT IV NONLINEAR DATA STRUCTURES Part A 1. Define Tree [N/D 08]

More information

CS102 Binary Search Trees

CS102 Binary Search Trees CS102 Binary Search Trees Prof Tejada 1 To speed up insertion, removal and search, modify the idea of a Binary Tree to create a Binary Search Tree (BST) Binary Search Trees Binary Search Trees have one

More information

The tree data structure. Trees COL 106. Amit Kumar Shweta Agrawal. Acknowledgement :Many slides are courtesy Douglas Harder, UWaterloo

The tree data structure. Trees COL 106. Amit Kumar Shweta Agrawal. Acknowledgement :Many slides are courtesy Douglas Harder, UWaterloo The tree data structure 1 Trees COL 106 Amit Kumar Shweta Agrawal Acknowledgement :Many slides are courtesy Douglas Harder, UWaterloo 1 Trees The tree data structure 3 A rooted tree data structure stores

More information

Computer Science 210 Data Structures Siena College Fall Topic Notes: Trees

Computer Science 210 Data Structures Siena College Fall Topic Notes: Trees Computer Science 0 Data Structures Siena College Fall 08 Topic Notes: Trees We ve spent a lot of time looking at a variety of structures where there is a natural linear ordering of the elements in arrays,

More information

CSCI-401 Examlet #5. Name: Class: Date: True/False Indicate whether the sentence or statement is true or false.

CSCI-401 Examlet #5. Name: Class: Date: True/False Indicate whether the sentence or statement is true or false. Name: Class: Date: CSCI-401 Examlet #5 True/False Indicate whether the sentence or statement is true or false. 1. The root node of the standard binary tree can be drawn anywhere in the tree diagram. 2.

More information

9. Binary Search Trees

9. Binary Search Trees Trees. Binary Search Trees [Ottman/Widmayer, Kap..1, Cormen et al, Kap. 12.1-12.] Trees are Generalized lists: nodes can have more than one successor Special graphs: graphs consist of nodes and edges.

More information

Binary Search Trees. BinaryTree<E> Class (cont.) Section /27/2017

Binary Search Trees. BinaryTree<E> Class (cont.) Section /27/2017 Binary Search Trees Section.4 BinaryTree Class (cont.) public class BinaryTree { // Data members/fields...just the root is needed - Node root; // Constructor(s) + BinaryTree() + BinaryTree(Node

More information

Programming II (CS300)

Programming II (CS300) 1 Programming II (CS300) Chapter 12: Heaps and Priority Queues MOUNA KACEM mouna@cs.wisc.edu Fall 2018 Heaps and Priority Queues 2 Priority Queues Heaps Priority Queue 3 QueueADT Objects are added and

More information

Data Structures and Algorithms for Engineers

Data Structures and Algorithms for Engineers 04-630 Data Structures and Algorithms for Engineers David Vernon Carnegie Mellon University Africa vernon@cmu.edu www.vernon.eu Data Structures and Algorithms for Engineers 1 Carnegie Mellon University

More information

Data Structure Lecture#10: Binary Trees (Chapter 5) U Kang Seoul National University

Data Structure Lecture#10: Binary Trees (Chapter 5) U Kang Seoul National University Data Structure Lecture#10: Binary Trees (Chapter 5) U Kang Seoul National University U Kang (2016) 1 In This Lecture The concept of binary tree, its terms, and its operations Full binary tree theorem Idea

More information

Binary search trees (BST) Binary search trees (BST)

Binary search trees (BST) Binary search trees (BST) Tree A tree is a structure that represents a parent-child relation on a set of object. An element of a tree is called a node or vertex. The root of a tree is the unique node that does not have a parent

More information

Data Structures and Algorithms

Data Structures and Algorithms Data Structures and Algorithms Trees Sidra Malik sidra.malik@ciitlahore.edu.pk Tree? In computer science, a tree is an abstract model of a hierarchical structure A tree is a finite set of one or more nodes

More information

TREES Lecture 12 CS2110 Spring 2018

TREES Lecture 12 CS2110 Spring 2018 TREES Lecture 12 CS2110 Spring 2018 Important Announcements 2 A4 is out now and due two weeks from today. Have fun, and start early! Data Structures 3 There are different ways of storing data, called data

More information

CS 350 : Data Structures Binary Search Trees

CS 350 : Data Structures Binary Search Trees CS 350 : Data Structures Binary Search Trees David Babcock (courtesy of James Moscola) Department of Physical Sciences York College of Pennsylvania James Moscola Introduction to Binary Search Trees A binary

More information

Binary Trees and Binary Search Trees

Binary Trees and Binary Search Trees Binary Trees and Binary Search Trees Learning Goals After this unit, you should be able to... Determine if a given tree is an instance of a particular type (e.g. binary, and later heap, etc.) Describe

More information

Hierarchical data structures. Announcements. Motivation for trees. Tree overview

Hierarchical data structures. Announcements. Motivation for trees. Tree overview Announcements Midterm exam 2, Thursday, May 18 Closed book/notes but one sheet of paper allowed Covers up to stacks and queues Today s topic: Binary trees (Ch. 8) Next topic: Priority queues and heaps

More information

CE 221 Data Structures and Algorithms

CE 221 Data Structures and Algorithms CE 221 Data Structures and Algorithms Chapter 4: Trees (Binary) Text: Read Weiss, 4.1 4.2 Izmir University of Economics 1 Preliminaries - I (Recursive) Definition: A tree is a collection of nodes. The

More information