Trees 2: Section 4.2 and 4.3 Binary trees. Binary Trees Definition: A binary tree is a rooted tree in which no vertex has more than two children 3 456.

Slides:



Advertisements
Similar presentations
Chapter 12 Binary Search Trees
Advertisements

Chapter 10: Trees. Definition A tree is a connected undirected acyclic (with no cycle) simple graph A collection of trees is called forest.
S. Sudarshan Based partly on material from Fawzi Emad & Chau-Wen Tseng
1 abstract containers hierarchical (1 to many) graph (many to many) first ith last sequence/linear (1 to 1) set.
Main Index Contents 11 Main Index Contents Week 6 – Binary Trees.
Data Structures and Algorithms1 Trees The definitions for this presentation are from from: Corman, et. al., Introduction to Algorithms (MIT Press), Chapter.
Chapter 4: Trees General Tree Concepts Binary Trees Lydia Sinapova, Simpson College Mark Allen Weiss: Data Structures and Algorithm Analysis in Java.
Binary Trees A binary tree is made up of a finite set of nodes that is either empty or consists of a node called the root together with two binary trees,
© 2006 Pearson Addison-Wesley. All rights reserved11 A-1 Chapter 11 Trees.
4/17/2017 Section 9.3 Tree Traversal ch9.3.
Unit 11a 1 Unit 11: Data Structures & Complexity H We discuss in this unit Graphs and trees Binary search trees Hashing functions Recursive sorting: quicksort,
1 abstract containers hierarchical (1 to many) graph (many to many) first ith last sequence/linear (1 to 1) set.
1 Trees 3: The Binary Search Tree Section Binary Search Tree A binary tree B is called a binary search tree iff: –There is an order relation
Trees and Tree Traversals Prof. Sin-Min Lee Department of Computer Science San Jose State University.
Chapter Chapter Summary Introduction to Trees Applications of Trees (not currently included in overheads) Tree Traversal Spanning Trees Minimum.
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.
Review Binary Tree Binary Tree Representation Array Representation Link List Representation Operations on Binary Trees Traversing Binary Trees Pre-Order.
Chapter 19: Binary Trees. Objectives In this chapter, you will: – Learn about binary trees – Explore various binary tree traversal algorithms – Organize.
CS Data Structures Chapter 5 Trees. Chapter 5 Trees: Outline  Introduction  Representation Of Trees  Binary Trees  Binary Tree Traversals 
LOGO.  Trees:  In these slides: Introduction to trees Applications of trees Tree traversal 2.
1 Trees 2 Binary trees Section Binary Trees Definition: A binary tree is a rooted tree in which no vertex has more than two children –Left and.
S EARCHING AND T REES COMP1927 Computing 15s1 Sedgewick Chapters 5, 12.
CISC220 Fall 2009 James Atlas Lecture 13: Trees. Skip Lists.
1 Trees A tree is a data structure used to represent different kinds of data and help solve a number of algorithmic problems Game trees (i.e., chess ),
Binary Trees 2 Overview Trees. Terminology. Traversal of Binary Trees. Expression Trees. Binary Search Trees.
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.
Compiled by: Dr. Mohammad Omar Alhawarat
Prof. Amr Goneid, AUC1 CSCE 210 Data Structures and Algorithms Prof. Amr Goneid AUC Part 4. Trees.
Preview  Graph  Tree Binary Tree Binary Search Tree Binary Search Tree Property Binary Search Tree functions  In-order walk  Pre-order walk  Post-order.
Discrete Structures Trees (Ch. 11)
Chap 8 Trees Def 1: A tree is a connected,undirected, graph with no simple circuits. Ex1. Theorem1: An undirected graph is a tree if and only if there.
Trees : Part 1 Section 4.1 (1) Theory and Terminology (2) Preorder, Postorder and Levelorder Traversals.
Tree Traversals, TreeSort 20 February Expression Tree Leaves are operands Interior nodes are operators A binary tree to represent (A - B) + C.
Chapter 4: Trees Part I: General Tree Concepts Mark Allen Weiss: Data Structures and Algorithm Analysis in Java.
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.
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.
Trees 3 The Binary Search Tree Section 4.3. Binary Search Tree Also known as Totally Ordered Tree Definition: A binary tree B is called a binary search.
Binary Search Trees (BST)
1 Trees 2 Binary trees Section Binary Trees Definition: A binary tree is a rooted tree in which no vertex has more than two children –Left and.
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.
Binary Tree Implementation. Binary Search Trees (BST) Nodes in Left subtree has smaller values Nodes in right subtree has bigger values.
Data Structures Azhar Maqsood School of Electrical Engineering and Computer Sciences (SEECS-NUST) Binary 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.
1 Trees : Part 1 Reading: Section 4.1 Theory and Terminology Preorder, Postorder and Levelorder Traversals.
1 Trees. 2 Trees Trees. Binary Trees Tree Traversal.
1 Trees 3: The Binary Search Tree Reading: Sections 4.3 and 4.6.
DS.T.1 Trees Chapter 4 Overview Tree Concepts Traversals Binary Trees Binary Search Trees AVL Trees Splay Trees B-Trees.
TREES From root to leaf. Trees  A tree is a non-linear collection  The elements are in a hierarchical arrangement  The elements are not accessible.
Prof. Amr Goneid, AUC1 CSCE 210 Data Structures and Algorithms Prof. Amr Goneid AUC Part 4. Trees.
CSCE 210 Data Structures and Algorithms
Recursive Objects (Part 4)
CISC220 Fall 2009 James Atlas Lecture 13: Binary Trees.
Week 6 - Wednesday CS221.
Binary Search Tree (BST)
Source Code for Data Structures and Algorithm Analysis in C (Second Edition) – by Weiss
Tree.
Section 8.1 Trees.
abstract containers sequence/linear (1 to 1) hierarchical (1 to many)
Trees 3: The Binary Search Tree
Trees 1: Theory, Models, Generic Heap Algorithms, Priority Queues
Abstract Data Structures
Chapter 12: Binary Search Trees
CSE 373, Copyright S. Tanimoto, 2002 Binary Trees -
Section 9.3 by Andrew Watkins
Binary Trees, Binary Search Trees
CSE 373, Copyright S. Tanimoto, 2001 Binary Trees -
Trees.
General Tree Concepts Binary Trees
Binary Trees, Binary Search Trees
A Binary Tree is a tree in which each node has at most 2 children
Presentation transcript:

