Chapter 19: Binary Trees Java Programming: Program Design Including Data Structures Program Design Including Data Structures.

Slides:



Advertisements
Similar presentations
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 20: Binary Trees.
Advertisements


1 abstract containers hierarchical (1 to many) graph (many to many) first ith last sequence/linear (1 to 1) set.
Binary Trees, Binary Search Trees CMPS 2133 Spring 2008.
Binary Trees, Binary Search Trees COMP171 Fall 2006.
DictionaryADT and Trees. Overview What is the DictionaryADT? What are trees? Implementing DictionaryADT with binary trees Balanced trees DictionaryADT.
C++ Programming:. Program Design Including
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved L12 (Chapter 20) Lists, Stacks,
1 abstract containers hierarchical (1 to many) graph (many to many) first ith last sequence/linear (1 to 1) set.
Chapter 12 Trees. Copyright © 2005 Pearson Addison-Wesley. All rights reserved Chapter Objectives Define trees as data structures Define the terms.
Data Structures Using C++ 2E Chapter 11 Binary Trees and B-Trees.
A Binary Search Tree Implementation Chapter 25 Copyright ©2012 by Pearson Education, Inc. All rights reserved.
Marc Smith and Jim Ten Eyck
C++ Programming: Program Design Including Data Structures, Third Edition Chapter 20: Binary 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.
Binary Trees Chapter 6.
Data Structures Using C++1 Chapter 11 Binary Trees.
Data Structures Using C++1 Chapter 11 Binary Trees.
Version TCSS 342, Winter 2006 Lecture Notes Trees Binary Trees Binary Search Trees.
By : Budi Arifitama Pertemuan ke Objectives Upon completion you will be able to: Create and implement binary search trees Understand the operation.
Trees Chapter 8. 2 Tree Terminology A tree consists of a collection of elements or nodes, organized hierarchically. The node at the top of a tree is called.
Min Chen School of Computer Science and Engineering Seoul National University Data Structure: Chapter 7.
Chapter 19: Binary Trees. Objectives In this chapter, you will: – Learn about binary trees – Explore various binary tree traversal algorithms – Organize.
1 TK1924 Program Design & Problem Solving Session 2011/2012 L8: Binary Trees.
CS 61B Data Structures and Programming Methodology July 15, 2008 David Sun.
S EARCHING AND T REES COMP1927 Computing 15s1 Sedgewick Chapters 5, 12.
© 2011 Pearson Addison-Wesley. All rights reserved 11 B-1 Chapter 11 (continued) Trees.
Topic 14 The BinaryTree ADT Objectives Define trees as data structures Define the terms associated with trees Discuss tree traversal algorithms.
Spring 2010CS 2251 Trees Chapter 6. Spring 2010CS 2252 Chapter Objectives Learn to use a tree to represent a hierarchical organization of information.
Chapter 6 Binary Trees. 6.1 Trees, Binary Trees, and Binary Search Trees Linked lists usually are more flexible than arrays, but it is difficult to use.
Binary Trees, Binary Search Trees RIZWAN REHMAN CENTRE FOR COMPUTER STUDIES DIBRUGARH UNIVERSITY.
Starting at Binary Trees
Chapter 9 Binary Tree and General Tree. Overview ● Two-way decision making is one of the fundamental concepts in computing.  A binary tree models two-way.
 Trees Data Structures Trees Data Structures  Trees Trees  Binary Search Trees Binary Search Trees  Binary Tree Implementation Binary Tree Implementation.
AVL Trees. AVL Node Structure The AVL node structure follows the same structure as the binary search tree, with the addition of a term to store the.
Chapter 7 Trees_Part3 1 SEARCH TREE. Search Trees 2  Two standard search trees:  Binary Search Trees (non-balanced) All items in left sub-tree are less.
Chapter 4: Trees Part I: General Tree Concepts Mark Allen Weiss: Data Structures and Algorithm Analysis in Java.
IKI 10100I: Data Structures & Algorithms Ruli Manurung (acknowledgments to Denny & Ade Azurat) 1 Fasilkom UI Ruli Manurung (Fasilkom UI)IKI10100I: Data.
Data Structures Using Java1 Chapter 10 Binary 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.
Week 10 - Friday.  What did we talk about last time?  Graph representations  Adjacency matrix  Adjacency lists  Depth first search.
1 Binary Trees and Binary Search Trees Based on Dale & Co: Object-Oriented Data Structures using C++ (graphics)
ADT Binary Search Tree Ellen Walker CPSC 201 Data Structures Hiram College.
Binary Search Trees (BST)
Chapter 6 (cont’) 1 AVL Tree. Search Trees 2 Two standard search trees: Binary Search Trees (non-balanced) All items in left sub-tree are less than root.
Lecture 17: Trees and Networks I Discrete Mathematical Structures: Theory and Applications.
AVL Trees and Heaps. AVL Trees So far balancing the tree was done globally Basically every node was involved in the balance operation Tree balancing can.
Chapter 11 Binary Trees Dr. Youssef Harrath
Data Structures Using C++ 2E Chapter 11 Binary Trees.
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.
BINARY TREES Objectives Define trees as data structures Define the terms associated with trees Discuss tree traversal algorithms Discuss a binary.
IKI 10100: Data Structures & Algorithms Ruli Manurung (acknowledgments to Denny & Ade Azurat) 1 Fasilkom UI Ruli Manurung (Fasilkom UI)IKI10100: Lecture13.
Trees Chapter 15.
Data Structure and Algorithms
Trees Chapter 11 (continued)
Binary Trees and Binary Search Trees
Trees Chapter 11 (continued)
BST Trees
Binary Search Tree (BST)
Binary Search Tree Chapter 10.
Section 8.1 Trees.
Data Structures Using C++ 2E
Binary Trees, Binary Search Trees
Binary Tree and General Tree
Data Structures Using Java
AVL Trees CENG 213 Data Structures.
Binary Trees, Binary Search Trees
Binary Trees, Binary Search Trees
Data Structures Using C++ 2E
Presentation transcript:

