Basic Algorithms on Trees. If the node v is the root, then the depth of the node v is 0. Otherwise, the depth of the node v is one plus the depth of.

Slides:



Advertisements
Similar presentations
Alyce Brady CS 470: Data Structures CS 510: Computer Algorithms Post-order Traversal: Left Child - Right Child - Root Depth-First Search.
Advertisements

Postorder traversal - Ed. 2. and 3.: Chapter 6 – - Ed. 4.: Chapter 7 -
CS Fall 2012, Lab 08 Haohan Zhu. Boston University Slideshow Title Goes Here CS Fall 2012, Lab /17/2015 Tree - Data Structure  Basic.
S. Sudarshan Based partly on material from Fawzi Emad & Chau-Wen Tseng
SUNY Oneonta Data Structures and Algorithms Visualization Teaching Materials Generation Group Binary Search Tree A running demonstration of binary search.
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,
Chapter 4: Trees General Tree Concepts Binary Trees Lydia Sinapova, Simpson College Mark Allen Weiss: Data Structures and Algorithm Analysis in Java.
Kymberly Fergusson CSE1303 Part A Data Structures and Algorithms Summer Semester 2003 Lecture A12 – Binary Trees.
Tree Traversal. Traversal Algorithms preorder inorder postorder.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Chapter 26 Binary Search Trees.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved L12 (Chapter 20) Lists, Stacks,
© 2006 Pearson Addison-Wesley. All rights reserved11 A-1 Chapter 11 Trees.
Kymberly Fergusson CSE1303 Part A Data Structures and Algorithms Summer Semester 2003 Lecture A12 – Binary Trees.
Tree Traversal. Traversal Algorithms preorder inorder postorder.
Properties: -Each node has a value -The left subtree contains only values less than the parent node’s value -The right subtree contains only values greater.
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 19: Binary Trees. Objectives In this chapter, you will: – Learn about binary trees – Explore various binary tree traversal algorithms – Organize.
Trees CS212 & CS-240 D.J. Foreman. What is a Tree A tree is a finite set of one or more nodes such that: –There is a specially designated node called.
© 2011 Pearson Addison-Wesley. All rights reserved 11 B-1 Chapter 11 (continued) Trees.
CISC220 Fall 2009 James Atlas Lecture 13: Trees. Skip Lists.
Binary Trees 2 Overview Trees. Terminology. Traversal of Binary Trees. Expression Trees. Binary Search Trees.
COMP20010: Algorithms and Imperative Programming Lecture 1 Trees.
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.
 Trees Data Structures Trees Data Structures  Trees Trees  Binary Search Trees Binary Search Trees  Binary Tree Implementation Binary Tree Implementation.
Discrete Structures Trees (Ch. 11)
Computer Science 112 Fundamentals of Programming II Introduction to Trees.
© Copyright 2012 by Pearson Education, Inc. All Rights Reserved. 1 Chapter 19 Binary Search Trees.
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.
Trees. 2 Root leaf CHAPTER 5 3 Definition of Tree n A tree is a finite set of one or more nodes such that: n There is a specially designated node called.
© 2006 Pearson Addison-Wesley. All rights reserved11 A-1 Chapter 11 Trees.
Data Abstraction and Problem Solving with JAVA Walls and Mirrors Frank M. Carrano and Janet J. Prichard © 2001 Addison Wesley Data Abstraction and Problem.
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.
Foundation of Computing Systems Lecture 4 Trees: Part I.
Lecture 7: Searching a Tree Neil Ghani University of Strathclyde.
CHAPTER 10.1 BINARY SEARCH TREES    1 ACKNOWLEDGEMENT: THESE SLIDES ARE ADAPTED FROM SLIDES PROVIDED WITH DATA STRUCTURES AND ALGORITHMS.
Chapter 7 Trees_ Part2 TREES. Depth and Height 2  Let v be a node of a tree T. The depth of v is the number of ancestors of v, excluding v itself. 
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
Data Structures and Algorithms
Trees Chapter 15.
Lecture 1 (UNIT -4) TREE SUNIL KUMAR CIT-UPES.
Chapter 25 Binary Search Trees
Trees Chapter 11 (continued)
Fundamentals of Programming II Introduction to Trees
Planning & System installation
Recursive Objects (Part 4)
Planning & System installation
CISC220 Fall 2009 James Atlas Lecture 13: Binary Trees.
Binary Search Tree (BST)
Chapter 10.1 Binary Search Trees
Tree.
Section 8.1 Trees.
CS223 Advanced Data Structures and Algorithms
Alyce Brady CS 470: Data Structures CS 510: Computer Algorithms
(2,4) Trees (2,4) Trees 1 (2,4) Trees (2,4) Trees
Trees.
Binary Tree Traversal Methods
Binary Tree Traversal Methods
Abstract Data Structures
Principles of Computing – UFCFA3-30-1
CSE 373, Copyright S. Tanimoto, 2002 Binary Trees -
Binary Tree Traversal Methods
Binary Tree Traversals
2018, Fall Pusan National University Ki-Joune Li
CSE 373, Copyright S. Tanimoto, 2001 Binary Trees -
Chapter 20: Binary Trees.
Binary Trees.
General Tree Concepts Binary Trees
Data Structures Using C++ 2E
Chapter 11 Trees © 2011 Pearson Addison-Wesley. All rights reserved.
Presentation transcript:

Basic Algorithms on Trees

If the node v is the root, then the depth of the node v is 0. Otherwise, the depth of the node v is one plus the depth of the parent of the node v.

Data Structure Exercises 11.1

Binary Trees

The Binary Tree Abstract Data Type

A Binary Tree Interface in Java

Levels of a Binary Tree

Level: number of nodes at a level:

Level Number of nodes Pattern … … k … … … k 2 …

Traversals of a Binary Tree

Data Structure Exercises 11.2

Inorder tree traversal Inorder traversal based on recursion:

inorder(T, r) if … inorder(T, u) “visit” r If … inorder (T, a) inorder(T, u) if … inorder(T, w) “visit” u If … inorder (T, v) inorder(T, w) if … “visit” w if … 1 26

inorder(T, x) if … “visit” x if … 3 inorder(T, v) if … inorder(T, x) “visit” v If … inorder (T, y) 4

if … “visit” y if … 5 inorder(T, a) if … inorder(T, b) “visit” a If … inorder (T, c) 8 7 9

Inorder traversal based on Stack data structure Algorithm Stack-control-inorder(T, v) establish stack S; S.push(v); while (S is not empty) do {u := S.pop(); if u is leaf or u is marked, visit u; else {let v1 and v2 be the left and right child node of v, respectively; S.push(v2); mark u; S.push(u*); S.push(v1); }

u r* a r w u* v r* a u* v r* a v r* a print(u) x v* y r* a v* y r* a print(v) y r* a print(y) r* a print(y) a b a* c a* c print(b) c print(a) print(x) 3 print(w) 1 print(c) 89