Tree Structures. A hierarchical data structure whose point of entry is the root node

Similar documents
Binary Trees

Topic 18 Binary Trees "A tree may grow a thousand feet tall, but its leaves will return to its roots." -Chinese Proverb

Programming II (CS300)

Definitions A A tree is an abstract t data type. Topic 17. "A tree may grow a. its leaves will return to its roots." Properties of Trees

Trees. Q: Why study trees? A: Many advance ADTs are implemented using tree-based data structures.

BINARY TREES, THE SEARCH TREE ADT BINARY SEARCH TREES, RED BLACK TREES, TREE TRAVERSALS, B TREES WEEK - 6

TREES. Trees - Introduction

BBM 201 Data structures

Trees 11/15/16. Chapter 11. Terminology. Terminology. Terminology. Terminology. Terminology

Binary Trees Fall 2018 Margaret Reid-Miller

Binary Trees, Binary Search Trees

Trees! Ellen Walker! CPSC 201 Data Structures! Hiram College!

CSE 230 Intermediate Programming in C and C++ Binary Tree

Trees. Tree Structure Binary Tree Tree Traversals

CE 221 Data Structures and Algorithms

Trees. (Trees) Data Structures and Programming Spring / 28

CSCI-401 Examlet #5. Name: Class: Date: True/False Indicate whether the sentence or statement is true or false.

Trees. Truong Tuan Anh CSE-HCMUT

Terminology. The ADT Binary Tree. The ADT Binary Search Tree

Trees. Introduction & Terminology. February 05, 2018 Cinda Heeren / Geoffrey Tien 1

Trees : Part 1. Section 4.1. Theory and Terminology. A Tree? A Tree? Theory and Terminology. Theory and Terminology

Chapter 11.!!!!Trees! 2011 Pearson Addison-Wesley. All rights reserved 11 A-1

Chapter 11.!!!!Trees! 2011 Pearson Addison-Wesley. All rights reserved 11 A-1

Tree. A path is a connected sequence of edges. A tree topology is acyclic there is no loop.

Data Structure. IBPS SO (IT- Officer) Exam 2017

Data Structures and Algorithms

Why Use Binary Trees? Data Structures - Binary Trees 1. Trees (Contd.) Trees

Principles of Computer Science

BST Implementation. Data Structures. Lecture 4 Binary search trees (BST) Dr. Mahmoud Attia Sakr University of Ain Shams

Data Structures - Binary Trees 1

Trees. CSE 373 Data Structures

a graph is a data structure made up of nodes in graph theory the links are normally called edges

Chapter 20: Binary Trees

Computer Science 210 Data Structures Siena College Fall Topic Notes: Trees

CS 231 Data Structures and Algorithms Fall Recursion and Binary Trees Lecture 21 October 24, Prof. Zadia Codabux

Data Structure Lecture#10: Binary Trees (Chapter 5) U Kang Seoul National University

[ DATA STRUCTURES ] Fig. (1) : A Tree


Lecture Notes 16 - Trees CSS 501 Data Structures and Object-Oriented Programming Professor Clark F. Olson

Data Structures - Binary Trees and Operations on Binary Trees

March 20/2003 Jayakanth Srinivasan,

Section 5.5. Left subtree The left subtree of a vertex V on a binary tree is the graph formed by the left child L of V, the descendents

DATA STRUCTURES AND ALGORITHMS. Hierarchical data structures: AVL tree, Bayer tree, Heap

Discussion 2C Notes (Week 8, February 25) TA: Brian Choi Section Webpage:

CS24 Week 8 Lecture 1

Trees. Eric McCreath

Stacks, Queues & Trees. The Stack Interface. The Stack Interface. Harald Gall, Prof. Dr.

Programming II (CS300)

Binary Trees. College of Computing & Information Technology King Abdulaziz University. CPCS-204 Data Structures I

INF2220: algorithms and data structures Series 1

Data Structure - Binary Tree 1 -

CS 206 Introduction to Computer Science II

Lec 17 April 8. Topics: binary Trees expression trees. (Chapter 5 of text)

CSI33 Data Structures

Uses for Trees About Trees Binary Trees. Trees. Seth Long. January 31, 2010

Garbage Collection: recycling unused memory