Chapter 19: Binary Trees Java Programming: Program Design Including Data Structures Program Design Including Data Structures

Java Programming: Program Design Including Data Structures2 Chapter Objectives  Learn about binary trees  Explore various binary tree traversal algorithms  Learn how to organize data in a binary search tree  Discover how to insert and delete items in a binary search tree

Java Programming: Program Design Including Data Structures3 Binary Trees  A binary tree, T, is either empty or  T has a special node called the root node  T has two sets of nodes, L T and R T, called the left subtree and right subtree  L T and R T are binary trees  A binary tree can be shown pictorially

Java Programming: Program Design Including Data Structures4 Binary Trees (continued) Figure 19-1 Binary tree

Java Programming: Program Design Including Data Structures5 Binary Trees (continued) Figure 19-6 Various binary trees with three nodes

Java Programming: Program Design Including Data Structures6 Binary Trees (continued)  You can write a class that represents each node in a binary tree  Called BinaryTreeNode  Instance variables of the class BinaryTreeNode  info: stores the information part of the node  lLink: points to the root node of the left subtree  rLink: points to the root node of the right subtree

Java Programming: Program Design Including Data Structures7 Binary Trees (continued) Figure 19-7 UML class diagram of the class BinaryTreeNode and the outer-inner class relationship

Java Programming: Program Design Including Data Structures8 Binary Trees (continued) Figure 19-8 Binary tree

Java Programming: Program Design Including Data Structures9 Binary Trees (continued)  A leaf is a node in a tree with no children  Let U and V be two nodes in a binary tree  U is called the parent of V if there is a branch from U to V  A path from a node X to a node Y is a sequence of nodes X 0, X 1,..., X n such that:  X = X 0,X n = Y  X i-1 is the parent of X i for all i = 1, 2,..., n

Java Programming: Program Design Including Data Structures10 Binary Trees (continued)  Length of a path  The number of branches on that path  Level of a node  The number of branches on the path from the root to the node  Height of a binary tree  The number of nodes on the longest path from the root to a leaf

Java Programming: Program Design Including Data Structures11 Binary Trees (continued)  Method height private int height(BinaryTreeNode p) { if (p == null) return 0; else return 1 + Math.max(height(p.lLink), height(p.rLink)); }

Java Programming: Program Design Including Data Structures12 Copy Tree  Method copyTree private BinaryTreeNode copyTree (BinaryTreeNode otherTreeRoot) { BinaryTreeNode temp; if (otherTreeRoot == null) temp = null; else { temp = (BinaryTreeNode ) otherTreeRoot.clone(); temp.lLink = copyTree(otherTreeRoot.lLink); temp.rLink = copyTree(otherTreeRoot.rLink); } return temp; }//end copyTree

Java Programming: Program Design Including Data Structures13 Binary Tree Traversal  Item insertion, deletion, and lookup operations require that the binary tree be traversed  Commonly used traversals  Inorder traversal  Preorder traversal  Postorder traversal

Java Programming: Program Design Including Data Structures14 Inorder Traversal  Binary tree is traversed as follows:  Traverse left subtree  Visit node  Traverse right subtree

