1 Binary Trees and Binary Search Trees Based on Dale & Co: Object-Oriented Data Structures using C++ (graphics)

Slides:



Advertisements
Similar presentations
S. Sudarshan Based partly on material from Fawzi Emad & Chau-Wen Tseng
Advertisements

Chapter 8 Binary Search Tree 1 Fall Binary Trees 2 A structure with: i) a unique starting node (the root), in which ii) each node has up to two.
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.
1 Jake’s Pizza Shop Owner Jake Manager Chef Brad Carol Waitress Waiter Cook Helper Joyce Chris Max Len.
Binary Trees, Binary Search Trees COMP171 Fall 2006.
CS 171: Introduction to Computer Science II
Unit 11a 1 Unit 11: Data Structures & Complexity H We discuss in this unit Graphs and trees Binary search trees Hashing functions Recursive sorting: quicksort,
Data Structures Using C++ 2E Chapter 11 Binary Trees and B-Trees.
CS21, Tia Newhall Binary Search Trees (BST) 1.Hierarchical data structure with a single pointer to root node 2.Each node has at most two child nodes (a.
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.
CSCE 3110 Data Structures & Algorithm Analysis Binary Search Trees Reading: Chap. 4 (4.3) Weiss.
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 Data Structures Chapter 15 Trees Mehmet H Gunes
Trees EENG212 Algorithms and Data Structures. Trees Outline  Introduction to Trees  Binary Trees: Basic Definitions  Traversing Binary Trees  Node.
1 Lecture 11 POLYNOMIALS and Tree sort 2 INTRODUCTION EVALUATING POLYNOMIAL FUNCTIONS Horner’s method Permutation Tree sort.
Lecture 10 Trees –Definiton of trees –Uses of trees –Operations on a tree.
CISC220 Fall 2009 James Atlas Lecture 13: Trees. Skip Lists.
Binary Trees 2 Overview Trees. Terminology. Traversal of Binary Trees. Expression Trees. Binary Search Trees.
Tree (new ADT) Terminology:  A tree is a collection of elements (nodes)  Each node may have 0 or more successors (called children)  How many does a.
Binary Trees, Binary Search Trees RIZWAN REHMAN CENTRE FOR COMPUTER STUDIES DIBRUGARH UNIVERSITY.
Tree Data Structures.
Chapter 19: Binary Trees Java Programming: Program Design Including Data Structures Program Design Including Data Structures.
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.
Trees Isaac Sheff. Nodes Building blocks of trees “Parent” node may have “Child” nodes Can be both parent and child Can’t be its own ancestor Can’t have.
Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy Walters, and Godfrey Muganda Modified for use at Midwestern State University Chapter.
Topics Definition and Application of Binary Trees Binary Search Tree Operations.
1 Nell Dale Chapter 8 Binary Search Trees Modified from the slides by Sylvia Sorkin, Community College of Baltimore County - Essex Campus C++ Plus Data.
Binary Trees In computer science, a binary tree is a tree data structure in which each node has at most two children, which are referred to as the left.
Computer Science and Software Engineering University of Wisconsin - Platteville 10. Binary Search Tree Yan Shi CS/SE 2630 Lecture Notes Partially adopted.
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.
Binary Tree. Some Terminologies Short review on binary tree Tree traversals Binary Search Tree (BST)‏ Questions.
1/14/20161 BST Operations Data Structures Ananda Gunawardena.
1 Lecture 21: Binary Search Tree delete etc. operations Lecturer: Santokh Singh CompSci 105 SS 2005 Principles of Computer Science.
ADT Binary Search Tree Ellen Walker CPSC 201 Data Structures Hiram College.
Lecture - 11 on Data Structures. Prepared by, Jesmin Akhter, Lecturer, IIT,JU Threaded Trees Binary trees have a lot of wasted space: the leaf nodes each.
Binary Search Trees (BST)
Copyright © 2012 Pearson Education, Inc. Chapter 20: Binary Trees.
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.
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 Nell Dale Chapter 8 Binary Search Trees Slides by Sylvia Sorkin, Community College of Baltimore County - Essex Campus C++ Plus Data Structures.
1 Trees. 2 Trees Trees. Binary Trees Tree Traversal.
Data Structure and Algorithms
Binary Trees and Binary Search Trees
Recursive Objects (Part 4)
BST Trees
Binary Search Tree (BST)
Section 8.1 Trees.
Trees.
ITEC 2620M Introduction to Data Structures
Data Structures Using C++ 2E
Binary Trees, Binary Search Trees
Chapter 20: Binary Trees.
Chapter 21: Binary Trees.
Binary Trees.
Find in a linked list? first last 7  4  3  8 NULL
Binary Trees.
Binary Trees, Binary Search Trees
Chapter 20: Binary Trees.
Yan Shi CS/SE 2630 Lecture Notes
Trees.
Binary Trees, Binary Search Trees
Data Structures Using C++ 2E
Presentation transcript:

1 Binary Trees and Binary Search Trees Based on Dale & Co: Object-Oriented Data Structures using C++ (graphics)

2 A binary tree is a structure in which: Each node can have at most two children, and in which a unique path exists from the root to every other node. The two children of a node are called the left child and the right child, if they exist. Binary Tree

3 A Binary Tree Q V T K S A E L

4 How many leaf nodes? Q V T K S A E L

5 How many descendants of Q? Q V T K S A E L

6 How many ancestors of K? Q V T K S A E L

7 A Binary Tree with Tree Nodes Q V T K S A E L

8 Node Terminology for a Tree Node

9 Binary Trees l 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

10 Tree Traversals l Insertion, deletion, and lookup (and other) operations require that the binary tree be traversed l Commonly used traversals n Inorder traversal n Preorder traversal n Postorder traversal

11 Tree Traversals: In Order Print

12 Inorder (tree): LeftNodeRight l Binary tree is traversed as follows: n Traverse left subtree n Visit node n Traverse right subtree private void inorder(BinaryTreeNode t){ if (t != null) { inorder(t.lLink); System.out.print(t.info + “ “); inorder(t.rLink); } To print in alphabetical order

13 Postorder (tree): LeftRightNode l Binary tree is traversed as follows: n Traverse left subtree n Traverse right subtree n Visit node private void postorder(BinaryTreeNode t){ if (t != null){ postorder(t.lLink); postorder(t.rLink); System.out.print(t.info + “ “); } Visits leaves first (good for deletion)

14 Preorder (tree): NodeLeftRight l Binary tree is traversed as follows: n Visit node n Traverse left subtree n Traverse right subtree private void preorder(BinaryTreeNode t){ if (t != null) { System.out.print(t.info + “ “); preorder(t.lLink); preorder(t.rLink); } Useful with binary trees (not BST)

15 Three Tree Traversals

16 Binary Search Trees (BST) l To search for an item in a normal binary tree, you must traverse entire tree until item is found n Search process will be very slow n Similar to searching in an arbitrary linked list l Binary search tree n Data in each node is –Larger than the data in its left child –Smaller than the data in its right child

17 A special kind of binary tree in which: 1. Each node contains a distinct data value, 2. The key values in the tree can be compared using “greater than” and “less than”, and 3. The key value of each node in the tree is less than every key value in its right subtree, and greater than every key value in its left subtree. So, a Binary Search Tree (BST) is...

18 Depends on its key values and their order of insertion. Insert the elements ‘J’ ‘E’ ‘F’ ‘T’ ‘A’ in that order. The first value to be inserted is put into the root node. The shape of a binary search tree... ‘J’

19 Thereafter, each value to be inserted begins by comparing itself to the value in the root node, moving left it is less, or moving right if it is greater. This continues at each level until it can be inserted as a new leaf. Inserting ‘E’ into the BST ‘J’ ‘E’

20 Begin by comparing ‘F’ to the value in the root node, moving left it is less, or moving right if it is greater. This continues until it can be inserted as a leaf. Inserting ‘F’ into the BST ‘J’ ‘E’ ‘F’

21 Begin by comparing ‘T’ to the value in the root node, moving left it is less, or moving right if it is greater. This continues until it can be inserted as a leaf. Inserting ‘T’ into the BST ‘J’ ‘E’ ‘F’ ‘T’

22 Begin by comparing ‘A’ to the value in the root node, moving left it is less, or moving right if it is greater. This continues until it can be inserted as a leaf. Inserting ‘A’ into the BST ‘J’ ‘E’ ‘F’ ‘T’ ‘A’

23 is obtained by inserting the elements ‘A’ ‘E’ ‘F’ ‘J’ ‘T’ in that order? What binary search tree... ‘A’

24 obtained by inserting the elements ‘A’ ‘E’ ‘F’ ‘J’ ‘T’ in that order. Binary search tree... ‘A’ ‘E’ ‘F’ ‘J’ ‘T’

25 Another binary search tree Add nodes containing these values in this order: ‘D’ ‘B’ ‘L’ ‘Q’ ‘S’ ‘V’ ‘Z’ ‘J’ ‘E’ ‘A’ ‘H’ ‘T’ ‘M’ ‘K’ ‘P’

26 Is ‘F’ in the binary search tree? ‘J’ ‘E’ ‘A’ ‘H’ ‘T’ ‘M’ ‘K’ ‘V’ ‘P’ ‘Z’‘D’‘Q’‘L’‘B’‘S’

27 Tree Recursion Method countNodes (pseudocode): if tree is null return 0 else return countNodes(Left(tree)) + countNodes(Right(tree)) + 1

28 The Search Operation

29 The Search Operation public boolean search(T item) { return recSearch(root, item); } //helper: recursive method called by search public boolean recSearch(BinaryTreeNode tree, T item) { if(tree == null) return false; else { Comparable temp = (Comparable ) tree.info; if (temp.compareTo(item) == 0) return true; else if (temp.compareTo(item) > 0) return recSearch(tree.lLink, item); else return recSearch(tree.rLink, item); }

30 The Insert Operation l Important: A new node is always inserted into its appropriate position in the binary search tree as a leaf.

31 Insertions into a Binary Search Tree

32 The Insert Operation public void insert(T item) { root = recInsert(root, item); } //helper: recursive method called by insert public BinaryTreeNode recInsert(BinaryTreeNode tree, T item) { if(tree == null) { //create new node tree = new BinaryTreeNode (item); } else { Comparable temp = (Comparable ) tree.info; if (temp.compareTo(item) == 0) { System.err.print("Already in - no duplicates."); return null; } else if (temp.compareTo(item) > 0) tree.lLink = recInsert(tree.lLink, item); else tree.rLink = recInsert(tree.rLink, item); } return tree; }

33 The Delete Operation l The delete operation has 3 cases n Node to be deleted is a leaf n Node to be deleted has 1 child (left OR right) n Node to be deleted has 2 children (nonempty left and right subtrees)

34 Deleting a Leaf Node

35 Deleting a Node with One Child

36 Deleting a Node with Two Children (Q)

37 The Delete Operation Base Case: If item's key matches Info(tree), delete node General Case: If item < Info(tree), delete(Left(tree), item); else delete(Right(tree), item).

38 Code for Recursive Delete public void delete(T item) { root = recDelete(root, item); } //helper: recursive method called by delete public BinaryTreeNode recDelete(BinaryTreeNode tree, T item) { if(tree == null){ //empty tree System.err.println("Cannot delete from an empty tree."); return null; } else { Comparable temp = (Comparable ) tree.info; if (temp.compareTo(item) > 0) tree.lLink = recDelete(tree.lLink, item); else if(temp.compareTo(item) < 0) tree.rLink = recDelete(tree.rLink, item); else if(tree.lLink != null && tree.rLink != null) {// 2 children tree.info = findMin(tree.rLink).info; //tree.info = findMax(tree.lLink).info; tree.rLink = removeMin(tree.rLink); } else if(root.lLink != null) //1 left child tree = tree.lLink; else if(root.rLink != null) //1 right child tree = tree.rLink; return tree; }

39 Code for Recursive Delete //helper: method called by recDelete protected BinaryTreeNode findMin(BinaryTreeNode tree) { if(tree != null) while(tree.lLink != null) tree = tree.lLink; return tree; } //helper: method called by recDelete protected BinaryTreeNode removeMin(BinaryTreeNode tree) { if(tree == null){ //empty tree System.err.println("Cannot delete from an empty tree."); return null; } else if(tree.lLink != null) { tree.lLink = removeMin(tree.lLink); return tree; } else return tree.rLink; }