Data Structures - CSCI 102 Binary Tree In binary trees, each Node can point to two other Nodes and looks something like this: template class BTNode { public:

Slides:



Advertisements
Similar presentations
Chapter 12 Binary Search Trees
Advertisements

Comp 122, Spring 2004 Binary Search Trees. btrees - 2 Comp 122, Spring 2004 Binary Trees  Recursive definition 1.An empty tree is a binary tree 2.A node.
Trees Types and Operations
CS 332: Algorithms Binary Search Trees. Review: Dynamic Sets ● Next few lectures will focus on data structures rather than straight algorithms ● In particular,
ALGORITHMS THIRD YEAR BANHA UNIVERSITY FACULTY OF COMPUTERS AND INFORMATIC Lecture six Dr. Hamdy M. Mousa.
Binary Search Trees Azhar Maqsood School of Electrical Engineering and Computer Sciences (SEECS-NUST)
David Luebke 1 5/4/2015 Binary Search Trees. David Luebke 2 5/4/2015 Dynamic Sets ● Want a data structure for dynamic sets ■ Elements have a key and satellite.
Binary Trees Chapter 6. Linked Lists Suck By now you realize that the title to this slide is true… By now you realize that the title to this slide is.
Binary Trees, Binary Search Trees CMPS 2133 Spring 2008.
Binary Trees, Binary Search Trees COMP171 Fall 2006.
CS 171: Introduction to Computer Science II
Department of Computer Science University of Maryland, College Park
1 Trees. 2 Outline –Tree Structures –Tree Node Level and Path Length –Binary Tree Definition –Binary Tree Nodes –Binary Search Trees.
1.1 Data Structure and Algorithm Lecture 12 Binary Search Trees Topics Reference: Introduction to Algorithm by Cormen Chapter 13: Binary Search Trees.
Binary Search Trees Chapter 7 Objectives
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.
CSE373: Data Structures & Algorithms Lecture 6: Binary Search Trees Nicki Dell Spring 2014 CSE373: Data Structures & Algorithms1.
CSE373: Data Structures & Algorithms Lecture 6: Binary Search Trees Lauren Milne Summer
Chapter 12. Binary Search Trees. Search Trees Data structures that support many dynamic-set operations. Can be used both as a dictionary and as a priority.
Binary Trees, Binary Search Trees RIZWAN REHMAN CENTRE FOR COMPUTER STUDIES DIBRUGARH UNIVERSITY.
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.
Binary SearchTrees [CLRS] – Chap 12. What is a binary tree ? A binary tree is a linked data structure in which each node is an object that contains following.
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.
CS 206 Introduction to Computer Science II 10 / 05 / 2009 Instructor: Michael Eckmann.
Binary Search Tree Qamar Abbas.
 Trees Data Structures Trees Data Structures  Trees Trees  Binary Search Trees Binary Search Trees  Binary Tree Implementation Binary Tree Implementation.
Data Structures Haim Kaplan and Uri Zwick November 2012 Lecture 3 Dynamic Sets / Dictionaries Binary Search Trees.
CS 206 Introduction to Computer Science II 02 / 13 / 2009 Instructor: Michael Eckmann.
Lec 15 Oct 18 Binary Search Trees (Chapter 5 of text)
Preview  Graph  Tree Binary Tree Binary Search Tree Binary Search Tree Property Binary Search Tree functions  In-order walk  Pre-order walk  Post-order.
Lecture 9 Algorithm Analysis Arne Kutzner Hanyang University / Seoul Korea.
1 Algorithms CSCI 235, Fall 2015 Lecture 22 Binary Search Trees.
David Stotts Computer Science Department UNC Chapel Hill.
Binary Search Trees Lecture 5 1. Binary search tree sort 2.
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.
CISC 235 Topic 3 General Trees, Binary Trees, Binary Search Trees.
Binary Tree. Some Terminologies Short review on binary tree Tree traversals Binary Search Tree (BST)‏ Questions.
ADT Binary Search Tree Ellen Walker CPSC 201 Data Structures Hiram College.
Binary Search Trees (BST)
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 19. Binary Search Tree 1. Recap Tree is a non linear data structure to present data in hierarchical form. It is also called acyclic data structure.
1 Joe Meehean. A A B B D D I I C C E E X X A A B B D D I I C C E E X X  Terminology each circle is a node pointers are edges topmost node is the root.
CSE 2331/5331 Topic 8: Binary Search Tree Data structure Operations.
BINARY TREES Objectives Define trees as data structures Define the terms associated with trees Discuss tree traversal algorithms Discuss a binary.
CSE373: Data Structures & Algorithms Lecture 6: Binary Search Trees Linda Shapiro Winter 2015.
CS6045: Advanced Algorithms Data Structures. Dynamic Sets Next few lectures will focus on data structures rather than straight algorithms In particular,
Binary Search Trees Chapter 7 Objectives
Binary Search Trees What is a binary search tree?
Instructor: Lilian de Greef Quarter: Summer 2017
Binary Search Trees.
UNIT III TREES.
Trees.
Data Structures & Algorithm Design
CS200: Algorithms Analysis
Lecture 7 Algorithm Analysis
Binary Trees, Binary Search Trees
CSE373: Data Structures & Algorithms Lecture 6: Binary Search Trees
CSE373: Data Structures & Algorithms Lecture 6: Binary Search Trees
Lecture 7 Algorithm Analysis
Chapter 12: Binary Search Trees
Binary Search Trees.
CS6045: Advanced Algorithms
Lecture 7 Algorithm Analysis
Binary Trees, Binary Search Trees
Topic 6: Binary Search Tree Data structure Operations
Chapter 20: Binary Trees.
Mark Redekopp David Kempe
Trees.
Binary Trees, Binary Search Trees
Presentation transcript:

Data Structures - CSCI 102 Binary Tree In binary trees, each Node can point to two other Nodes and looks something like this: template class BTNode { public: //The data section BTNode data T data; left right //The links BTNode * left; BTNode * right; }; 6 Copyright © William C. Cheng

7 Copyright © William C. Cheng G D H E I B Data Structures - CSCI 102 Binary Tree A C F

Trees grown downward in computer science 8 Data Structures - CSCI 102 Copyright © William C. Cheng Binary Tree Terminology Each BTNode has 0, 1 or 2 Child Nodes that are below it in the tree A BTNode that has a child is called that child’s Parent Node Parent nodes point to child nodes via a branch (child nodes don’t need to point to parents, but they can if they want)

9 Copyright © William C. Cheng Data Structures - CSCI 102 Binary Trees A is the Parent of B and C B is the Left Child of A C is the Right Child of A A Branch B Branch C

The Root Node is the only BTNode in the tree that has no parent node 10 Data Structures - CSCI 102 Copyright © William C. Cheng Binary Tree Terminology A BTNode is a Leaf Node if it has no children The Level of a BTNode in the tree is the number of branches from the root to the node The Height of the tree is the number of nodes on the longest path from root to leaf

11 Copyright © William C. Cheng Data Structures - CSCI 102 Binary Trees B A C DE A is the Root Node C, D, E are Leaf Nodes Level(A) = 0 Level(B) = 1 Level(D) = 2 The Height of the tree is 3

What kind of operations do we need to perform on a binary tree? 12 Data Structures - CSCI 102 Copyright © William C. Cheng Binary Trees height() size() search() insert() delete() print() NOTE: Pretty much every operation requires a way to traverse the tree

14 Copyright © William C. Cheng E HI D G BC F Data Structures - CSCI 102 Binary Tree Height If you had to calculate the height of a binary tree, how would you do it? Root A

15 Copyright © William C. Cheng E HI D G BC F Data Structures - CSCI 102 Binary Tree Height Height = 1 + max(Height(Root->left),Height(Root->right)) Root A Height of Root->left Root->right 1 Height of 1

18 Copyright © William C. Cheng E HI D G BC F Data Structures - CSCI 102 Binary Tree Traversal If you had to print out all the nodes in a binary tree, how would you do it? Root A

If you had to print out all the nodes in a binary tree, how would you do it? 19 Data Structures - CSCI 102 Copyright © William C. Cheng Binary Tree Traversal At each node in the tree, you have three choices: These choices leads to different kinds of traversals Print the current node Go to the left child Go to the right child In Order: print left, print current, print right Pre Order: print current, print left, print right Post Order: print left, print right, print current

20 Copyright © William C. Cheng Data Structures - CSCI 102 Binary Tree Traversal In Order Traversal: Traverse A’s left subtree Print A Traverse A’s right subtree Order of output: GDBHEIACF E I D G BHBH A C F Root

Pre Order Traversal: Print A Traverse A’s left subtree Traverse A’s right subtree Order of output: ABDGEHICF E I D G BHBH A C F Root 22 Copyright © William C. Cheng Data Structures - CSCI 102 Binary Tree Traversal

Post Order Traversal: Traverse A’s left subtree Traverse A’s right subtree Print A Order of output: GDHIEBFCA E I D G BHBH A C F Root 24 Copyright © William C. Cheng Data Structures - CSCI 102 Binary Tree Traversal

26 Copyright © William C. Cheng E HI D G BC F Data Structures - CSCI 102 Binary Tree Search If you had to find a particular value in a binary tree, how would you do it? Root A

Search still requires us to potentially look at every single node in the tree Still O(n) So far binary trees are interesting, but they don’t provide any extra benefit 27 Data Structures - CSCI 102 Copyright © William C. Cheng Binary Tree Issues If you’re going to insert a new Node, where should you put it in the tree? What’s missing here is some extra organization that makes it easier to find things

template class BTNode { public: T key;//The key data U data; //The data left right //The links BTNode * left; BTNode * right; }; 8 Copyright © William C. Cheng Data Structures - CSCI 102 Binary Tree BTNode can also have a separate key and data key is used to order the nodes in the tree data actually holds the stuff we care about BTNode key

To speed up insertion, removal and search, modify the idea of a Binary Tree to create a Binary Search Tree (BST) 9 Data Structures - CSCI 102 Copyright © William C. Cheng Binary Search Trees Binary Search Trees have one major organizational property (the binary search tree property): Let X be a Node in a binary search tree Every Node X is comparable to every other Node via a field we call the key (for now, "data" is the key) If Y is a Node in the left subtree of X: key[Y] <= key[X] key[Y] > key[X] If keys must be unique, then we have key[Y] < key[X] You can implement your BST to enforce this We will assume that this is the case here If Y is a Node in the right subtree of X:

11 Copyright © William C. Cheng Data Structures - CSCI 102 Binary Search Tree Search If you had to find a particular value in a binary search tree, how would you do it? Ex: find Root

If you had to find a particular value in a binary search tree, how would you do it? 12 Copyright © William C. Cheng Data Structures - CSCI 102 Binary Search Tree Search Root Ex: find 58 Start at root 58 == 60? No 58 < 60? Yes, go left Current

13 Copyright © William C. Cheng Data Structures - CSCI 102 Binary Search Tree Search Root If you had to find a particular value in a binary search tree, how would you do it? Ex: find 58 Start at root 58 == 60? No 58 < 60? Yes, go left Current 58 == 50? No 58 < 50? No 58 > 50? Yes, go right

14 Copyright © William C. Cheng Data Structures - CSCI 102 Binary Search Tree Search Root If you had to find a particular value in a binary search tree, how would you do it? Ex: find 58 Start at root 58 == 60? No 58 < 60? Yes, go left 58 == 50? No 58 < 50? No Current 58 > 50? Yes, go right 58 == 58? Yes

Start at the root What will our search algorithm be? If the key we want is less than the key of the current node, search the left subtree 15 Data Structures - CSCI 102 Copyright © William C. Cheng Binary Search Tree Search Is the current node what we want? Otherwise, search the right subtree If yes, we’re done If no... Should our solution be recursive or iterative?

18 Copyright © William C. Cheng Data Structures - CSCI 102 Binary Search Tree Min/Max How would you find the minimum/maximum values in a binary search tree? Root 60

Start at the root What will our findMin algorithm be? 19 Data Structures - CSCI 102 Copyright © William C. Cheng Binary Search Tree Min/Max Does the current Node have a left child? If yes, go to the left child If no, we’re at the minimum Start at the root What will our findMax algorithm be? Does the current Node have a right child? If yes, go to the right child If no, we’re at the maximum

Order of output: discussed? In Order Traversal Visits all the nodes in the tree in order 21 Copyright © William C. Cheng Data Structures - CSCI 102 Binary Search Tree Traversal Now that we have an implicit ordering to our tree, what happens with the traversal algorithms we previously Root

22 Data Structures - CSCI 102 Copyright © William C. Cheng Binary Search Tree Traversal Traverse the binary search tree with an in-order traversal Tree Sort Given an unordered list of elements, add them all to a binary search tree All the nodes come back out sorted!

Binary Search Tree Insertion 24 Data Structures - CSCI 102 Copyright © William C. Cheng Binary Search Tree Insertion New nodes are always added in the place of an existing NULL...they never reposition other nodes 1) Create the new node with its data and set both of its pointers to NULL 2) If the tree is empty, make the new node the root 3) Walk down the tree node by node like you were doing a search If new value is less than current node, move to left subtree, otherwise move to right subtree Potentially reject duplicates