Java Programming: Program Design Including Data Structures15 Inorder Traversal (continued)  Method inOrder private void inorder(BinaryTreeNode p) { if (p != null) { inorder(p.lLink); System.out.print(p + “ “); inorder(p.rLink); }

Java Programming: Program Design Including Data Structures16 Preorder Traversal  Binary tree is traversed as follows:  Visit node  Traverse left subtree  Traverse right subtree

Java Programming: Program Design Including Data Structures17 Preorder Traversal (continued)  Method preOrder private void preorder(BinaryTreeNode p) { if (p != null) { System.out.print(p + “ “); preorder(p.lLink); preorder(p.rLink); }

Java Programming: Program Design Including Data Structures18 Postorder Traversal  Binary tree is traversed as follows:  Traverse left subtree  Traverse right subtree  Visit node

Java Programming: Program Design Including Data Structures19 Postorder Traversal (continued)  Method postOrder private void postorder(BinaryTreeNode p) { if (p != null) { postorder(p.lLink); postorder(p.rLink); System.out.print(p + “ “); }

Java Programming: Program Design Including Data Structures20 Implementing Binary Trees Figure UML class diagram of the interface BinaryTreeADT

Java Programming: Program Design Including Data Structures21 Implementing Binary Trees (continued) Figure UML class diagram of the class BinaryTree

Java Programming: Program Design Including Data Structures22 Implementing Binary Trees (continued)  Method clone public Object clone() { BinaryTree copy = null; try { copy = (BinaryTree ) super.clone(); } catch (CloneNotSupportedException e) { return null; } if (root != null) copy.root = copyTree(root); return copy; }

Java Programming: Program Design Including Data Structures23 Binary Search Trees  To search for an item in a normal binary tree, you must traverse entire tree until item is found  Search process will be very slow  Similar to searching in an arbitrary linked list  Binary search tree  Data in each node is  Larger than the data in its left child  Smaller than the data in its right child

Java Programming: Program Design Including Data Structures24 Binary Search Trees (continued) Figure Binary search tree

Java Programming: Program Design Including Data Structures25 Binary Search Trees (continued) Figure UML class diagram of the class BinarySearchTree and the inheritance hierarchy

Java Programming: Program Design Including Data Structures26 Search  Searches tree for a given item  General steps  Compare item with info in root node  If they are the same, stop the search  If item is smaller than info in root node  Follow link to left subtree  Otherwise  Follow link to right subtree

Java Programming: Program Design Including Data Structures27 Insert  Inserts a new item into a binary search tree  General steps  Search tree and find the place where new item is to be inserted  Search algorithm is similar to method search  Insert new item  Duplicate items are not allowed

Java Programming: Program Design Including Data Structures28 Delete  Deletes item from a binary search tree  After deleting items, resulting tree must be a binary search tree  General steps  Search the tree for the item to be deleted  Searching algorithm is similar to method search  Delete item

Java Programming: Program Design Including Data Structures29 Delete (continued)  Delete operation has four cases  Case 1: Node to be deleted is a leaf  Case 2: Node to be deleted has no left subtree  Case 3: Node to be deleted has no right subtree  Case 4: Node to be deleted has nonempty left and right subtrees  search left subtree of the node to be deleted to find its immediate predecessor

Java Programming: Program Design Including Data Structures30 Binary Search Tree: Analysis  Performance depends on shape of the tree  If tree shape is linear, performance is the same as for a linked list  Average number of nodes visited 1.39log 2 n = O(log 2 n)  Average number of key comparisons 2.77log 2 n = O(log 2 n)

Java Programming: Program Design Including Data Structures31 Nonrecursive Binary Tree Traversal Algorithms  Traversal algorithms  Inorder  Preorder  Postorder

Java Programming: Program Design Including Data Structures32 Nonrecursive Inorder Traversal  Method nonRecursiveInTraversal public void nonRecursiveInTraversal() { LinkedStackClass > stack = new LinkedStackClass >(); BinaryTreeNode current; current = root; while ((current != null) || (!stack.isEmptyStack())) if (current != null) { stack.push(current); current = current.lLink; }

Java Programming: Program Design Including Data Structures33 Nonrecursive Inorder Traversal (continued) else { current = (BinaryTreeNode ) stack.peek(); stack.pop(); System.out.print(current.info + “ “); current = current.rLink; } System.out.println(); }

Java Programming: Program Design Including Data Structures34 Nonrecursive Preorder Traversal  General algorithm create stack current = root; while (current is not null or stack is nonempty) if (current is not null) { visit current; push current onto stack; current = current.lLink; } else { pop stack into current; current = current.rLink; //prepare to visit right subtree }

Java Programming: Program Design Including Data Structures35 Nonrecursive Postorder Traversal  General algorithm  Mark left subtree of node as visited  Visit left subtree and return to node  Mark right subtree of node as visited  Visit right subtree and return to node  Visit node

Java Programming: Program Design Including Data Structures36 An Iterator to a Binary Tree  You can create an iterator for a binary tree  Iterator can traverse tree using  Inorder traversal  Preorder traversal  Postorder traversal

Java Programming: Program Design Including Data Structures37 AVL (Height-Balanced) Trees  Search performance depends on shape of the tree  You want the tree to be balanced  AVL (height-balanced) tree  Resulting binary search tree is nearly balanced  Perfectly balanced binary search tree  Heights of the left and right subtrees are equal  Left and right subtrees are perfectly balanced binary trees

Java Programming: Program Design Including Data Structures38 AVL (Height-Balanced) Trees(continued) Figure Perfectly balanced binary tree

Java Programming: Program Design Including Data Structures39 AVL (Height-Balanced) Trees (continued)  AVL tree: A binary search tree where  Heights of left and right subtrees differs by at most 1  Left and right subtrees are AVL trees  Balance factor  Difference between height of right subtree and height of left subtree

Java Programming: Program Design Including Data Structures40 AVL (Height-Balanced) Trees (continued) Figure Perfectly balanced binary tree

Java Programming: Program Design Including Data Structures41 Insertion into AVL Trees  General steps  Search AVL tree to find insertion point  Insert new item  Duplicate items are not allowed  Rebalance tree (if needed)

Java Programming: Program Design Including Data Structures42 Insertion into AVL Trees (continued) Figure AVL tree before inserting 90

Java Programming: Program Design Including Data Structures43 Insertion into AVL Trees (continued) Figure Binary search tree of Figure after inserting 90 ; nodes other than 90 show their balance factors before insertion

Java Programming: Program Design Including Data Structures44 Insertion into AVL Trees (continued) Figure AVL tree of Figure after inserting 90 and adjusting the balance factors

Java Programming: Program Design Including Data Structures45 AVL Tree Rotations  Reconstruction procedure  Types of rotations  Left rotation  Nodes from right subtree move to left subtree  Root of right subtree becomes root of reconstructed subtree  Right rotation  Nodes from left subtree move to right subtree  Root of left subtree becomes root of reconstructed subtree

Java Programming: Program Design Including Data Structures46 AVL Tree Rotations (continued) Figure Left rotation at a

Java Programming: Program Design Including Data Structures47 AVL Tree Rotations (continued) Figure Right rotation at b

Java Programming: Program Design Including Data Structures48 AVL Tree Rotations (continued)  Method rotateToLeft private AVLNode rotateToLeft(AVLNode root) { AVLNode p; //reference variable to the root of the right subtree of root if (root == null) System.err.println(“Error in the tree.”); else if (root.rLink == null) System.err.println(“Error in the tree: “ + “No right subtree to rotate.”); else { p = root.rLink; root.rLink = p.lLink; //the left subtree of p becomes the right //subtree of root p.lLink = root; root = p; //make p the new root node } return root; }//end rotateToLeft

Java Programming: Program Design Including Data Structures49 AVL Tree Rotations (continued)  Method rotateToRight private AVLNode rotateToRight(AVLNode root) { AVLNode p; //reference variable to the root of the //left subtree of root if (root == null) System.err.println(“Error in the tree.”); else if (root.lLink == null) System.err.println(“Error in the tree: “ + “No left subtree to rotate.”); else { p = root.lLink; root.lLink = p.rLink; //the right subtree of p //becomes the left subtree of root p.rLink = root; root = p; //make p the new root node } return root; }//end rotateToRight

Java Programming: Program Design Including Data Structures50 AVL Tree Rotations (continued) Figure AVL tree after inserting 40 Figure AVL tree after inserting 30

Java Programming: Program Design Including Data Structures51 AVL Tree Rotations (continued) Figure AVL tree after inserting 20

Java Programming: Program Design Including Data Structures52 AVL Tree Rotations (continued) Figure AVL tree after inserting 25

Java Programming: Program Design Including Data Structures53 Deletion from AVL Trees  General steps  Find node to be deleted  Delete node  Four cases arise:  Node to be deleted is a leaf  Node to be deleted has no right child  Node to be deleted has no left child  Node to be deleted has both children

Java Programming: Program Design Including Data Structures54  Height of AVL tree with n nodes (worst case) 1.44log 2 n = O(log 2 n)  Time to manipulate an AVL tree in the worst case is no more than 44% of optimum time  Average search time of an AVL tree is about 4% more than the optimum Analysis: AVL Trees

Java Programming: Program Design Including Data Structures55 Programming Example: Video Store (Revisited)  Program in Chapter 16 used a linked list to keep track of video inventory  Search could be time consuming  Modify program to use a binary tree instead  Insertion and deletion in a binary search tree is faster than in a linked list

Java Programming: Program Design Including Data Structures56 Chapter Summary  Binary trees  Every node has only two children  Left subtree  Right subtree  Binary tree traversal  Inorder  Preorder  Postorder

Java Programming: Program Design Including Data Structures57 Chapter Summary (continued)  Binary search trees  Each node is greater than elements in its left subtree and less than elements in its right subtree  AVL (height-balanced) trees  Binary search tree  Heights of left and right subtrees differs by at most 1  Left and right subtrees of the root node are AVL trees