BINARY TREES A BINARY TREE t IS EITHER EMPTY OR CONSISTS OF AN ITEM, CALLED THE ROOT ITEM, AND TWO DISTINCT BINARY TREES, CALLED THE LEFT SUBTREE AND.

Slides:



Advertisements
Similar presentations
COSC2007 Data Structures II Chapter 10 Trees I. 2 Topics Terminology.
Advertisements

© 2004 Goodrich, Tamassia Binary Search Trees
1 AVL Trees (10.2) CSE 2011 Winter April 2015.
SUNY Oneonta Data Structures and Algorithms Visualization Teaching Materials Generation Group Binary Search Tree A running demonstration of binary search.
Binary Search Trees. A binary search tree is a binary tree that keeps the following property: Every element is larger than all elements in its left sub-tree.
Binary Search Trees Azhar Maqsood School of Electrical Engineering and Computer Sciences (SEECS-NUST)
AA Trees another alternative to AVL trees. Balanced Binary Search Trees A Binary Search Tree (BST) of N nodes is balanced if height is in O(log N) A balanced.
CS 171: Introduction to Computer Science II
Binary Search Trees Briana B. Morrison Adapted from Alan Eugenio.
Department of Computer Science University of Maryland, College Park
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,
1 Trees. 2 Outline –Tree Structures –Tree Node Level and Path Length –Binary Tree Definition –Binary Tree Nodes –Binary Search Trees.
Lists A list is a finite, ordered sequence of data items. Two Implementations –Arrays –Linked Lists.
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.
Version TCSS 342, Winter 2006 Lecture Notes Trees Binary Trees Binary Search Trees.
COSC2007 Data Structures II
Data Structures - CSCI 102 Binary Tree In binary trees, each Node can point to two other Nodes and looks something like this: template class BTNode { public:
Min Chen School of Computer Science and Engineering Seoul National University Data Structure: Chapter 7.
Lecture 10 Trees –Definiton of trees –Uses of trees –Operations on a tree.
S EARCHING AND T REES COMP1927 Computing 15s1 Sedgewick Chapters 5, 12.
Chapter 19 Implementing Trees and Priority Queues Fundamentals of Java.
CISC220 Fall 2009 James Atlas Lecture 13: Trees. Skip Lists.
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 Search Trees Binary Search Trees (BST)  the tree from the previous slide is a special kind of binary tree called a binary.
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.
Binary Search Trees (10.1) CSE 2011 Winter November 2015.
Data Structures Haim Kaplan and Uri Zwick November 2012 Lecture 3 Dynamic Sets / Dictionaries Binary Search Trees.
Binary Search Trees   . 2 Binary Search Tree Properties A binary search tree is a binary tree storing keys (or key-element pairs) at its.
Topics Definition and Application of Binary Trees Binary Search Tree Operations.
Computer Science 112 Fundamentals of Programming II Introduction to Trees.
Tree Traversals, TreeSort 20 February Expression Tree Leaves are operands Interior nodes are operators A binary tree to represent (A - B) + C.
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.
Lecture 91 Data Structures, Algorithms & Complexity Insertion and Deletion in BST GRIFFITH COLLEGE DUBLIN.
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.
1. Iterative Preorder Traversal Rpreorder(T) 1. [process the root node] if T!= NULL then Write Data(T) else Write “empty Tree” 2. [process the left subtree]
Copyright © 2012 Pearson Education, Inc. Chapter 20: Binary Trees.
Binary Tree.
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.
Definitions Read Weiss, 4.1 – 4.2 Implementation Nodes and Links One Arrays Three Arrays Traversals Preorder, Inorder, Postorder K-ary Trees Converting.
BINARY TREES Objectives Define trees as data structures Define the terms associated with trees Discuss tree traversal algorithms Discuss a binary.
Lecture 9 Binary Trees Trees General Definition Terminology
Question 4 Tutorial 8. Part A Insert 20, 10, 15, 5,7, 30, 25, 18, 37, 12 and 40 in sequence into an empty binary tree
Tree Representation and Terminology Binary Trees Binary Search Trees Pointer-Based Representation of a Binary Tree Array-Based Representation of a Binary.
Data Structures and Design in Java © Rick Mercer
AA Trees.
Binary search tree. Removing a node
Trees.
CISC220 Fall 2009 James Atlas Lecture 13: Binary Trees.
Binary Search Tree (BST)
Chapter 10 Search Trees 10.1 Binary Search Trees Search Trees
AVL DEFINITION An AVL tree is a binary search tree in which the balance factor of every node, which is defined as the difference between the heights of.
Binary Search Trees.
Binary Search Tree Chapter 10.
Chapter 10.1 Binary Search Trees
Objective: Understand Concepts related to trees.
Section 8.1 Trees.
Trees.
ITEC 2620M Introduction to Data Structures
Revised based on textbook author’s notes.
Chapter 20: Binary Trees.
Chapter 21: Binary Trees.
Height Balanced Trees 2-3 Trees.
Chapter 12: Binary Search Trees
CMSC 202 Trees.
Lecture 36 Section 12.2 Mon, Apr 23, 2007
Trees.
Presentation transcript:

BINARY TREES

A BINARY TREE t IS EITHER EMPTY OR CONSISTS OF AN ITEM, CALLED THE ROOT ITEM, AND TWO DISTINCT BINARY TREES, CALLED THE LEFT SUBTREE AND RIGHT SUBTREE OF t.

height(t) = ? TO DISTINGUISH THE HEIGHT OF AN EMPTY TREE FROM THE HEIGHT OF A SINGLE-ITEM TREE, WHAT SHOULD THE HEIGHT OF AN EMPTY TREE BE?

ABCDERSFGL THIS ASSOCIATION SUGGESTS THAT A COMPLETE BINARY TREE CAN BE STORED IN AN ARRAY:

EXTERNAL PATH LENGTH

TRAVERSALS OF A BINARY TREE

HERE ARE THE STEPS IF THE CALL IS find (60) :

HERE ARE THE STEPS IF THE CALL IS find (70):

insert (73 )

WILL THE INSERTED ITEM ALWAYS BE A LEAF?

SUPPOSE link IS POINTING TO THE NODE WITH

WHAT IF link IS POINTING TO THE NODE WITH 40?

SUPPOSE link IS POINTING TO 80’S NODE:

THE ITEM 80 HAS TWO CHILDREN, SO WE CANNOT SIMPLY UNLINK 80 FROM THE TREE: THAT WOULD CREATE A HOLE. OF THE ITEMS ALREADY IN THE TREE, WHICH TWO COULD REPLACE 80 WITHOUT DESTROYING THE STRUCTURE OF THE TREE?

WE CAN REPLACE 80 WITH EITHER ITS PREDECESSOR, 75, OR ITS SUCCESSOR, 85. WE’LL CHOOSE ITS SUCCESSOR. THE SUCCESSOR OF AN ITEM IS THE LEFTMOST ITEM IN THE RIGHT SUBTREE. REPLACE 80 WITH 85, AND THEN REMOVE 85.

HERE IS THE RESULTING TREE:

CAN REMOVING THE SUCCESSOR GET COMPLICATED? CAN THE SUCCESSOR HAVE TWO CHILDREN?

RECALL THAT link IS THE ONLY FIELD IN THE Iterator CLASS. if (link has a right child) // make link point to the leftmost node in link’s right subtree else // go up the tree to the left as far as possible, then go up // to the right. Make link point to that ancestor.