Chapter 25 Binary Search Trees

Slides:



Advertisements
Similar presentations
Chapter 12 Binary Search Trees
Advertisements

TREES Chapter 6. Trees - Introduction  All previous data organizations we've studied are linear—each element can have only one predecessor and successor.
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.
Trees Chapter 8.
Fall 2007CS 2251 Trees Chapter 8. Fall 2007CS 2252 Chapter Objectives To learn how to use a tree to represent a hierarchical organization of information.
Binary Trees Terminology A graph G = is a collection of nodes and edges. An edge (v 1,v 2 ) is a pair of vertices that are directly connected. A path,
Trees Chapter 8. Chapter 8: Trees2 Chapter Objectives To learn how to use a tree to represent a hierarchical organization of information To learn how.
Trees Chapter 8. Chapter 8: Trees2 Chapter Objectives To learn how to use a tree to represent a hierarchical organization of information To learn how.
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,
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Chapter 26 Binary Search Trees.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved L12 (Chapter 20) Lists, Stacks,
Lecture Using Libraries Java has a very strong culture of open-source software Students, professors, programming hobbyists, and developers who choose.
Binary Search Trees Chapter 7 Objectives
Chapter 08 Binary Trees and Binary Search Trees © John Urrutia 2013, All Rights Reserved.
More Trees COL 106 Amit Kumar and Shweta Agrawal Most slides courtesy : Douglas Wilhelm Harder, MMath, UWaterloo
By : Budi Arifitama Pertemuan ke Objectives Upon completion you will be able to: Create and implement binary search trees Understand the operation.
Data Structures Arrays both single and multiple dimensions Stacks Queues Trees Linked Lists.
1 Chapter 25 Trees Iterators Heaps Priority Queues.
Trees. Tree Terminology Chapter 8: Trees 2 A tree consists of a collection of elements or nodes, with each node linked to its successors The node at the.
Review Binary Tree Binary Tree Representation Array Representation Link List Representation Operations on Binary Trees Traversing Binary Trees Pre-Order.
Trees Chapter 8. Chapter 8: Trees2 Chapter Objectives To learn how to use a tree to represent a hierarchical organization of information To learn how.
Spring 2010CS 2251 Trees Chapter 6. Spring 2010CS 2252 Chapter Objectives Learn to use a tree to represent a hierarchical organization of information.
CISC220 Fall 2009 James Atlas Lecture 13: Trees. Skip Lists.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Chapter 20 Lists, Stacks,
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).
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 Search Tree Traversal Methods. How are they different from Binary Trees?  In computer science, a binary tree is a tree data structure in which.
 Trees Data Structures Trees Data Structures  Trees Trees  Binary Search Trees Binary Search Trees  Binary Tree Implementation Binary Tree Implementation.
Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy Walters, and Godfrey Muganda Modified for use at Midwestern State University Chapter.
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved Chapter 25 Trees, Iterators,
© Copyright 2012 by Pearson Education, Inc. All Rights Reserved. 1 Chapter 19 Binary Search Trees.
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.
Binary Tree. Some Terminologies Short review on binary tree Tree traversals Binary Search Tree (BST)‏ Questions.
Binary Search Trees (BST)
Copyright © 2012 Pearson Education, Inc. Chapter 20: Binary Trees.
COSC 2P03 Week 21 Tree Traversals – reminder Breadth-first traversal: starting from root, visit all nodes on each level in turn, from left to right Depth-first.
Data Structures: A Pseudocode Approach with C, Second Edition 1 Chapter 7 Objectives Create and implement binary search trees Understand the operation.
1 Trees General Trees  Nonrecursive definition: a tree consists of a set of nodes and a set of directed edges that connect pairs of nodes.
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.
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.
1 Trees General Trees  Nonrecursive definition: a tree consists of a set of nodes and a set of directed edges that connect pairs of nodes.
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. 
Chapter 11. Chapter Summary  Introduction to trees (11.1)  Application of trees (11.2)  Tree traversal (11.3)  Spanning trees (11.4)
Binary Search Trees Chapter 7 Objectives
Trees Saurav Karmakar
Non Linear Data Structure
Data Structures and Design in Java © Rick Mercer
Recursive Objects (Part 4)
Chapter 5 : Trees.
Binary Search Trees Chapter 7 Objectives
Binary Search Tree (BST)
Lecture 22 Binary Search Trees Chapter 10 of textbook
Trees.
Tree Traversals – reminder
Chapter 17 Object-Oriented Data Structures
Binary Tree and General Tree
Chapter 20: Binary Trees.
Tree Traversals – reminder
Chapter 21: Binary Trees.
Trees 7/14/2009.
General Trees & Binary Trees
Find in a linked list? first last 7  4  3  8 NULL
Lecture 12 CS203 1.
Binary Trees, Binary Search Trees
Chapter 20: Binary Trees.
Trees.
Binary Trees, Binary Search Trees
Data Structures Using C++ 2E
Tree (new ADT) Terminology: A tree is a collection of elements (nodes)
Presentation transcript:

Chapter 25 Binary Search Trees Jung Soo (Sue) Lim Cal State LA

Objectives To design and implement a binary search tree (§25.2). To represent binary trees using linked data structures (§25.2.1). To search an element in binary search tree (§25.2.2). To insert an element into a binary search tree (§25.2.3). To traverse elements in a binary tree (§25.2.4). To delete elements from a binary search tree (§25.3). To display binary tree graphically (§25.4). To create iterators for traversing a binary tree (§25.5). To implement Huffman coding for compressing data using a binary tree (§25.6).

Binary Trees A list, stack, or queue is a linear structure that consists of a sequence of elements. A binary tree is a hierarchical structure. It is either empty or consists of an element, called the root, and two distinct binary trees, called the left subtree and right subtree.

See How a Binary Search Tree Works www.cs.armstrong.edu/liang/animation/web/BST.html

Binary Tree Terms The root of left (right) subtree of a node is called a left (right) child of the node. A node without children is called a leaf. A special type of binary tree called a binary search tree is often useful. A binary search tree (with no duplicate elements) has the property that for every node in the tree the value of any node in its left subtree is less than the value of the node and the value of any node in its right subtree is greater than the value of the node. The binary trees in Figure 25.1 are all binary search trees. This section is concerned with binary search trees.

Representing Binary Trees A binary tree can be represented using a set of linked nodes. Each node contains a value and two links named left and right that reference the left child and right child, respectively, as shown in Figure 25.2. class TreeNode<E> { E element; TreeNode<E> left; TreeNode<E> right;   public TreeNode(E o) { element = o; }

Searching an Element in a Binary Search Tree public boolean search(E element) { TreeNode<E> current = root; // Start from the root while (current != null) if (element < current.element) { current = current.left; // Go left } else if (element > current.element) { current = current.right; // Go right else // Element matches current.element return true; // Element is found return false; // Element is not in the tree

Inserting an Element to a Binary Search Tree If a binary tree is empty, create a root node with the new element. Otherwise, locate the parent node for the new element node. If the new element is less than the parent element, the node for the new element becomes the left child of the parent. If the new element is greater than the parent element, the node for the new element becomes the right child of the parent. Here is the algorithm:

Inserting an Element to a Binary Tree if (root == null) root = new TreeNode(element); else { // Locate the parent node current = root; while (current != null) if (element value < the value in current.element) { parent = current; current = current.left; } else if (element value > the value in current.element) { current = current.right; else return false; // Duplicate node not inserted // Create the new node and attach it to the parent node if (element < parent.element) parent.left = new TreeNode(elemenet); parent.right = new TreeNode(elemenet);   return true; // Element inserted Insert 101 into the following tree.

Trace Inserting 101 into the following tree if (root == null) root = new TreeNode(element); else { // Locate the parent node current = root; while (current != null) if (element value < the value in current.element) { parent = current; current = current.left; } else if (element value > the value in current.element) { current = current.right; else return false; // Duplicate node not inserted // Create the new node and attach it to the parent node if (element < parent.element) parent.left = new TreeNode(elemenet); parent.right = new TreeNode(elemenet);   return true; // Element inserted Insert 101 into the following tree.

Trace Inserting 101 into the following tree, cont. if (root == null) root = new TreeNode(element); else { // Locate the parent node current = root; while (current != null) if (element value < the value in current.element) { parent = current; current = current.left; } else if (element value > the value in current.element) { current = current.right; else return false; // Duplicate node not inserted // Create the new node and attach it to the parent node if (element < parent.element) parent.left = new TreeNode(elemenet); parent.right = new TreeNode(elemenet);   return true; // Element inserted Insert 101 into the following tree.

Trace Inserting 101 into the following tree, cont. if (root == null) root = new TreeNode(element); else { // Locate the parent node current = root; while (current != null) if (element value < the value in current.element) { parent = current; current = current.left; } else if (element value > the value in current.element) { current = current.right; else return false; // Duplicate node not inserted // Create the new node and attach it to the parent node if (element < parent.element) parent.left = new TreeNode(elemenet); parent.right = new TreeNode(elemenet);   return true; // Element inserted Insert 101 into the following tree.

Trace Inserting 101 into the following tree, cont. if (root == null) root = new TreeNode(element); else { // Locate the parent node current = root; while (current != null) if (element value < the value in current.element) { parent = current; current = current.left; } else if (element value > the value in current.element) { current = current.right; else return false; // Duplicate node not inserted // Create the new node and attach it to the parent node if (element < parent.element) parent.left = new TreeNode(elemenet); parent.right = new TreeNode(elemenet);   return true; // Element inserted Insert 101 into the following tree. 101 < 60?

Trace Inserting 101 into the following tree, cont. if (root == null) root = new TreeNode(element); else { // Locate the parent node current = root; while (current != null) if (element value < the value in current.element) { parent = current; current = current.left; } else if (element value > the value in current.element) { current = current.right; else return false; // Duplicate node not inserted // Create the new node and attach it to the parent node if (element < parent.element) parent.left = new TreeNode(elemenet); parent.right = new TreeNode(elemenet);   return true; // Element inserted Insert 101 into the following tree. 101 > 60?

Trace Inserting 101 into the following tree, cont. if (root == null) root = new TreeNode(element); else { // Locate the parent node current = root; while (current != null) if (element value < the value in current.element) { parent = current; current = current.left; } else if (element value > the value in current.element) { current = current.right; else return false; // Duplicate node not inserted // Create the new node and attach it to the parent node if (element < parent.element) parent.left = new TreeNode(elemenet); parent.right = new TreeNode(elemenet);   return true; // Element inserted Insert 101 into the following tree. 101 > 60 true

Trace Inserting 101 into the following tree, cont. if (root == null) root = new TreeNode(element); else { // Locate the parent node current = root; while (current != null) if (element value < the value in current.element) { parent = current; current = current.left; } else if (element value > the value in current.element) { current = current.right; else return false; // Duplicate node not inserted // Create the new node and attach it to the parent node if (element < parent.element) parent.left = new TreeNode(elemenet); parent.right = new TreeNode(elemenet);   return true; // Element inserted Insert 101 into the following tree. 101 > 60 true

Trace Inserting 101 into the following tree, cont. if (root == null) root = new TreeNode(element); else { // Locate the parent node current = root; while (current != null) if (element value < the value in current.element) { parent = current; current = current.left; } else if (element value > the value in current.element) { current = current.right; else return false; // Duplicate node not inserted // Create the new node and attach it to the parent node if (element < parent.element) parent.left = new TreeNode(elemenet); parent.right = new TreeNode(elemenet);   return true; // Element inserted Insert 101 into the following tree. 101 > 60 true

Trace Inserting 101 into the following tree, cont. if (root == null) root = new TreeNode(element); else { // Locate the parent node current = root; while (current != null) if (element value < the value in current.element) { parent = current; current = current.left; } else if (element value > the value in current.element) { current = current.right; else return false; // Duplicate node not inserted // Create the new node and attach it to the parent node if (element < parent.element) parent.left = new TreeNode(elemenet); parent.right = new TreeNode(elemenet);   return true; // Element inserted Insert 101 into the following tree. 101 > 100 false

Trace Inserting 101 into the following tree, cont. if (root == null) root = new TreeNode(element); else { // Locate the parent node current = root; while (current != null) if (element value < the value in current.element) { parent = current; current = current.left; } else if (element value > the value in current.element) { current = current.right; else return false; // Duplicate node not inserted // Create the new node and attach it to the parent node if (element < parent.element) parent.left = new TreeNode(elemenet); parent.right = new TreeNode(elemenet);   return true; // Element inserted Insert 101 into the following tree. 101 > 100 true

Trace Inserting 101 into the following tree, cont. if (root == null) root = new TreeNode(element); else { // Locate the parent node current = root; while (current != null) if (element value < the value in current.element) { parent = current; current = current.left; } else if (element value > the value in current.element) { current = current.right; else return false; // Duplicate node not inserted // Create the new node and attach it to the parent node if (element < parent.element) parent.left = new TreeNode(elemenet); parent.right = new TreeNode(elemenet);   return true; // Element inserted Insert 101 into the following tree. 101 > 100 true

Trace Inserting 101 into the following tree, cont. if (root == null) root = new TreeNode(element); else { // Locate the parent node current = root; while (current != null) if (element value < the value in current.element) { parent = current; current = current.left; } else if (element value > the value in current.element) { current = current.right; else return false; // Duplicate node not inserted // Create the new node and attach it to the parent node if (element < parent.element) parent.left = new TreeNode(elemenet); parent.right = new TreeNode(elemenet);   return true; // Element inserted Insert 101 into the following tree. 101 > 100 true

Trace Inserting 101 into the following tree, cont. if (root == null) root = new TreeNode(element); else { // Locate the parent node current = root; while (current != null) if (element value < the value in current.element) { parent = current; current = current.left; } else if (element value > the value in current.element) { current = current.right; else return false; // Duplicate node not inserted // Create the new node and attach it to the parent node if (element < parent.element) parent.left = new TreeNode(elemenet); parent.right = new TreeNode(elemenet);   return true; // Element inserted Insert 101 into the following tree. 101 > 100 true

Trace Inserting 101 into the following tree, cont. if (root == null) root = new TreeNode(element); else { // Locate the parent node current = root; while (current != null) if (element value < the value in current.element) { parent = current; current = current.left; } else if (element value > the value in current.element) { current = current.right; else return false; // Duplicate node not inserted // Create the new node and attach it to the parent node if (element < parent.element) parent.left = new TreeNode(elemenet); parent.right = new TreeNode(elemenet);   return true; // Element inserted Insert 101 into the following tree. 101 < 107 true

Trace Inserting 101 into the following tree, cont. if (root == null) root = new TreeNode(element); else { // Locate the parent node current = root; while (current != null) if (element value < the value in current.element) { parent = current; current = current.left; } else if (element value > the value in current.element) { current = current.right; else return false; // Duplicate node not inserted // Create the new node and attach it to the parent node if (element < parent.element) parent.left = new TreeNode(elemenet); parent.right = new TreeNode(elemenet);   return true; // Element inserted Insert 101 into the following tree. 101 < 107 true

Trace Inserting 101 into the following tree, cont. if (root == null) root = new TreeNode(element); else { // Locate the parent node current = root; while (current != null) if (element value < the value in current.element) { parent = current; current = current.left; } else if (element value > the value in current.element) { current = current.right; else return false; // Duplicate node not inserted // Create the new node and attach it to the parent node if (element < parent.element) parent.left = new TreeNode(elemenet); parent.right = new TreeNode(elemenet);   return true; // Element inserted Insert 101 into the following tree. 101 < 107 true

Trace Inserting 101 into the following tree, cont. if (root == null) root = new TreeNode(element); else { // Locate the parent node current = root; while (current != null) if (element value < the value in current.element) { parent = current; current = current.left; } else if (element value > the value in current.element) { current = current.right; else return false; // Duplicate node not inserted // Create the new node and attach it to the parent node if (element < parent.element) parent.left = new TreeNode(elemenet); parent.right = new TreeNode(elemenet);   return true; // Element inserted Insert 101 into the following tree. current is null now

Trace Inserting 101 into the following tree, cont. if (root == null) root = new TreeNode(element); else { // Locate the parent node current = root; while (current != null) if (element value < the value in current.element) { parent = current; current = current.left; } else if (element value > the value in current.element) { current = current.right; else return false; // Duplicate node not inserted // Create the new node and attach it to the parent node if (element < parent.element) parent.left = new TreeNode(elemenet); parent.right = new TreeNode(elemenet);   return true; // Element inserted Insert 101 into the following tree. 101 < 107 true

Trace Inserting 101 into the following tree, cont. if (root == null) root = new TreeNode(element); else { // Locate the parent node current = root; while (current != null) if (element value < the value in current.element) { parent = current; current = current.left; } else if (element value > the value in current.element) { current = current.right; else return false; // Duplicate node not inserted // Create the new node and attach it to the parent node if (element < parent.element) parent.left = new TreeNode(elemenet); parent.right = new TreeNode(elemenet);   return true; // Element inserted Insert 101 into the following tree. 101 < 107 true

Trace Inserting 101 into the following tree, cont. if (root == null) root = new TreeNode(element); else { // Locate the parent node current = root; while (current != null) if (element value < the value in current.element) { parent = current; current = current.left; } else if (element value > the value in current.element) { current = current.right; else return false; // Duplicate node not inserted // Create the new node and attach it to the parent node if (element < parent.element) parent.left = new TreeNode(elemenet); parent.right = new TreeNode(elemenet);   return true; // Element inserted Insert 101 into the following tree. 101 < 107 true

Inserting 59 into the Tree if (root == null) root = new TreeNode(element); else { // Locate the parent node current = root; while (current != null) if (element value < the value in current.element) { parent = current; current = current.left; } else if (element value > the value in current.element) { current = current.right; else return false; // Duplicate node not inserted // Create the new node and attach it to the parent node if (element < parent.element) parent.left = new TreeNode(elemenet); parent.right = new TreeNode(elemenet);   return true; // Element inserted

Tree Traversal Tree traversal is the process of visiting each node in the tree exactly once. There are several ways to traverse a tree. This section presents inorder, preorder, postorder, depth-first, and breadth-first traversals. The inorder traversal is to visit the left subtree of the current node first recursively, then the current node itself, and finally the right subtree of the current node recursively.   The postorder traversal is to visit the left subtree of the current node first, then the right subtree of the current node, and finally the current node itself.

Tree Traversal, cont. The preorder traversal is to visit the current node first, then the left subtree of the current node recursively, and finally the right subtree of the current node recursively.

Tree Traversal, cont. The breadth-first traversal is to visit the nodes level by level. First visit the root, then all children of the root from left to right, then grandchildren of the root from left to right, and so on.   For example, in the tree in Figure 25.2, the inorder is 45 55 57 59 60 67 100 101 107. The postorder is 45 59 57 55 67 101 107 100 60. The preorder is 60 55 45 57 59 100 67 107 101. The breadth-first traversal is 60 55 100 45 57 67 107 59 101.

The Tree Interface The Tree interface defines common operations for trees. The AbstractTree class partially implements Tree.

The BST Class Let’s define the binary tree class, named BST with A concrete BST class can be defined to extend AbstractTree.

Tree After Insertions Inorder: Adam, Daniel George, Jones, Michael, Peter, Tom Postorder: Daniel Adam, Jones, Peter, Tom, Michael, George Preorder: George, Adam, Daniel, Michael, Jones, Tom, Peter

Deleting Elements in a Binary Search Tree To delete an element from a binary tree, you need to first locate the node that contains the element and also its parent node. Let current point to the node that contains the element in the binary tree and parent point to the parent of the current node. The current node may be a left child or a right child of the parent node. There are two cases to consider:

Deleting Elements in a Binary Search Tree Case 1: The current node does not have a left child, as shown in this figure (a). Simply connect the parent with the right child of the current node, as shown in this figure (b).

Deleting Elements in a Binary Search Tree For example, to delete node 10 in Figure 25.9a. Connect the parent of node 10 with the right child of node 10, as shown in Figure 25.9b.

Deleting Elements in a Binary Search Tree Case 2: The current node has a left child. Let rightMost point to the node that contains the largest element in the left subtree of the current node and parentOfRightMost point to the parent node of the rightMost node, as shown in Figure 25.10a. Note that the rightMost node cannot have a right child, but may have a left child. Replace the element value in the current node with the one in the rightMost node, connect the parentOfRightMost node with the left child of the rightMost node, and delete the rightMost node, as shown in Figure 25.10b.

Deleting Elements in a Binary Search Tree Case 2 diagram

Deleting Elements in a Binary Search Tree Case 2 example, delete 20

Examples

Examples

Examples

binary tree time complexity It is obvious that the time complexity for the inorder, preorder, and postorder is O(n), since each node is traversed only once. The time complexity for search, insertion and deletion is the height of the tree. In the worst case, the height of the tree is O(n).

Tree Visualization

Iterators An iterator is an object that provides a uniform way for traversing the elements in a container such as a set, list, binary tree, etc.

Data Compression: Huffman Coding In ASCII, every character is encoded in 8 bits. Huffman coding compresses data by using fewer bits to encode more frequently occurring characters. The codes for characters are constructed based on the occurrence of characters in the text using a binary tree, called the Huffman coding tree.

See How a Binary Tree Works www.cs.armstrong.edu/liang/animation/HuffmanCodingAnimation.html

Constructing Huffman Tree To construct a Huffman coding tree, use a greedy algorithm as follows: Begin with a forest of trees. Each tree contains a node for a character. The weight of the node is the frequency of the character in the text. Repeat this step until there is only one tree: Choose two trees with the smallest weight and create a new node as their parent. The weight of the new tree is the sum of the weight of the subtrees.

Constructing Huffman Tree