25 Copyright © William C. Cheng Data Structures - CSCI 102 Binary Search Tree Insertion How do you insert a new value into a binary search tree? Root Ex: insert 59

Make new node 26 Copyright © William C. Cheng Data Structures - CSCI 102 Binary Search Tree Insertion How do you insert a new value into a binary search tree? Root 59 New Node

27 Copyright © William C. Cheng Data Structures - CSCI 102 Binary Search Tree Insertion How do you insert a new value into a binary search tree? Root 59 New Node Current Ex: insert 59 Make new node Start at root 59 < 60; go left

59 > 50; go right 28 Copyright © William C. Cheng Data Structures - CSCI 102 Binary Search Tree Insertion How do you insert a new value into a binary search tree? Root 59 New Node Current Ex: insert 59 Make new node Start at root 59 < 60; go left

29 Copyright © William C. Cheng Data Structures - CSCI 102 Binary Search Tree Insertion How do you insert a new value into a binary search tree? Root 59 New Node Current 58 Ex: insert 59 Make new node Start at root 59 < 60; go left 59 > 50; go right 59 > 58; go right

30 Copyright © William C. Cheng Data Structures - CSCI 102 Binary Search Tree Insertion How do you insert a new value into a binary search tree? Root 59 Ex: insert 59 Make new node Start at root 59 < 60; go left 59 > 50; go right 59 > 58; go right Right is NULL Insert new Node

