Data Structures Binary Trees 1.

Slides:



Advertisements
Similar presentations
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.
Advertisements

Binary Trees, Binary Search Trees CMPS 2133 Spring 2008.
Lists A list is a finite, ordered sequence of data items. Two Implementations –Arrays –Linked Lists.
© 2006 Pearson Addison-Wesley. All rights reserved11 A-1 Chapter 11 Trees.
Trees CMSC 433 Chapter 8.1 Nelson Padua-Perez Bill Pugh.
C++ Programming: Program Design Including Data Structures, Third Edition Chapter 20: Binary Trees.
C o n f i d e n t i a l HOME NEXT Subject Name: Data Structure Using C Unit Title: Trees.
Binary Trees Chapter 6.
Chapter 18 - basic definitions - binary trees - tree traversals Intro. to Trees 1CSCI 3333 Data Structures.
1 Chapter 18 Trees Objective To learn general trees and recursion binary trees and recursion tree traversal.
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.
Chapter 19: Binary Trees. Objectives In this chapter, you will: – Learn about binary trees – Explore various binary tree traversal algorithms – Organize.
Lecture 10 Trees –Definiton of trees –Uses of trees –Operations on a tree.
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 Traversals, TreeSort 20 February Expression Tree Leaves are operands Interior nodes are operators A binary tree to represent (A - B) + C.
Trees By P.Naga Srinivasu M.tech,(MBA). Basic Tree Concepts A tree consists of finite set of elements, called nodes, and a finite set of directed lines.
M180: Data Structures & Algorithms in Java Trees & Binary Trees Arab Open University 1.
CMSC 341 Introduction to Trees. 2/21/20062 Tree ADT Tree definition –A tree is a set of nodes which may be empty –If not empty, then there is a distinguished.
Binary Tree. Some Terminologies Short review on binary tree Tree traversals Binary Search Tree (BST)‏ Questions.
DATA STRUCTURE BS(IT)3rd. Tree An Introduction By Yasir Mustafa Roll No. BS(IT) Bahauddin Zakariya University, Multan.
1 Trees General Trees  Nonrecursive definition: a tree consists of a set of nodes and a set of directed edges that connect pairs of nodes.
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.
TREES General trees Binary trees Binary search trees AVL trees Balanced and Threaded trees.
1 Trees General Trees  Nonrecursive definition: a tree consists of a set of nodes and a set of directed edges that connect pairs of nodes.
Trees By JJ Shepherd. Introduction Last time we discussed searching and sorting in a more efficient way Divide and Conquer – Binary Search – Merge Sort.
DS.T.1 Trees Chapter 4 Overview Tree Concepts Traversals Binary Trees Binary Search Trees AVL Trees Splay Trees B-Trees.
SUYASH BHARDWAJ FACULTY OF ENGINEERING AND TECHNOLOGY GURUKUL KANGRI VISHWAVIDYALAYA, HARIDWAR.
Binary Trees.
CSE 373 Data Structures Lecture 7
Trees Saurav Karmakar
Chapter 12 – Data Structures
Non Linear Data Structure
Trees Chapter 15.
Data Structure and Algorithms
CC 215 Data Structures Trees
Lecture 1 (UNIT -4) TREE SUNIL KUMAR CIT-UPES.
Binary Trees.
Recursive Objects (Part 4)
Week 6 - Wednesday CS221.
Binary Search Tree (BST)
Tree.
Section 8.1 Trees.
Data Structures & Algorithm Design
CSE 373 Data Structures Lecture 7
ITEC 2620M Introduction to Data Structures
i206: Lecture 13: Recursion, continued Trees
Data Structures Using C++ 2E
Binary Trees, Binary Search Trees
Binary Tree and General Tree
TREES General trees Binary trees Binary search trees AVL trees
CS223 Advanced Data Structures and Algorithms
Binary Trees.
Binary Trees.
Binary Trees.
Trees CMSC 202, Version 5/02.
CSE 373, Copyright S. Tanimoto, 2002 Binary Trees -
CMSC 202 Trees.
Binary Trees, Binary Search Trees
CE 221 Data Structures and Algorithms
Trees.
Binary Trees.
CSE 373, Copyright S. Tanimoto, 2001 Binary Trees -
Chapter 20: Binary Trees.
Binary Trees.
Binary Trees, Binary Search Trees
Data Structures Using C++ 2E
Chapter 11 Trees © 2011 Pearson Addison-Wesley. All rights reserved.
NATURE VIEW OF A TREE leaves branches root. NATURE VIEW OF A TREE leaves branches root.
Presentation transcript:

Data Structures Binary Trees 1

(Maximum level of the tree) leaves or nodes Level=1 Root Level = 0 number of leaves: ?? leaves Depth (Maximum level of the tree) leaves or nodes Level=1 height Level=2 root 2 57 feet Number of nodes: 2(2+1) - 1 = 7 CIS265/506: Chapter 08 Intro to Binary Trees 2

