Binary Tree. Binary tree terminology. Binary tree terminology Definition and Applications of Binary Trees

Similar documents
Chapter 20: Binary Trees

Trees. 9/21/2007 8:11 AM Trees 1

Trees and Binary Trees

Trees. Make Money Fast! Stock Fraud. Bank Robbery. Ponzi Scheme. Trees Goodrich, Tamassia

Unit 1 (9Hrs) Trees. Mahesh Sanghavi & Deepali Pawar Department of Computer Engineering, SNJB s KBJ College of Engineering, Chandwad, India

Trees. Make Money Fast! Bank Robbery. Ponzi Scheme. Stock Fraud. 02/06/2006 Trees 1

Trees. Make Money Fast! Bank Robbery. Stock Fraud. Ponzi Scheme Goodrich, Tamassia. Trees 1

OUTLINE. General Trees (Ch. 7.1) Binary Trees (Ch. 7.3) Tree Traversals (Ch. 7.2)

Trees. Make Money Fast! Bank Robbery. Ponzi Scheme. Stock Fraud. 1/18/2005 4:17 AM Trees 1

Trees. Trees. CSE 2011 Winter 2007

Trees. Make Money Fast! Bank Robbery. Stock Fraud. Ponzi Scheme. Trees Goodrich, Tamassia

Data Structures and Algorithms. Trees

Trees. Make Money Fast! Bank Robbery. Ponzi Scheme. Stock Fraud Goodrich, Tamassia, Goldwasser Trees

Trees 3/19/14. Mammal. Dog Pig Cat

Binary Trees. Examples:

Stock Fraud. Nancy Amato. Parasol Lab, Dept. CSE, Texas A&M University

In a postorder traversal, a node is visited after its descendants Application: compute space used by files in a directory and its subdirectories 9 1

DataStruct 7. Trees. Michael T. Goodrich, et. al, Data Structures and Algorithms in C++, 2 nd Ed., John Wiley & Sons, Inc., 2011.

Binary Trees, Binary Search Trees

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

Binary trees. Binary trees. Binary trees

Data Structures Lecture 6

Data Structures. Trees, Binary trees & Binary Search Trees

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

Outline. Definitions Traversing trees Binary trees

CSI33 Data Structures

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

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

TREES. Trees - Introduction

Binary Trees and Binary Search Trees

Binary Tree. Preview. Binary Tree. Binary Tree. Binary Search Tree 10/2/2017. Binary Tree

Elementary Data Structures. Stacks, Queues, & Lists Amortized analysis Trees

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

March 20/2003 Jayakanth Srinivasan,

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

Also, recursive methods are usually declared private, and require a public non-recursive method to initiate them.

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

Trees. Data structures and Algorithms. Make Money Fast! Bank Robbery. Stock Fraud. Ponzi Scheme

BBM 201 Data structures

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

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

Cpt S 122 Data Structures. Data Structures Trees

Data and File Structures Laboratory

Abstract Data Structures IB Computer Science. Content developed by Dartford Grammar School Computer Science Department

Binary Trees. Height 1

Ch02 Data Structures

Programming II (CS300)

Introduction to Computers and Programming. Concept Question

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

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

Ch02 Data Structures

Data Structure. Chapter 5 Trees (Part II) Angela Chih-Wei Tang. National Central University Jhongli, Taiwan

CS350: Data Structures Tree Traversal

Data Structures. Trees. By Dr. Mohammad Ali H. Eljinini. M.A. Eljinini, PhD

Trees: examples (Family trees)

Binary Trees

Trees, Binary Trees, and Binary Search Trees

Why Do We Need Trees?

What is a Tree. Trees. Tree ADT ( 6.1.2) Tree Terminology. subtree. In computer science, a tree is an abstract model

Data Structure - Binary Tree 1 -

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

Outline. Preliminaries. Binary Trees Binary Search Trees. What is Tree? Implementation of Trees using C++ Tree traversals and applications

Advanced Java Concepts Unit 5: Trees. Notes and Exercises

CE 221 Data Structures and Algorithms

We have the pointers reference the next node in an inorder traversal; called threads

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