CS61B Lecture #20: Trees. Last modified: Mon Oct 8 21:21: CS61B: Lecture #20 1

A set of nodes (or vertices) with a single starting point

Binary Trees. Height 1

CSC148 Week 6. Larry Zhang

Lecture 26. Introduction to Trees. Trees

LECTURE 13 BINARY TREES

Data Structures. Binary Trees. Root Level = 0. number of leaves:?? leaves Depth (Maximum level of the tree) leaves or nodes. Level=1.

Cpt S 122 Data Structures. Data Structures Trees

Tree. Virendra Singh Indian Institute of Science Bangalore Lecture 11. Courtesy: Prof. Sartaj Sahni. Sep 3,2010

CS 206 Introduction to Computer Science II

Chapter Contents. Trees. Tree Concepts. Hierarchical Organization. Hierarchical Organization. Hierarchical Organization.

CSE 214 Computer Science II Introduction to Tree

(2,4) Trees. 2/22/2006 (2,4) Trees 1

Tree Data Structures CSC 221

Data Structures And Algorithms

Name CPTR246 Spring '17 (100 total points) Exam 3

Objectives. In this session, you will learn to:

Successor/Predecessor Rules in Binary Trees

Data Structures and Algorithms

Trees. A tree is a directed graph with the property

The tree data structure. Trees COL 106. Amit Kumar Shweta Agrawal. Acknowledgement :Many slides are courtesy Douglas Harder, UWaterloo

Binary Trees. Examples:

COMP : Trees. COMP20012 Trees 219

Trees (Part 1, Theoretical) CSE 2320 Algorithms and Data Structures University of Texas at Arlington

ITI Introduction to Computing II

CS350: Data Structures Tree Traversal

Chapter 4 Trees. Theorem A graph G has a spanning tree if and only if G is connected.

IX. Binary Trees (Chapter 10)

Binary Trees and Huffman Encoding Binary Search Trees

Analysis of Algorithms

Bioinformatics Programming. EE, NCKU Tien-Hao Chang (Darby Chang)

Tree traversals and binary trees

13 BINARY TREES DATA STRUCTURES AND ALGORITHMS INORDER, PREORDER, POSTORDER TRAVERSALS

1 Binary trees. 1 Binary search trees. 1 Traversal. 1 Insertion. 1 An empty structure is an empty tree.

Tree: non-recursive definition. Trees, Binary Search Trees, and Heaps. Tree: recursive definition. Tree: example.

ITI Introduction to Computing II

Chapter 5. Binary Trees

(2,4) Trees Goodrich, Tamassia (2,4) Trees 1

Topic 14. The BinaryTree ADT

Module 4: Index Structures Lecture 13: Index structure. The Lecture Contains: Index structure. Binary search tree (BST) B-tree. B+-tree.

Data Structures and Algorithms for Engineers

Trees. Trees. CSE 2011 Winter 2007

Why Do We Need Trees?

! Tree: set of nodes and directed edges. ! Parent: source node of directed edge. ! Child: terminal node of directed edge

Transcription:

Binary Trees 1

Tree Structures A tree is A hierarchical data structure whose point of entry is the root node This structure can be partitioned into disjoint subsets These subsets are themselves trees and are also subtrees of the tree

Definitions A tree is an abstract data type one entry point, the root Each node is either a leaf or an internal node An internal node has 1 or more children, nodes that can be reached directly from that internal node. The internal node is said to be the parent of its child nodes internal nodes leaf nodes root node 3

Properties of Trees Only access point is the root All nodes, except the root, have one parent like the inheritance hierarchy in Java Traditionally trees drawn upside down root 4

Properties of Trees and Nodes siblings: two nodes that have the same parent edge: the link from one node to another path length: the number of edges that must be traversed to get from one node to another path length from root to this node is 3 edge siblings root 5

More Properties of Trees depth: the path length from the root of the tree to this node height of a node: The maximum distance (path length) of any leaf from this node a leaf has a height of 0 the height of a tree is the height of the root of that tree 6

Tree Visualization A B C D E F G H I J K L M N O 7

Binary Trees There are many variations on trees but we will work with binary trees binary tree: a tree with at most two children for each node the possible children are normally referred to as the left and right child parent left child right child 8