CIS265/506: Chapter 08 Intro to Binary Trees Trees – Levels - Nodes Max. number of leaves/nodes: 2(d+1) - 1 where d = depth (assume the tree is full (complete)) The level of a node is its distance from the root. The depth of a tree is the level of the leaf in the longest path from the root plus one. A strictly binary tree: every non-leaf node has nonempty left & right sub trees CIS265/506: Chapter 08 Intro to Binary Trees

CIS265/506: Chapter 08 Intro to Binary Trees Trees – Levels - Nodes A complete binary tree of depth d has all leaves at level d An almost complete binary tree of depth d is such that: Any node at a level less than (d-1) has 2 sons For any node with a right descendant at level d, it must have a left son and every left descendant is either a leaf at level d or has 2 sons. A tree can be strictly binary, but not almost complete Conversely, a tree can be almost complete, but not strictly binary. CIS265/506: Chapter 08 Intro to Binary Trees

CIS265/506: Chapter 08 Intro to Binary Trees A binary tree is a tree whose nodes have children (possibly empty) and each child is either a left child or a right child. Each circle is a node. The circle right below it (and attached by the arrow) is the child (or leaf). Each child is either the left or right child. Each child can have only one parent, and each parent can have only two children. Each path (from node to node) is a straight line. There are no cycles in binary trees. CIS265/506: Chapter 08 Intro to Binary Trees 3

CIS265/506: Chapter 08 Intro to Binary Trees This is a tree, but not a binary tree. There are 2 nodes that have three children. CIS265/506: Chapter 08 Intro to Binary Trees 4

Are these binary trees? Why or why not?? CIS265/506: Chapter 08 Intro to Binary Trees 5

CIS265/506: Chapter 08 Intro to Binary Trees Why do we have binary trees? Improve speed when searching a long list Trees are “naturally ordered” - information is placed in a logical location so it can be found easier. 1 2 3 4 5 3 4 2 5 1 CIS265/506: Chapter 08 Intro to Binary Trees 6

Implementing a Binary Tree Trees can be implemented as either arrays or dynamic structures, similar to linked lists. We will only look at the dynamic implementation - the array implementation can be difficult to manage. left info right Left & right are pointers to nodes (children) in the tree. Info is where your data are stored. CIS265/506: Chapter 08 Intro to Binary Trees 7

Conceptual View of a Tree data *left *right data data *left *right *left *right data data data data *left *right *left *right *left *right *left *right CIS265/506: Chapter 08 Intro to Binary Trees

Binary Trees - Ordering Trees can be ordered in many ways. The most common are: Hierarchical: Highest to lowest. Example - A university has multiple campuses, which have multiple colleges, which have many departments, etc. Chronological: The order in the tree is determined by when the item was placed in the tree. Search Trees (Alternately called ordered trees): A formal definition: if a node n of the tree contains the value v, then all the values stored in the left subtree are less than v, and all the values in the right subtree are greater than v. Duplicate values are not allowed. CIS265/506: Chapter 08 Intro to Binary Trees 2

CIS265/506: Chapter 08 Intro to Binary Trees Traversing a tree Three important ways to visit every node of a tree. INORDER Inorder visits the left child, root, then right child PREORDER Preorder visits the root, the left child, and then the right child POSTORDER Postorder visits the left child, the right child, and finally the root. CIS265/506: Chapter 08 Intro to Binary Trees 12

CIS265/506: Chapter 08 Intro to Binary Trees Tree Traversal The mechanics of visiting nodes could be accomplished by either: an explicitly navigation (I will do everything by myself – approach), or using recursion (let the system help me). The explicit method requires you to define and use a stack - all data points are pushed on the stack as we visit them, and we then pop each element from the stack to view the data. This could be tedious and expensive in terms of time & space. It also requires the additional data structure of at least one generic stack to do any tree manipulations. CIS265/506: Chapter 08 Intro to Binary Trees 13

CIS265/506: Chapter 08 Intro to Binary Trees Sample Tree Structure Tree (A pointer to the root) Left Sub Tree Right Sub Tree CIS265/506: Chapter 08 Intro to Binary Trees

CIS265/506: Chapter 08 Intro to Binary Trees Examining One Node tree info tree  left tree  right CIS265/506: Chapter 08 Intro to Binary Trees

CIS265/506: Chapter 08 Intro to Binary Trees Tree Traversal The other (more elegant) visitation method is based on recursion. This requires a bit of extra effort on your part when debugging, but the code is much shorter and simpler to write. The algorithm for INORDER Traversals: Assume the tree exists Visit the left subtree Visit the root Visit the right subtree CIS265/506: Chapter 08 Intro to Binary Trees 14