Trees 2: Section 4.2 and 4.3 Binary trees

Binary Trees Definition: A binary tree is a rooted tree in which no vertex has more than two children root 7 1 2

Example: Expression Tree How do you construct an expression tree from a postfix expression? E.g a b + c d e + * *

Binary Trees Definition: A binary tree is complete iff every layer but the bottom is fully populated with vertices root

Binary Trees A complete binary tree with n vertices and height H satisfies:  2 H ≤ n < 2 H + 1  2 2 ≤ 7 < root

Binary Trees A complete binary tree with n vertices and height H satisfies:  2 H ≤ n < 2 H + 1  H ≤ log n < H + 1  H = floor(log n)

Binary Trees Theorem: In a complete binary tree with n vertices and height H  2 H ≤ n < 2 H + 1

Binary Trees Proof:  At level k ≤ H-1, there are 2 k vertices  At level k = H, there are at most 2 H vertices  Total number of vertices: n = …2 k n = …2 k (Geometric Progression) n = (2 k + 1 – 1) / (2-1) n = 2 k

Binary Trees Let k = H  n = 2 H + 1 – 1  n < 2 H + 1 Let k = H – 1  n’ = 2 H – 1  n ≥ n’ + 1 = 2 H 2 H ≤ n < 2 H + 1

Binary Tree Traversals Inorder traversal  Definition: left child, visit, right child (recursive)  Algorithm: depth-first search (visit between children)

Inorder traversal 1root

Binary Tree Traversals Other traversals apply to binary case:  Preorder traversal vertex, left subtree, right subtree  Inorder traversal left subtree, vertex, right subtree  Postorder traversal left subtree, right subtree, vertex  Levelorder traversal vertex, left children, right children

Vector Representation of Complete Binary Tree Tree data  Vector elements carry data Tree structure  Vector indices carry tree structure  Index order = levelorder  Tree structure is implicit  Uses integer arithmetic for tree navigation

Vector Representation of Complete Binary Tree Tree navigation  Parent of v[k] = v[(k – 1)/2]  Left child of v[k] = v[2*k + 1]  Right child of v[k] = v[2*k + 2] 0 lr lllrrrrl root

Vector Representation of Complete Binary Tree Tree navigation  Parent of v[k] = v[(k – 1)/2]  Left child of v[k] = v[2*k + 1]  Right child of v[k] = v[2*k + 2]

Vector Representation of Complete Binary Tree Tree navigation  Parent of v[k] = v[(k – 1)/2]  Left child of v[k] = v[2*k + 1]  Right child of v[k] = v[2*k + 2] 0l

Vector Representation of Complete Binary Tree Tree navigation  Parent of v[k] = v[(k – 1)/2]  Left child of v[k] = v[2*k + 1]  Right child of v[k] = v[2*k + 2] 0lr

Vector Representation of Complete Binary Tree Tree navigation  Parent of v[k] = v[(k – 1)/2]  Left child of v[k] = v[2*k + 1]  Right child of v[k] = v[2*k + 2] 0lrll

Vector Representation of Complete Binary Tree Tree navigation  Parent of v[k] = v[(k – 1)/2]  Left child of v[k] = v[2*k + 1]  Right child of v[k] = v[2*k + 2] 0lrlllr

Vector Representation of Complete Binary Tree Tree navigation  Parent of v[k] = v[(k – 1)/2]  Left child of v[k] = v[2*k + 1]  Right child of v[k] = v[2*k + 2] 0lrlllrrl

Vector Representation of Complete Binary Tree Tree navigation  Parent of v[k] = v[(k – 1)/2]  Left child of v[k] = v[2*k + 1]  Right child of v[k] = v[2*k + 2] 0lrlllrrlrr

1375 Binary Search Tree Also known as Totally Ordered Tree Definition: A binary tree B is called a binary search tree iff:  There is an order relation ≤ defined for the vertices of B  For any vertex v, and any descendant u of v.left, u ≤ v  For any vertex v, and any descendent w of v.right, v ≤ w 4root 26

Binary Search Tree Consequences:  The smallest element in a binary search tree (BST) is the “left-most” node  The largest element in a BST is the “right-most” node  Inorder traversal of a BST encounters nodes in increasing order root

Binary Search using BST Assumes nodes are organized in a totally ordered binary tree  Begin at root node  Descend using comparison to make left/right decision if (search_value < node_value) go to the left child else if (search_value > node_value) go to the right child else return true (success)  Until descending move is impossible  Return false (failure)

Binary Search using BST Runtime <= descending path length <= depth of tree If tree has enough branching, runtime <= O(log size)

Reading assignment for next class Section 4.3