8. Binary Search Tree

Binary Trees. For example: Jargon: General Binary Trees. root node. level: internal node. edge. leaf node. Data Structures & File Management

MULTIMEDIA COLLEGE JALAN GURNEY KIRI KUALA LUMPUR

Binary Tree Node Relationships. Binary Trees. Quick Application: Expression Trees. Traversals

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

Tree Data Structures CSC 221

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

CSI33 Data Structures

CS302 - Data Structures using C++

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

IX. Binary Trees (Chapter 10)

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

Binary Search Trees. See Section 11.1 of the text.

PESIT Bangalore South Campus Hosur road, 1km before Electronic City, Bengaluru -100 Department of Electronics and Communication

Principles of Computer Science

Topic Binary Trees (Non-Linear Data Structures)

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

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

CSC/MAT-220: Lab 6. Due: 11/26/2018

Algorithms. Deleting from Red-Black Trees B-Trees

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

CMPT 225. Binary Search Trees

INF2220: algorithms and data structures Series 1

INF2220: algorithms and data structures Series 1

CS 206 Introduction to Computer Science II

Advanced Java Concepts Unit 5: Trees. Notes and Exercises

CS 234. Module 5. October 18, CS 234 Module 5 ADTS with items related by structure 1 / 25

tree nonlinear Examples

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

Tree traversals and binary trees

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

Introduction to Binary Trees

Trees. Truong Tuan Anh CSE-HCMUT

Tree Traversal. Lecture12: Tree II. Preorder Traversal

Transcription:

Binary Tree (Chapter 0. Starting Out with C++: From Control structures through Objects, Tony Gaddis) Le Thanh Huong School of Information and Communication Technology Hanoi University of Technology 11.1 Definition and Applications of Binary Trees Binary tree: a nonlinear linked list in which each node may point to 0, 1, or other nodes. Each node contains one or more data fields and two pointers Le Thanh Huong - SoICT - HUT Tree pointer: like a head pointer for a linked list, it points to the first node in the binary tree Root node: the node at the top of the tree Leaf nodes: nodes that have no children The nodes containing 7 and 43 are leaf nodes Le Thanh Huong - SoICT - HUT 3 Le Thanh Huong - SoICT - HUT 4 1

Child nodes, children: nodes below a given node The children of the node containing 31 are the nodes containing 19 and 59 Parent node: node above a given node The parent of the node containing 43 is the node containing 59 Le Thanh Huong - SoICT - HUT 5 Le Thanh Huong - SoICT - HUT 6 Subtree: the portion of a tree from a node down to the leaves The nodes containing 19 and 7 are the left subtree of the node containing 31 Uses of binary trees - Arithmetic Expression Tree Binary tree associated with an arithmetic expression internal nodes: operators external nodes: operands Example: arithmetic expression tree for the expression ( (a 1) + (3 b)) + a 1 3 b Le Thanh Huong - SoICT - HUT 7 Le Thanh Huong - SoICT - HUT 8

Uses of binary trees Decision Tree Binary tree associated with a decision process internal nodes: questions with yes/no answer external nodes: decisions Example: dining decision Yes How about coffee? Want a fast meal? On expense account? Starbucks Spike s Al Forno Café Paragon No Yes No Yes No Uses of binary trees Binary search tree Binary search tree: data organized in a binary tree to simplify searches Values in left subtree less than parent Values in right subtree greater than parent Fast searches - for a balanced tree, maximum of log (n) comparisons Le Thanh Huong - SoICT - HUT 9 Le Thanh Huong - SoICT - HUT 10 Searching in a binary tree Searching in a binary tree 1. Start at root node. Examine node data: a) Is it desired values? Done b) Else, is desired data < node data? Repeat step with left subtree c) Else, is desired data > node data? Repeat step with right subtree 3. Continue until desired value found or NULL pointer reached x? To locate the node containing 43, Examine the root node (31) first Since 43>31, examine the right child of the node containing 31, (59) Since 43<59, examine the left child of the node containing 59, (43) The node containing 43 has been found 43 Le Thanh Huong - SoICT - HUT 11 Le Thanh Huong - SoICT - HUT 1 3

