Chapter 20: Binary Trees

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

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

Programming II (CS300)

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

Binary Trees

TREES. Trees - Introduction

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

CSI33 Data Structures

Binary Trees, Binary Search Trees

Binary Trees. Examples:

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

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

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

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

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

Trees: examples (Family trees)

March 20/2003 Jayakanth Srinivasan,

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

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

CS350: Data Structures Tree Traversal

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

Associate Professor Dr. Raed Ibraheem Hamed

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

Why Do We Need Trees?

INF2220: algorithms and data structures Series 1

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

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

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

CSI33 Data Structures

Binary Trees. Height 1

Data Structure - Binary Tree 1 -

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

Objectives. In this session, you will learn to:

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

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

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

Trees. Truong Tuan Anh CSE-HCMUT

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

Binary Trees Fall 2018 Margaret Reid-Miller

MULTIMEDIA COLLEGE JALAN GURNEY KIRI KUALA LUMPUR

Cpt S 122 Data Structures. Data Structures Trees

BBM 201 Data structures

COSC 2011 Section N. Trees: Terminology and Basic Properties

IX. Binary Trees (Chapter 10)

CS302 - Data Structures using C++

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

Trees. CSE 373 Data Structures

Data and File Structures Laboratory

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

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

Advanced Java Concepts Unit 5: Trees. Notes and Exercises

INF2220: algorithms and data structures Series 1

Data Structures and Algorithms

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

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

Algorithms. Deleting from Red-Black Trees B-Trees

SCJ2013 Data Structure & Algorithms. Binary Search Tree. Nor Bahiah Hj Ahmad

AP Programming - Chapter 20 Lecture page 1 of 17

CSC148-Section:L0301

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

CISC 235 Topic 3. General Trees, Binary Trees, Binary Search Trees

Trees, Binary Trees, and Binary Search Trees

Binary Trees and Binary Search Trees

Programming Languages and Techniques (CIS120)

IX. Binary Trees (Chapter 10) Linear search can be used for lists stored in an array as well as for linked lists. (It's the method used in the find

Principles of Computer Science

Binary Trees. Directed, Rooted Tree. Terminology. Trees. Binary Trees. Possible Implementation 4/18/2013

Friday Four Square! 4:15PM, Outside Gates

Trees. Trees. CSE 2011 Winter 2007

CE 221 Data Structures and Algorithms

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

STUDENT LESSON AB30 Binary Search Trees

CS302 - Data Structures using C++

Binary Search Tree (3A) Young Won Lim 6/2/18

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

Introduction to Binary Trees

7.1 Introduction. A (free) tree T is A simple graph such that for every pair of vertices v and w there is a unique path from v to w

3137 Data Structures and Algorithms in C++

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

CS 206 Introduction to Computer Science II

Algorithms and Data Structures

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

Binary Search Trees. 1. Inorder tree walk visit the left subtree, the root, and right subtree.

Binary Search Trees. See Section 11.1 of the text.

Binary Search Tree (2A) Young Won Lim 5/17/18

Chapter 5. Binary Trees

C Data Structures Stacks. Stack. push Adds a new node to the top of the stack

selectors, methodsinsert() andto_string() the depth of a tree and a membership function

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

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

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

Advanced Java Concepts Unit 5: Trees. Notes and Exercises

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

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

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

Trees. A tree is a directed graph with the property

Data Structures - Binary Trees and Operations on Binary Trees

Binary Search Tree 1.0. Generated by Doxygen Mon Jun :12:39

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

Transcription:

Chapter 20: Binary Trees

20.1 Definition and Application of Binary Trees

Definition and Application of Binary Trees Binary tree: a nonlinear linked list in which each node may point to 0, 1, or two other nodes Each node contains one or more data fields and two pointers

Binary Tree Terminology 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

Binary Tree Terminology Leaf nodes: nodes that have no children The nodes containing and are leaf nodes 31

Binary Tree Terminology Child nodes, children: nodes below a given node The children of the node containing 31 are the nodes containing and 31

Binary Tree Terminology Parent node: node above a given node The parent of the node containing is the node containing 31

Binary Tree Terminology Subtree: the portion of a tree from a node down to the leaves The nodes containing and are the left subtree of the node containing 31 31

Uses of Binary Trees Binary search tree: data organized in a binary tree to simplify searches Left subtree of a node contains data values < the data in the node Right subtree of a node contains values > the data in the node 31

Searching in a Binary Tree 1) Start at root node 2) Examine node data: a) Is it desired value? Done b) Else, is desired data < node data? Repeat step 2 with left subtree c) Else, is desired data > node data? Repeat step 2 with 31 right subtree 3) Continue until desired value found or pointer reached

Searching in a Binary Tree To locate the node containing, Examine the root node (31) first Since > 31, examine the right child of the node containing 31, () Since <, examine the left child of the node containing, () 31 The node containing has been found

20.2 Binary Search Tree Operations

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; }

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 : newnode->left = newnode->right 23 newnode newnode = ; newnode 23

Inserting a Node in a Binary Search Tree 1) If tree is empty, insert the new node as the root node 2) 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 pointer found 4) Set this pointer to point to new node

Inserting a Node in a Binary Search Tree Examine this node first value is < node, so go to left subtree root 23 newnode Examine this node second value is > node, so go to right subtree 31 Since the right subtree is, insert here

Traversing a Binary Tree Three traversal methods: 1) Inorder: a) Traverse left subtree of node b) Process data in node c) Traverse right subtree of node 2) Preorder: a) Process data in node b) Traverse left subtree of node c) Traverse right subtree of node 3) Postorder: a) Traverse left subtree of node b) Traverse right subtree of node c) Process data in node

Traversing a Binary Tree 31 TRAVERSAL METHOD NODES VISITED IN ORDER Inorder,, 31,, Preorder 31,,,, Postorder,,,, 31

Searching in a Binary Tree Start at root node, traverse the tree looking for value Stop when value found or pointer detected Can be implemented as a bool function 31 Search for? return true Search for 1? return false

Deleting a Node from a Binary Tree Leaf Node If node to be deleted is a leaf node, replace parent node s pointer to it with a pointer, then delete the node Deleting node with before deletion Deleting node with after deletion

Deleting a Node from a Binary Tree One Child 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

Deleting a Node from a Binary Tree One Child 31 31 Deleting node with before deletion Deleting node with after deletion

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

Deleting a Node from a Binary Tree Two Children 31 Deleting node with 31 before deletion Deleting node with 31 after deletion

20.3 Template Considerations for Binary Search Trees

Template Considerations for Binary Search Trees Binary tree can be implemented as a template, allowing flexibility in determining type of data stored Implementation must support relational operators >, <, and == to allow comparison of nodes