Binary Trees our leafy, annoying friends

Slides:



Advertisements
Similar presentations
Chapter 7. Binary Search Trees
Advertisements

Trees Chapter 8.
ITEC200 – Week08 Trees. 2 Chapter Objectives Students can: Describe the Tree abstract data type and use tree terminology such as.
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.
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.
Trees. 2 Definition of a tree A tree is like a binary tree, except that a node may have any number of children Depending on the needs of the program,
Trees. Definition of a tree A tree is like a binary tree, except that a node may have any number of children –Depending on the needs of the program, the.
Trees. 2 Definition of a tree A tree is a node with a value and zero or more children Depending on the needs of the program, the children may or may not.
Trees CMSC 433 Chapter 8.1 Nelson Padua-Perez Bill Pugh.
Advanced Data Structures and Algorithms COSC-600 Lecture presentation-6.
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.
Introduction Of Tree. Introduction A tree is a non-linear data structure in which items are arranged in sequence. It is used to represent hierarchical.
Spring 2010CS 2251 Trees Chapter 6. Spring 2010CS 2252 Chapter Objectives Learn to use a tree to represent a hierarchical organization of information.
Tree A connected graph that contains no simple circuits is called a tree. Because a tree cannot have a simple circuit, a tree cannot contain multiple.
Trees and Graphs CSE 2320 – Algorithms and Data Structures Vassilis Athitsos University of Texas at Arlington 1.
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.
Data Structures TREES.
Problem of the Day  You drive a bus from Rotterdam to Delft.  At the 1 st stop, 33 people get in.  At the 2 nd stop, 7 add people & 11 passengers leave.
Trees : Part 1 Section 4.1 (1) Theory and Terminology (2) Preorder, Postorder and Levelorder Traversals.
Problem of the Day  Solve this equation by moving the numbers: 76 = 24.
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.
Week 7 - Wednesday.  What did we talk about last time?  Recursive running time  Master Theorem  Symbol tables.
Trees A non-linear implementation for collection classes.
Tree Representation and Terminology Binary Trees Binary Search Trees Pointer-Based Representation of a Binary Tree Array-Based Representation of a Binary.
Binary Trees.
Non Linear Data Structure
Chapter 10 Trees © 2006 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved.
Trees Chapter 15.
Lecture 1 (UNIT -4) TREE SUNIL KUMAR CIT-UPES.
AA Trees.
Binary Trees.
University of Palestine
Source Code for Data Structures and Algorithm Analysis in C (Second Edition) – by Weiss
Problems with Linked List (as we’ve seen so far…)
B+-Trees.
Tree.
Lecture 18. Basics and types of Trees
CHAPTER 4 Trees.
Week 11 - Friday CS221.
Trees.
Trees Trees are a very useful data structure. Many different kinds of trees are used in Computer Science. We shall study just a few of these.
Binary Tree and General Tree
Data Structures and Database Applications Binary Trees in C#
TREES General trees Binary trees Binary search trees AVL trees
Trees.
Trees.
8.2 Tree Traversals Chapter 8 - Trees.
Binary Trees.
Taibah University College of Computer Science & Engineering Course Title: Discrete Mathematics Code: CS 103 Chapter 10 Trees Slides are adopted from “Discrete.
Find in a linked list? first last 7  4  3  8 NULL
Trees.
Binary Trees.
Binary Trees.
A Robust Data Structure
Lecture 12 CS203 1.
Binary Trees CS-2851 Dr. Mark L. Hornick.
CMSC 202 Trees.
Lecture 36 Section 12.2 Mon, Apr 23, 2007
Trees.
Binary Trees.
Trees 11.1 Introduction to Trees Dr. Halimah Alshehri.
CSC 143 Java Trees.
Tree.
Trees.
Binary Trees.
Trees.
Data Structures Using C++ 2E
8.2 Tree Traversals Chapter 8 - Trees.
Trees.
Tree (new ADT) Terminology: A tree is a collection of elements (nodes)
Presentation transcript:

Binary Trees our leafy, annoying friends Annatala Wolf 2231 Lecture 5 1

