Tree.

Slides:



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

Trees Types and Operations
Algorithms and Data Structures Lecture 4. Agenda: Trees – fundamental notions, variations Binary search tree.
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.
Binary Trees, Binary Search Trees CMPS 2133 Spring 2008.
Binary Trees, Binary Search Trees COMP171 Fall 2006.
CS 171: Introduction to Computer Science II
Trees Chapter 8.
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,
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.
1 Trees. 2 Outline –Tree Structures –Tree Node Level and Path Length –Binary Tree Definition –Binary Tree Nodes –Binary Search Trees.
Data Structures Data Structures Topic #8. Today’s Agenda Continue Discussing Table Abstractions But, this time, let’s talk about them in terms of new.
© 2006 Pearson Addison-Wesley. All rights reserved11 A-1 Chapter 11 Trees.
Data Structures Using C++ 2E Chapter 11 Binary Trees and B-Trees.
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
Binary Trees Chapter 6.
By : Budi Arifitama Pertemuan ke Objectives Upon completion you will be able to: Create and implement binary search trees Understand the operation.
Index Structures for Files Indexes speed up the retrieval of records under certain search conditions Indexes called secondary access paths do not affect.
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.
CHAPTER 71 TREE. Binary Tree A binary tree T is a finite set of one or more nodes such that: (a) T is empty or (b) There is a specially designated node.
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.
Min Chen School of Computer Science and Engineering Seoul National University Data Structure: Chapter 7.
1 COP 3538 Data Structures with OOP Chapter 8 - Part 2 Binary Trees.
Chapter 19: Binary Trees. Objectives In this chapter, you will: – Learn about binary trees – Explore various binary tree traversal algorithms – Organize.
Lecture 81 Data Structures, Algorithms & Complexity Tree Algorithms GRIFFITH COLLEGE DUBLIN.
Lecture 10 Trees –Definiton of trees –Uses of trees –Operations on a tree.
1 CSE 1342 Programming Concepts Trees. 2 Basic Terminology Trees are made up of nodes and edges. A tree has a single node known as a root. –The root is.
Spring 2010CS 2251 Trees Chapter 6. Spring 2010CS 2252 Chapter Objectives Learn to use a tree to represent a hierarchical organization of information.
Binary Trees. Binary Tree Finite (possibly empty) collection of elements A nonempty binary tree has a root element The remaining elements (if any) are.
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.
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.
© 2010 Pearson Addison-Wesley. All rights reserved. Addison Wesley is an imprint of CHAPTER 12: Multi-way Search Trees Java Software Structures: Designing.
Topics Definition and Application of Binary Trees Binary Search Tree Operations.
Review 1 Queue Operations on Queues A Dequeue Operation An Enqueue Operation Array Implementation Link list Implementation Examples.
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.
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.
M180: Data Structures & Algorithms in Java Trees & Binary Trees Arab Open University 1.
Binary Tree. Some Terminologies Short review on binary tree Tree traversals Binary Search Tree (BST)‏ Questions.
Data Structures Using C++ 2E Chapter 11 Binary Trees.
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.
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.
18-1 Chapter 18 Binary Trees Data Structures and Design in Java © Rick Mercer.
Chapter 8 (Lafore’s Book) Binary Tree Hwajung Lee.
Tree Representation and Terminology Binary Trees Binary Search Trees Pointer-Based Representation of a Binary Tree Array-Based Representation of a Binary.
Binary Search Trees Chapter 7 Objectives
Data Structures and Design in Java © Rick Mercer
Data Structure and Algorithms
ITEC324 Principle of CS III
Binary Tree.
Binary Search Tree (BST)
Section 8.1 Trees.
Trees.
Tonga Institute of Higher Education
i206: Lecture 13: Recursion, continued Trees
Data Structures Using C++ 2E
Binary Trees, Binary Search Trees
Introduction to Trees IT12112 Lecture 05.
Binary Trees, Binary Search Trees
Chapter 20: Binary Trees.
Trees.
Binary Trees, Binary Search Trees
Data Structures Using C++ 2E
Presentation transcript:

Tree

Why Use Binary Trees? It combines the advantages of two other structures: an ordered array and a linked list. You can search a tree quickly, as you can an ordered array, and you can also insert and delete items quickly, as you can with a linked list. you can quickly search such an array for a particular value, using a binary search. insertions and deletions are quick to perform on a linked list. They are accomplished simply by changing a few references

What Is a Tree? A tree consists of nodes connected by edges In computer programs, nodes often represent such entities as people, car parts, airline reservations, and so on The lines (edges) between the nodes represent the way the nodes are related

Tree Terminology similar to real-world trees or to family relationships (as in parents and children),

Tree Terminology (con’t) Path Think of someone walking from node to node along the edges that connect them. Root The node at the top of the tree is called the root. There is only one root in a tree. For a collection of nodes and edges to be defined as a tree, there must be one (and only one!) path from the root to any other node.