11. Binary Search Tree Operations Create a binary search tree organize data into a binary search tree Insert a node into a binary tree - put node into tree in its correct position to maintain order Find a node in a binary tree locate a node with particular data value Delete a node from a binary tree remove a node and adjust links to maintain binary tree Binary search tree node A node in a binary tree is like a node in a linked list, with two node pointer fields: struct TreeNode { int value; TreeNode *left; TreeNode *right; }; Le Thanh Huong - SoICT - HUT 13 Le Thanh Huong - SoICT - HUT 14 Creating a new node Allocate memory for new node: newnode = new TreeNode; Initialize the contents of the node: newnode->value = num; Set the pointers to NULL newnode->left = newnode->right = NULL; Inserting a node into a binary search tree 1. If tree is empty, insert the new node as the root node. Else, compare new node against left or right child, depending on whether data value of new node is < or > root node 3. Continue comparing and choosing left or right subtree until NULL pointer found 4. Set this NULL pointer to point to new node Le Thanh Huong - SoICT - HUT 15 Le Thanh Huong - SoICT - HUT 16 4

Inserting a node in a binary search tree Trarversing a binary tree Examine this node first - value is < node, so go to left subtree Examine this node second - value is > node, so go to right subtree Since the right subtree is NULL, insert here Three traversal methods: Inorder: Traverse left subtree of node Process data in node Traverse right subtree of node Preorder: Process data in node Traverse left subtree of node Traverse right subtree of node Postorder: Traverse left subtree of node Traverse right subtree of node Process data in node Le Thanh Huong - SoICT - HUT 17 Le Thanh Huong - SoICT - HUT 18 Trarversing a binary tree Searching in a binary tree Start at root node, traverse the tree looking for value Stop when value found or NULL pointer detected Can be implemented as a bool function Search for 43? Return true Search for 17? Return false Le Thanh Huong - SoICT - HUT 19 Le Thanh Huong - SoICT - HUT 0 5

Deleting a node from a binary tree leaf node Deleting a node from a binary tree one child If node to be deleted is a leaf node, replace parent node s pointer to it with a NULL pointer, then delete the node If node to be deleted has one child node, adjust pointers so that parent of node to be deleted points to child of node to be deleted, then delete the node Delete node with 7 before deletion Delete node with 7 after deletion Le Thanh Huong - SoICT - HUT 1 Delete node with 19 before deletion Delete node with 19 after deletion Le Thanh Huong - SoICT - HUT Deleting a node from a binary tree two children Deleting a node from a binary tree two children If node to be deleted has left and right children, Promote one child to take the place of the deleted node Locate correct position for other child in subtree of promoted child Convention in text: promote the right child, position left subtree underneath Le Thanh Huong - SoICT - HUT 3 Le Thanh Huong - SoICT - HUT 4 6

11.3 Template Considerations for Binary Search Trees Exercise: Inorder Traversal Binary tree can be implemented as a template, allowing flexibility in determining type of data stored Write a program to list the nodes of this tree in inorder traversal order. B A C Implementation must suport relational operators >, <, and == to allow comparison of nodes Algorithm inorder(v) inorder (leftchild (v)) visit(v) inorder (rightchild (v)) E I F K G H Le Thanh Huong - SoICT - HUT 5 Le Thanh Huong - SoICT - HUT 6 Print Arithmetic Expressions Specialization of an inorder traversal print operand or operator when visiting node print ( before traversing left subtree print ) after traversing right subtree a 1 + 3 b Algorithm printexpression(v) print( ( ) inorder (leftchild (v)) print(v.element ()) inorder (rightchild (v)) print ( ) ) (( (a 1)) + (3 b)) Le Thanh Huong - SoICT - HUT 7 Evaluate Arithmetic Expressions Specialization of a postorder traversal recursive method returning the value of a subtree when visiting an internal node, combine the values of the subtrees 5 1 + 3 Algorithm evalexpr(v) if isexternal (v) return v.element () else x evalexpr(leftchild (v)) y evalexpr(rightchild (v)) operator stored at v return x y Le Thanh Huong - SoICT - HUT 8 7