31 Copyright © William C. Cheng Data Structures - CSCI 102 Binary Search Tree Insertion How do you insert a new value into a binary search tree? Root 59 Ex: insert 59 Make new node Start at root 59 < 60; go left 59 > 50; go right 59 > 58; go right Right is NULL Insert new Node

Binary Search Tree Removal 32 Data Structures - CSCI 102 Copyright © William C. Cheng Binary Search Tree Remove Search the tree for the node that you want to remove When node is found, removal breaks down into 3 distinct cases 1) Remove a node with no children 2) Remove a node with one child 3) Remove a node with two children

Find the node and its parent Remove a node with no children (case #1) 33 Data Structures - CSCI 102 Copyright © William C. Cheng Binary Search Tree Remove Set the parent pointer to the node to NULL Delete the node

Case #1 Ex: remove Copyright © William C. Cheng Data Structures - CSCI 102 Binary Search Tree Remove How do you remove a value from a binary search tree? Root

35 Copyright © William C. Cheng Data Structures - CSCI 102 Binary Search Tree Remove How do you remove a value from a binary search tree? Root To remove Case #1 Ex: remove 46 Search for the value

36 Copyright © William C. Cheng Data Structures - CSCI 102 Binary Search Tree Remove How do you remove a value from a binary search tree? Root To remove Case #1 Ex: remove 46 Search for the value Set parent’s pointer to NULL

37 Copyright © William C. Cheng Data Structures - CSCI 102 Binary Search Tree Remove How do you remove a value from a binary search tree? Root To remove Case #1 Ex: remove 46 Search for the value Set parent’s pointer to NULL Delete the child

Data Structures - CSCI 102 Binary Search Tree Remove How do you remove a value from a binary search tree? Copyright © William C. Cheng Root Case #1 Ex: remove 46 Search for the value Set parent’s pointer to NULL Delete the child

Find the node and its parent Remove a node with one child (case #2) Set the parent pointer to the node’s only child Delete the node 39 Data Structures - CSCI 102 Copyright © William C. Cheng Binary Search Tree Remove

Root 40 Copyright © William C. Cheng Data Structures - CSCI 102 Binary Search Tree Remove How do you remove a value from a binary search tree? Case #2 Ex: remove 30

Root 41 Copyright © William C. Cheng Data Structures - CSCI 102 Binary Search Tree Remove How do you remove a value from a binary search tree? To remove Case #2 Ex: remove 30 Search for the value

Root 42 Copyright © William C. Cheng Data Structures - CSCI 102 Binary Search Tree Remove How do you remove a value from a binary search tree? To remove Case #2 Ex: remove 30 Search for the value Pointer value’s parent to value’s only child

Root 43 Copyright © William C. Cheng Data Structures - CSCI 102 Binary Search Tree Remove How do you remove a value from a binary search tree? To remove Case #2 Ex: remove 30 Search for the value Pointer value’s parent to value’s only child Delete the node

Root 44 Copyright © William C. Cheng Data Structures - CSCI 102 Binary Search Tree Remove How do you remove a value from a binary search tree? Case #2 Ex: remove 30 Search for the value Pointer value’s parent to value’s only child Delete the node

Find the node Remove a node with two children (case #3) 45 Data Structures - CSCI 102 Copyright © William C. Cheng Binary Search Tree Remove Remove node’s successor or predecessor Replace node’s data with successor/predecessor data The node in the tree with the biggest value less than x Predecessor(x) The node in the tree with the smallest value greater than x Successor(x)

Root Case #3 Ex: remove Copyright © William C. Cheng Data Structures - CSCI 102 Binary Search Tree Remove How do you remove a value from a binary search tree?

Root Case #3 Ex: remove Copyright © William C. Cheng Data Structures - CSCI 102 Binary Search Tree Remove How do you remove a value from a binary search tree? Search for the value To remove

Root Case #3 Ex: remove Copyright © William C. Cheng Data Structures - CSCI 102 Binary Search Tree Remove How do you remove a value from a binary search tree? Search for the value Find predecessor Rightmost value in To remove left subtree of node Predecessor

Root Case #3 Ex: remove Copyright © William C. Cheng Data Structures - CSCI 102 Binary Search Tree Remove How do you remove a value from a binary search tree? Predecessor 46 To remove Search for the value Find predecessor Rightmost value in left subtree of node Swap values of node and its predecessor Swap 50

Root 50 Copyright © William C. Cheng Data Structures - CSCI 102 Binary Search Tree Remove How do you remove a value from a binary search tree? Case #3 Ex: remove 50 Search for the value Find predecessor Rightmost value in To remove left subtree of node Swap values of node and its predecessor Delete the node via case #1 or case #2

Root Data Structures - CSCI 102 Binary Search Tree Remove How do you remove a value from a binary search tree? Case #3 Ex: remove 50 Search for the value Find predecessor Rightmost value in left subtree of node Swap values of node and its predecessor Delete the node via case #1 or case # Copyright © William C. Cheng

60, 46, 77, 45, 50, 63, 91 Insert the following values in an empty tree (in this order) 52 Data Structures - CSCI 102 Copyright © William C. Cheng Binary Search Tree Construction What does the resulting tree look like?

53 Copyright © William C. Cheng Data Structures - CSCI 102 Binary Search Tree Construction Insert the following values in an empty tree (in this order) 60, 46, 77, 45, 50, 63, What does the resulting tree look like? Tree is perfectly balanced! Height = log(n+1) Root 77

91, 77, 63, 60, 50, 46, 45 Insert the following values in an empty tree (same values as last time, but different order) 54 Data Structures - CSCI 102 Copyright © William C. Cheng Binary Search Tree Construction What does the resulting tree look like?

91 Root Data Structures - CSCI 102 Binary Search Tree Construction Insert the following values in an empty tree (same values as last time, but different order) 91, 77, 63, 60, 50, 46, 45 What does the resulting tree look like? Insert Copyright © William C. Cheng

91 Root 77 Data Structures - CSCI 102 Binary Search Tree Construction Insert the following values in an empty tree (same values as last time, but different order) 91, 77, 63, 60, 50, 46, 45 What does the resulting tree look like? Insert 91 Insert Copyright © William C. Cheng

91 Root 77 Data Structures - CSCI 102 Binary Search Tree Construction Insert the following values in an empty tree (same values as last time, but different order) 91, 77, 63, 60, 50, 46, 45 What does the resulting tree look like? Insert Insert 77 Insert Copyright © William C. Cheng

58 Copyright © William C. Cheng 91 Root 77 Data Structures - CSCI 102 Binary Search Tree Construction Insert the following values in an empty tree (same values as last time, but different order) 91, 77, 63, 60, 50, 46, 45 What does the resulting tree look like? Insert Insert 77 Insert Insert 60

91 Root Copyright © William C. Cheng Data Structures - CSCI 102 Binary Search Tree Construction Insert the following values in an empty tree (same values as last time, but different order) 91, 77, 63, 60, 50, 46, 45 What does the resulting tree look like? Tree is completely unbalanced 63 Height = n