Tree Terminology (con’t) Parent Any node (except the root) has exactly one edge running upward to another node. The node above it is called the parent of the node. Child Any node may have one or more lines running downward to other nodes. These nodes below a given node are called its children. Leaf A node that has no children is called a leaf node or simply a leaf. There can be only one root in a tree, but there can be many leaves.

Tree Terminology (con’t) Subtree Any node may be considered to be the root of a subtree, which consists of its children, and its children’s children, and so on. If you think in terms of families, a node’s subtree contains all its descendants. Visiting A node is visited when program control arrives at the node, usually for the purpose of carrying out some operation on the node, such as checking the value of one of its data fields or displaying it. Merely passing over a node on the path from one node to another is not considered to be visiting the node. Traversing To traverse a tree means to visit all the nodes in some specified order. For example, you might visit all the nodes in order of ascending key value.

Tree Terminology (con’t) Levels The level of a particular node refers to how many generations the node is from the root. If we assume the root is Level 0, then its children will be Level 1, its grandchildren will be Level 2, and so on. Keys One data field in an object is usually designated a key value. This value is used to search for the item or perform other operations on it. In tree diagrams, when a circle represents a node holding a data item, the key value of the item is typically shown in the circle.

Binary Trees If every node in a tree can have at most two children, the tree is called a binary tree The two children of each node in a binary tree are called the left child and the right child The defining characteristic of a binary search tree is this: A node’s left child must have a key less than its parent, and a node’s right child must have a key greater than or equal to its parent.

The Node Class Node objects contain the data representing the objects being stored (employees in an employee database, for example) and also references to each of the node’s two children.

Finding a Node

Inserting a Node To insert a node, we must first find the place to insert it Let’s assume we’re going to insert a new node with the value 45 The value 45 is less than 60 but greater than 40, so we arrive at node 50. Now we want to go left because 45 is less than 50, but 50 has no left child; its leftChild fieldis null. When it sees this null, the insertion routine has found the place to attach the new node

Inserting a Node (con’t)

Traversing the Tree Inorder Traversal An inorder traversal of a binary search tree will cause all the nodes to be visited in ascending order, based on their key values. The method needs to do only three things: 1. Call itself to traverse the node’s left subtree. 2. Visit the node. 3. Call itself to traverse the node’s right subtree. Remember that visiting a node means doing something to it: displaying it, writing it to a file, or whatever.

Traversing the Tree (con’t)

Traversing the Tree (con’t)

Finding Maximum and Minimum Values For the minimum, go to the left child of the root; then go to the left child of that child, and so on, until you come to a node that has no left child. This node is the minimum For the maximum value in the tree, follow the same procedure, but go from right child to right child until you find a node with no right child. This node is the maximum. The code is the same except that the last statement in the loop is current = current.rightChild; // go to right child

Deleting a Node You start by finding the node you want to delete, using the same approach we saw in find() and insert(). When you’ve found the node, there are three cases to consider: 1. The node to be deleted is a leaf (has no children). 2. The node to be deleted has one child. 3. The node to be deleted has two children.

Case 1: The Node to Be Deleted Has No Children After we’ve found the node, we check first to verify that it has no children. When this is true, we check the special case of the root. If that’s the node to be deleted, we simply set it to null; this empties the tree. Otherwise, we set the parent’s leftChild or rightChild field to null to disconnect the parent from the node.

Case 2: The Node to Be Deleted Has One Child There are four variations: The child of the node to be deleted may be either a left or right child, and for each of these cases the node to be deleted may be either the left or right child of its parent. There is also a specialized situation: the node to be deleted may be the root, in which case it has no parent and is simply replaced by the appropriate subtree

Case 2: The Node to Be Deleted Has One Child

Case 3: The Node to Be Deleted Has Two Children To delete a node with two children, replace the node with its inorder successor. For each node, the node with the next-highest key is called its inorder successor, or simply its successor.

Finding the Successor First, the program goes to the original node’s right child, which must have a key larger than the node. Then it goes to this right child’s left child (if it has one), and to this left child’s left child, and so on, following down the path of left children. The last left child in this path is the successor of the original node

Why does this algorithm work? What we’re really looking for is the smallest of the set of nodes that are larger than the original node If the right child of the original node has no left children, this right child is itself the successor

Java Code to Find the Successor

Successor Is Right Child of delNode If successor is the right child of current, things are simplified somewhat. This operation requires only two steps: Unplug current from the rightChild field of its parent (or leftChild field if appropriate), and set this field to point to successor. Unplug current’s left child from current, and plug it into the leftChild field of Successor.

Successor Is Left Descendant of Right Child of delNode Plug the right child of successor into the leftChild field of the successor’s parent. Plug the right child of the node to be deleted into the rightChild field of successor. Unplug current from the rightChild field of its parent, and set this field to point to successor. Unplug current’s left child from current, and plug it into the leftChild field of successor Steps 1 and 2 are handled in the getSuccessor() routine, while 3 and 4 are carried out in delete()

Successor Is Left Descendant of Right Child of delNode (con’t)

The Complete Delete Method

The Complete Delete Method