Binary Trees A hierarchical data structure which May be empty (empty tree or null tree) All nodes can have degree of 0, 1 or 2 A node is explicitly defined as a left child or a right child Consists of subtrees called left subtree and right subtree

Binary Tree - concept A binary tree structure is either empty or consists of an element, called the Root element and two distinct branches a left subtree and right subtree The left and right subtrees may be empty or contain elements a left element and right element Left Root Right 10

The Binary Tree grows The left and right subtrees may also contain distinct branches And each of those subtrees may contain elements Root Left Right LeftLeft LeftRight Right Left Right Right 11

Binary Tree taxonomy A leaf is an element whose left and right subtrees are empty. A node is an element containing at least one subtree Root Leaf ode Leaf ode Leaf 12

In a binary tree t, height is the number of branches from the root to the farthest leaf. depth(x), the depth of an element x is the number of branches from the root element to x. If x is the root element, depth(x) = 0 level(x), the level of x, is the same as the depth of x. Root Depth=2 Leaf ode Height=3 Leaf ode Leaf 13

14

Full Binary Tree full binary tree: a binary tree is which each node was exactly 2 or 0 children 15

Complete Binary Tree complete binary tree: a binary tree in which every level, except possibly the deepest is completely filled. At depth n, the height of the tree, all nodes are as far left as possible 16

Perfect Binary Tree perfect binary tree: a binary tree with all leaf nodes at the same depth. All internal nodes have exactly two children. a perfect binary tree has the maximum number of nodes for a given height a perfect binary tree has 2 (n+1) - 1 nodes where n is the height of a tree height = 0 -> 1 node height = 1 -> 3 nodes height = 2 -> 7 nodes height = 3 -> 15 nodes 17

Traversals of a Binary Tree 18

Tree Traversal Traversing a tree means visiting each node in a specified order This process is not as commonly used as finding, inserting, and deleting nodes. One reason for this is that traversal is not particularly fast. But traversing a tree is useful in some circumstances and the algorithm is interesting

There are generally two types of traversal: breadth first depth first. There are three variants for depth first traverse a tree. They're called preorder, inorder, and postorder.

1-Inorder Traversal An inorder traversal of a binary search tree will cause all the nodes to be visited in ascending order, based on their key values. If you want to create a sorted list of the data in a binary tree, this is one way to do it. 21

The simplest way to carry out a traversal is the use of recursion. A recursive method to traverse the entire tree is called with a node as an argument. Initially, this node is the root. The method needs to do only three things: 1. Call itself to traverse the node's left subtree 2. Visit the node 3. Call itself to traverse the node's right subtree 22

Java Code for Traversing private void inorder(node localroot) { if(localroot!= null) { inorder(localroot.leftchild); localroot.displaynode(); inorder(localroot.rightchild); } } 23

50 30 90 10 40 80 100 Determine the order in which the elements would be accessed during an in-order traversal. 24

Answer: 12, 30, 40, 50, 86, 90, 100 25

2-Postorder 1. Call itself to traverse the node's left subtree. 2. Call itself to traverse the node's right subtree. 3. Visit the node. 26

2. postorder (t) { if (t is not empty) { postorder(lefttree(t)); postorder(righttree(t)); access the root element of t; } // if } // postorder traversal Left Right Root 27

3-Preorder 1. Visit the node. 2. Call itself to traverse the node's left subtree. 3. Call itself to traverse the node's right subtree. 28

preorder (t) { if (t is not empty) { access the root element of t; preorder (lefttree (t)); preorder (righttree (t)); } // if } // preorder traversal Root Left Right 29

Breadth-first traversal (level order) To perform a breadth-first traversal of a non-empty binary tree, first access the root element, then the children of the root element, from left to right, then the grandchildren of the root element, from left to right, and so on. 30

Perform a breadth-first traversal of the following binary tree. A B C D E R S F G L 31

Answer: A, B, C, D, E, R, S, F, G, L 32

Perform the other traversals of that tree: inorder (Left-Root-Right) postorder (Left - Right - Root) preorder (Root - Left - Right) A B C D E R S F G L 33

Perform the other traversals of that tree: inorder (Left-Root-Right) postorder (Left - Right - Root) preorder (Root - Left - Right) A B M D J R X H L P Z 34