CIS265/506: Chapter 08 Intro to Binary Trees Let’s Look at the Code // Inorder visits: // the left child, root, then right child void inOrderTraversal(NodePtr tree) { if (tree != NULL) { inOrderTraversal(tree->left); System.out.println(tree->info); inOrderTraversal(tree->right); } } CIS265/506: Chapter 08 Intro to Binary Trees 15

CIS265/506: Chapter 08 Intro to Binary Trees A Very Simple Case B blue A C red black Is the tree NULL? No, it does exist “inOrderTraversal(tree->left)” - Visit the red (A) node: We call the procedure again using the red (A) node as the tree. There is no left node of red (A) , so we print the root. Now, there is no right node of red (A) , so we exit the recursive call. “System.out.println(tree->info);” - We print the blue (B) node. CIS265/506: Chapter 08 Intro to Binary Trees 16

A Simple Case (Continued) B blue A C red black “inOrderTraversal(tree->right)” - Visit the black (C) node: We call the procedure again using the black (C) node as the tree. There is no left node of black (C) , so we print the root. Now, there is no right node of black (C) , so we exit the recursive call. “/* end if */” - We are done. Our output is: red (A) blue (B) black (C) CIS265/506: Chapter 08 Intro to Binary Trees 17

Let’s do one that is more difficult B E A D F A B C D E F CIS265/506: Chapter 08 Intro to Binary Trees 18

CIS265/506: Chapter 08 Intro to Binary Trees PreOrder Traversal Preorder visits the root, the left child, and then the right child The algorithm for PREORDER Traversals: Assume the tree exists Visit the root Visit the left subtree Visit the right subtree CIS265/506: Chapter 08 Intro to Binary Trees 19

CIS265/506: Chapter 08 Intro to Binary Trees Let’s Look at the Code // Preorder visits // the root, left child, then right child void preOrderTraversal(NODEPTR tree) { if (tree != NULL) { System.out.println(tree->info); preOrderTraversal(tree->left); preOrderTraversal(tree->right); } } CIS265/506: Chapter 08 Intro to Binary Trees 20

CIS265/506: Chapter 08 Intro to Binary Trees A Very Simple Case B blue A C red black Is the tree NULL? No, it does exist “System.out.println(tree->info);” - We print the blue (B) node. “preOrderTraversal(tree->left)” - Visit the red (A) node: We call the procedure again using the red (A) node as the tree. We print the root (red (A) ) and note that there is no left node of red (A) or no right node of red (A), so we exit the recursive call. CIS265/506: Chapter 08 Intro to Binary Trees 21

A Very Simple Case (Continued) B blue A C red black “preOrderTraversal(tree->right)” - Visit the black (C) node: We call the procedure again using the black (C) node as the tree. We print the root (black (C) ) and note that there is no left node of black (C) or no right node of black (C), so we exit the recursive call. “/* end if */” - We are done. Our output is: blue (B) red (A) black (C) CIS265/506: Chapter 08 Intro to Binary Trees 22

CIS265/506: Chapter 08 Intro to Binary Trees Let’s do one that is more difficult C B E A D F C B A E D F CIS265/506: Chapter 08 Intro to Binary Trees 23

CIS265/506: Chapter 08 Intro to Binary Trees PostOrder Traversal Postorder visits the the left child, the right child, then the root The algorithm for POSTORDER Traversals: Assume the tree exists Visit the left subtree Visit the right subtree Visit the root CIS265/506: Chapter 08 Intro to Binary Trees 24

CIS265/506: Chapter 08 Intro to Binary Trees Let’s Look at the Code // Postorder visits // left child, right child, then the root void postOrderTraversal(NODEPTR tree) { if (tree != NULL) { postOrderTraversal(tree->left); postOrderTraversal(tree->right); System.out.println(tree->info); } } CIS265/506: Chapter 08 Intro to Binary Trees 25

CIS265/506: Chapter 08 Intro to Binary Trees A Very Simple Case B blue A C red black Is the tree NULL? No, it does exist “postOrderTraversal(tree->left)” - Visit the red (A) node: We call the procedure again using the red (A) node as the tree. We note that there is no left node of red (A) or no right node of red, so we print the root (red (A) ) and exit the recursive call. CIS265/506: Chapter 08 Intro to Binary Trees 26

CIS265/506: Chapter 08 Intro to Binary Trees A Very Simple Case (Continued) B blue A C red black “postOrderTraversal(tree->right)” - Visit the black (C) node: We call the procedure again using the black (C) node as the tree. We note that there is no left node of black (C) or no right node of black (C), so we print the root (black (C) ) and exit the recursive call. “System.out.println(tree->info);” - We print the blue(B) node. “/* end if */” - We are done. Our output is: red (A) black (C) blue (B) CIS265/506: Chapter 08 Intro to Binary Trees 27

CIS265/506: Chapter 08 Intro to Binary Trees Let’s do one that is more difficult C B E A D F A B D F E C CIS265/506: Chapter 08 Intro to Binary Trees 28