Remember Trees? (from 2221) Graph: a set of vertices (or nodes) connected by edges Tree: an undirected, connected graph with no cycles Rooted Tree: a tree with one node designated “the root” Rooted trees are undirected, but often a direction toward or away from the root is useful to imagine. Rooted trees are usually ordered (you can tell the difference between each child). Our XMLTree component is a rooted, ordered tree. 2

Visualizing Rooted Trees When we draw rooted trees, we often use the following common conventions: Draw the root of the tree at the top Draw vertices horizontally level based on how many edges they are from the root If the tree is ordered, draw the outgoing (child) edges from left to right in that order. <0, 1, 2…> (if ordered by number) 3

Rooted Tree Terminology Size: total number of nodes. Height: total number of levels (max distance from the root). Leaves are nodes without children. Nodes with children are called internal nodes. 4

Binary Trees A binary tree is a rooted tree where every node has at most two children. Binary trees are also often ordered. This means there is a distinct left child and right child. The Tree of Harmony is not a binary tree. 5

The Order Zero (“Empty”) Tree In mathematics, the order zero graph is a graph with no vertices (or edges) at all. The order zero graph is usually not considered a graph, and almost never considered a tree. However, if we consider the order zero graph to be a tree (the order zero tree, or empty tree), we can use it to simplify the recursive structure of a binary tree. This is why we define the height of a tree as the total number of levels rather than distance from the root (our definition adds 1 to the common). 6

(subtrees might still be empty) Recursive Structure Think of each binary tree as either being empty, or consisting of a root node, a left subtree, and a right subtree. (subtrees might still be empty) ø OR empty tree (0 nodes, 0 edges) non-empty tree 7

What We Gain We no longer have special cases for nodes based on whether they have left, right, both, or neither child. All nodes now have exactly two subtrees. A A B ø B C D C D ø ø E ø E ø ø 8

this will be part of the recursive base case. Thinking Recursively By defining binary trees recursively, it’s much easier to work with them. The only special case we have to handle is the empty tree case, so the interface can be much simpler. Our only special case. Often (but not always), this will be part of the recursive base case. ø empty tree (0 nodes, 0 edges) non-empty tree 9

BinaryTree Intuitively, a BinaryTree is an Iterable collection with one “base” item (the root), and the rest of the items partitioned to the left or right of the root. You can only access the root, or break the tree into the root and its two subtrees. Mathematically, BinaryTree is an ordered binary tree of T. default value: the empty tree 10

Why not direct access? Like XMLTree, BinaryTree does not have a method for breadth-first traversal. When you work with an arbitrary tree, you’ll need to use a depth-first approach. Unlike XMLTree, BinaryTree also requires you to take the tree apart in order to drill down into its subtrees. (This necessitates recursion!) The reasoning behind this is that we don’t need this functionality for what we’ll be using the component. This makes its interface much simpler to work with. 11

Note: the abstract class BinaryTreeSecondary Interfaces and Classes Note: the abstract class BinaryTreeSecondary is not shown here.

BinaryTreeKernel Methods assemble(root, left, right)‏ requires: true replaces this T disassemble(root, left, right)‏ requires: this clears this int size( )‏ requires: true 13

BinaryTree Methods T root()‏ T replaceRoot(newRoot) int height( )‏ requires: this T replaceRoot(newRoot) int height( )‏ requires: true inOrderAssemble(…) (not needed right now) 14

Using BinaryTree Anything you do using BinaryTree will need to be recursive. The only way to work with BinaryTree is to handle one node of the tree at a time. If you ever disassemble a subtree, you’re probably doing it wrong. You must rely upon recursion to do the work for you at lower levels! Binary tree has a recursive structure, and only recursion will correctly exploit it. 15

Preconditions for BinaryTree The only precondition for BinaryTree is: You cannot disassemble(…) or mess with the (non-existent) root of the empty tree. Fortunately, the empty tree is easy to recognize. It is the only tree that has a size (and a height) of zero. Frequently, the empty tree will be the base case for recursion—but not always! Some methods might have “|tree| > 0” as a precondition, so the base case may need to be something different. 16