Tonga Institute of Higher Education

Slides:



Advertisements
Similar presentations
Binary Trees CSC 220. Your Observations (so far data structures) Array –Unordered Add, delete, search –Ordered Linked List –??
Advertisements

SUNY Oneonta Data Structures and Algorithms Visualization Teaching Materials Generation Group Binary Search Tree A running demonstration of binary search.
Binary Trees, Binary Search Trees CMPS 2133 Spring 2008.
CS 171: Introduction to Computer Science II
Data Structures: Trees i206 Fall 2010 John Chuang Some slides adapted from Marti Hearst, Brian Hayes, or Glenn Brookshear.
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,
CS 104 Introduction to Computer Science and Graphics Problems Data Structure & Algorithms (4) Data Structures 11/18/2008 Yang Song.
© 2006 Pearson Addison-Wesley. All rights reserved11 A-1 Chapter 11 Trees.
Binary Trees Chapter 6.
Data Structures and Algorithms Session 13 Ver. 1.0 Objectives In this session, you will learn to: Store data in a tree Implement a binary tree Implement.
Data Structures – Binary Tree
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.
Tree.
Trees Chapter 15 Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013.
Lecture 10 Trees –Definiton of trees –Uses of trees –Operations on a tree.
Binary Trees Chapter Definition And Application Of Binary Trees Binary tree: a nonlinear linked list in which each node may point to 0, 1, or two.
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.
Starting at Binary Trees
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.
Computer Science 112 Fundamentals of Programming II Introduction to Trees.
Chapter 4: Trees Part I: General Tree Concepts Mark Allen Weiss: Data Structures and Algorithm Analysis in Java.
Introduction to Trees IT12112 Lecture 05 Introduction Tree is one of the most important non-linear data structures in computing. It allows us to implement.
Discrete Mathematics Chapter 5 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.
M180: Data Structures & Algorithms in Java Trees & Binary Trees Arab Open University 1.
ADT Binary Search Tree Ellen Walker CPSC 201 Data Structures Hiram College.
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.
Copyright © 2012 Pearson Education, Inc. Chapter 20: Binary Trees.
Data Structures: A Pseudocode Approach with C, Second Edition 1 Chapter 7 Objectives Create and implement binary search trees Understand the operation.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 20: 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.
Trees By JJ Shepherd. Introduction Last time we discussed searching and sorting in a more efficient way Divide and Conquer – Binary Search – Merge Sort.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 20: Binary Trees.
18-1 Chapter 18 Binary Trees Data Structures and Design in Java © Rick Mercer.
Binary Search Trees Chapter 7 Objectives
Data Structures and Design in Java © Rick Mercer
Trees Chapter 15.
Data Structure and Algorithms
AA Trees.
B/B+ Trees 4.7.
Binary Tree.
Binary Search Tree (BST)
Lecture 22 Binary Search Trees Chapter 10 of textbook
Section 8.1 Trees.
Trees.
Data Structures & Algorithm Design
ITEC 2620M Introduction to Data Structures
i206: Lecture 13: Recursion, continued Trees
Binary Trees, Binary Search Trees
Chapter 20: Binary Trees.
Chapter 10: Non-linear Data Structures
COSC2100: Data Structures and Algorithms
Introduction to Trees IT12112 Lecture 05.
Chapter 21: Binary Trees.
Binary Trees.
Binary Trees.
Binary Trees, Binary Search Trees
Trees.
Chapter 20: Binary Trees.
Binary Trees.
Trees.
Non-Linear data structures
Binary Trees, Binary Search Trees
Data Structures Using C++ 2E
Chapter 11 Trees © 2011 Pearson Addison-Wesley. All rights reserved.
Data Structures – Binary Tree
Cs212: Data Structures Lecture 7: Tree_Part1
Presentation transcript:

Tonga Institute of Higher Education Binary Trees Tonga Institute of Higher Education

Review of Ordered Arrays and Linked Lists Searching is fast. Binary Search O(logN) Inserting and Deleting is slow. Need to move many values one space over O(N) Linked List Inserting and deleting is fast. Just need to change a few references O(1) Searching is slow Must start from the beginning of the list and check every link.

Introduction to Binary Trees A Binary Tree combines the advantages of ordered arrays and linked lists Like an ordered array, we can: Search for an item quickly Like a linked list, we can: Insert items quickly Delete items quickly

What is a Tree? A tree consists of nodes connected by edges Nodes represent objects Example: Customers, plane tickets, etc. Edges represent references There is 1 node at the top of the tree Trees are small on top and large on bottom It looks like an upside down tree

Tree Terminology - 1 Path – The route you must take to get to a node Root – The top node of the tree Parent – Every node must have one edge running upwards to another node. The node above it is a parent Child – Any node may have one or more edges running downward to another node. The node below it is a child.

Tree Terminology - 2 Leaf – A node with no children Subtree – Any node may be considered to be the root of a subtree, which consists off all the edges and nodes under that node. Visiting – A node is visited when a program control arrives at a node to carry out an operation like checking a value. Just passing over a node on a path from one node to another is not considered to be visiting the node.

Tree Terminology - 3 Traversing – To traverse a tree means to visit all nodes in a specified order. Levels – The level of a node refers to how many edges the node is from the root. Keys – One data field in an object stored in a node usually contains a key value. This value is used to search for the item or perform other operations on it.

Tree Rules This is not a valid tree because there is a node with more than one path to the root The only way to move from one node to another is to follow a path along the lines There must be only one path to the root.

Using Trees in Everyday Life You have been using a Tree for at least half a year already! The file structure in your computer follows these rules! Desktop is the root node Directories are nodes Directories without any subdirectories or files in them are leaves Files are leaves You double-click on a folder to move from one node to another

Binary Trees Binary Trees contain number keys Binary Trees can only have a maximum of 2 children The key in the left child node must be less than or equal to the key in the right child node

Presentation Binary Tree Applet

Node Class We can create an class to represent a node The class can contain data representing the object being stored Employees Items The class must also contain A key value A left node A right node

Code View Node Class

Tree Class We can create an class to represent a tree A tree has only one field: a node that holds the root The tree class can have many methods Find Insert Delete Etc.

Code View Tree Class

Finding a Node Each node can represent an object Each object has a key Customer Item Each object has a key Customer ID Item ID We can find an item easily with these steps At a node, determine whether the key of the object you are looking for is greater than, equal to, or less than the value of the node If the key is less: Move to the left child node and repeat from step 1. If no left child node is available, the node does not exist in this tree. If the key is equal: We have found the node we are looking for If the key is greater: Move to the right child node and repeat from step 1. If no right child node is available, the node does not exist in this tree.

Demonstration Find in Applet

Code View Find Code

Find Efficiency The time required to find a node depends on the level that the node is located This is O(logN) time

Inserting a Node We can insert an item with these steps Find the place to put the node Pretend like we’re looking for a node When we get to the last node and realize that the node we want to insert does not already exist, we add it as a left or right child node

Unbalanced Trees Sometimes, when we add a lot of nodes, our tree becomes very heavy on one side This condition is called an unbalance tree How to handle unbalanced trees is covered in Algorithm Analysis and Design

Demonstration Insert in Applet

Code View Insert Code

Traversing a Tree Traversing – To traverse a tree means to visit all nodes in a specified order. There are 3 ways to traverse a tree Inorder Preorder Postorder

Inorder Traversal - 1 An inorder traversal of a binary search tree will cause all the nodes to be visited in ascending order. To traverse a tree Inorder we repeat the 3 following steps until we have visited every node in the binary tree Go to left child Visit the node Go to right child

Inorder Traversal - 2 Start from the root node Move to right child left child

Inorder, Preorder and Postorder Traversals Inorder Traversal Go to left child Visit the node Go to right child Preorder Traversal Postorder Traversal

Demonstration Traversals in Applet

Code View Traversal Code

Finding Maximum and Minimum Values To get the minimum value, go to the left-most node To get the maximum value, go to the right-most node

Deleting a Node with No Children To delete a node with no children, set the reference of the parent node to be null. The node will eventually be cleaned up by the Java garbage collector.

Deleting a Node with 1 Child To delete a node with 1 child, set the reference of the parent node to be the child of the deleted node.

Deleting a Node with 2 Children - 1 When you are deleting a node with 2 children, we have a problem. How do we know which node to put in the place of the deleted node? In other words, which node is the successor?

Deleting a Node with 2 Children - 2 Subset of the nodes on the right To find the successor we must find the smallest value of the subset of the nodes on the right To do this: Go to the right child of the node to be deleted Then, find the leftmost node

Finding a Successor Node These nodes are the leftmost nodes of the right child! In this case the successor node is the right child of the node to be deleted In this case, the successor node is not the right child of the node to be deleted

Deleting a Node when the Successor is the Right Child of the Node to be Deleted 87 replaces 75 because it was the successor When the successor is the right child of the node to be deleted, replace the deleted node with the successor

Deleting a Node when the Successor is a Left Descendant of Right Child Node of the Node to be Deleted The successor replaces the deleted node The algorithm is redone as if the successor was deleted until the entire tree has been redone

Demonstration Deletions in Applet

Code View Deletion Code

Binary Tree Summary with Driver Code View Binary Tree Summary with Driver