Binary Trees. Examples:

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

Chapter 20: Binary Trees

Binary Trees, Binary Search Trees

BBM 201 Data structures

Binary Trees. Height 1

TREES. Trees - Introduction

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

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

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

Trees. Truong Tuan Anh CSE-HCMUT

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

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

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

Programming II (CS300)

8. Binary Search Tree

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

Data Structure - Binary Tree 1 -

Data and File Structures Laboratory

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

Binary Trees

INF2220: algorithms and data structures Series 1

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

Principles of Computer Science

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

Data Structures And Algorithms

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

Binary Trees and Binary Search Trees

Trees, Binary Trees, and Binary Search Trees

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

Tree Structures. Definitions: o A tree is a connected acyclic graph. o A disconnected acyclic graph is called a forest

CSI33 Data Structures

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

Associate Professor Dr. Raed Ibraheem Hamed

Algorithms. Deleting from Red-Black Trees B-Trees

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

Trees. Estruturas de Dados / Programação 2 Árvores. Márcio Ribeiro twitter.com/marciomribeiro. Introduc)on. Hierarchical structure

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

Binary Search Trees. See Section 11.1 of the text.

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

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

2-3 Tree. Outline B-TREE. catch(...){ printf( "Assignment::SolveProblem() AAAA!"); } ADD SLIDES ON DISJOINT SETS

Advanced Java Concepts Unit 5: Trees. Notes and Exercises

CMPT 225. Binary Search Trees

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

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

Tree Travsersals and BST Iterators

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

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

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

Data Structure and Algorithm Homework #3

Module 04 Trees Contents

Why Do We Need Trees?

Cpt S 122 Data Structures. Data Structures Trees

Partha Sarathi Mandal

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

CS350: Data Structures Binary Search Trees

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

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

Recursion, Binary Trees, and Heaps February 18

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

CSI 402 Spring 2014 Programming Assignment I 1 / 15

* Due 11:59pm on Sunday 10/4 for Monday lab and Tuesday 10/6 Wednesday Lab

CS350: Data Structures Tree Traversal

Binary search trees. Binary search trees are data structures based on binary trees that support operations on dynamic sets.

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

Binary search trees 3. Binary search trees. Binary search trees 2. Reading: Cormen et al, Sections 12.1 to 12.3

Chapter 5. Binary Trees

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

Garbage Collection: recycling unused memory

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

STUDENT LESSON AB30 Binary Search Trees

Data Structures and Algorithms

Multi-Way Search Tree

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

Introduction to Computers and Programming. Concept Question

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

Search Trees. Data and File Structures Laboratory. DFS Lab (ISI) Search Trees 1 / 17

Data Structures and Algorithms for Engineers

Successor/Predecessor Rules in Binary Trees

CS 350 : Data Structures Binary Search Trees

Binary search trees (BST) Binary search trees (BST)

Binary Search Trees. What is a Binary Search Tree?

Trees. A tree is a directed graph with the property

IX. Binary Trees (Chapter 10)

Lecture 32. No computer use today. Reminders: Homework 11 is due today. Project 6 is due next Friday. Questions?

Trees. Dr. Ronaldo Menezes Hugo Serrano Ronaldo Menezes, Florida Tech

3137 Data Structures and Algorithms in C++

binary tree empty root subtrees Node Children Edge Parent Ancestor Descendant Path Depth Height Level Leaf Node Internal Node Subtree

CE 221 Data Structures and Algorithms

Trees. CSE 373 Data Structures

Binary Trees. BSTs. For example: Jargon: Data Structures & Algorithms. root node. level: internal node. edge.

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

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

CS350: Data Structures Red-Black Trees

INF2220: algorithms and data structures Series 1

Tree Data Structures CSC 221

MULTIMEDIA COLLEGE JALAN GURNEY KIRI KUALA LUMPUR

CS24 Week 8 Lecture 1

March 20/2003 Jayakanth Srinivasan,

Transcription:

Binary Trees A tree is a data structure that is made of nodes and pointers, much like a linked list. The difference between them lies in how they are organized: In a linked list each node is connected to one successor node (via next pointer), that is, it is linear. In a tree, the nodes can have several next pointers and thus are not linear. Examples: root root The top node in the tree is called the root and all other nodes branch off from this one. Every node in the tree can have some number of children. Each child node can in turn be the parent node to its children and so on. The following are NOT binary trees: A common example of a tree structure is the binary tree. Definition: A binary tree is a tree that is limited such that each node has only two children. 1 2

Definitions: If n1 is the root of a binary tree and n2 is the root of its left or right tree, then n1 is the parent of n2 and n2 is the left or right child of n1. A node that has no children is called a leaf. The nodes are siblings if they are left and right children of the same parent. The level of a node in a binary tree: The root of the tree has level 0 The level of any other node in the tree is one more than the level of its parent. root Level 0 Implementation A binary tree has a natural implementation in linked storage. A separate pointer is used to point the tree (e.g. root) root = NULL; // empty tree Each node of a binary tree has both left and right subtrees which can be reached with pointers: struct tree_node{ int data; struct tree_node *left_child; struct tree_node *right_child; ; Level 1 Level 2 left_child data right_child Level 3 Exercise: Construct all possible 5 binary trees with 3 nodes. 3 4

Traversal of Binary Trees Linked lists are traversed from first to last sequentially. However, there is no such natural linear order for the nodes of a tree. Different orderings are possible for traversing a binary tree. Three of these traversal orderings are: Preorder Inorder Postorder These names are chosen according to the step at which the root node is visited. With preorder traversal the node is visited before its left and right subtrees, With inorder traversal the root is visited between the subtrees, With postorder traversal the root is visited after both subtrees. Example : root b d f Preorder: a b c d f g e Inorder: b a f d g c e Postorder: b f g d e c a a c g e Because of the recursively defined structure of a binary tree, these traversal algorithms are inherently recursive. Recursive definition of a binary tree: A binary tree is either Empty, or A node (called root) together with two binary trees (called left subtree and the right subtree of the root) Tree traversal algorithms exploit this fact. 5 6

Preorder void preorder(struct tree_node * p) { if (p!=null) { printf( %d\n, p->data); preorder(p->left_child); preorder(p->right_child); Inorder void inorder(struct tree_node *p) { if (p!=null) { inorder(p->left_child); printf( %d\n, p->data); inorder(p->right_child); Postorder Finding the maximum value in a binary tree int FindMax(struct tree_node *p) { int root_val, left, right, max; max = -1; // Assuming all values in the // are positive integers if (p!=null) { root_val = p -> data; left = FindMax(p ->left_child); right = FindMax(p->right_child); // Find the largest of the three values. if (left > right) max = left; max = right; if (root_val > max) max = root_val; return max; void postorder(struct tree_node *p) { if (p!=null) { postorder(p->left_child); postorder(p->right_child); printf( %d\n, p->data); 7 8

Adding up all values in a Binary Tree int add(struct tree_node *p) { if (p == NULL) return 0; return (p->data + add(p->left_child)+ add(p->right_child)); Binary Search Tree Binary search tree is a binary tree that is either empty or in which each node contains a data value that satisfies the following: a) all data values in the left subtree are smaller than the data value in the root. b) the data value in the root is smaller than all values in its right subtree. c) the left and right subtrees are also binary search tees. This structure allows us to quickly search for a particular value. Example: 59 18 67 13 32 63 72 25 9 10

Searching the Binary Search Tree struct tree_node{ int data; struct tree_node *left_child; struct tree_node *right_child; ; Adding Nodes to a BST In a BST, a new node will always be inserted at a NULL pointer. We never have to rearrange existing nodes to make room for a new one. Example: Add values 43, 65, 66 to the example tree given above. int treeseach( struct tree_node *p, int target) { if (p==null) return 0; if (p->data == target) return 1; if (p->data > target) return(treesearch(p->left_child)); return(treesearch(p->right_child)); 11 12

Deleting Nodes from a BST a) Deleting a leaf node: Replace the link to the deleted node by NULL. b) Deleting a node with one empty subtree. p q p p p q T1 T2 T1 T2 T1 Immediate predecessor of q T1 c) Deleting a node with both left and right subtrees. Any deleted value that has two children must be replaced by an existing value that is one of the following: - The largest value in the deleted node s left subtree - The smallest value in the deleted node s right subtree. 13 14

Exercises: 1) Write a function that will count the leaves of a binary tree. 2) Write a function that will find the height of a binary tree. (Height of an empty tree is zero). int height(struct tree_node *p) { int lefth, righth; int num_of_leaves(struct tree_node *p) { if (p == NULL) return ; /* check if it is a leaf */ if ( && ) return 1; return (num_of_leaves( + num_of_leaves( ); if (p==null) return ; { lefth = height( ); righth = height( ); if (lefth > righth) return ( ); return ( ); 15 16

3) Write a function that will interchange all left and right subtrees in a binary tree. void interchange(struct tree_node *p) { struct tree_node *temp; if (p!= NULL) { interchange ( ); interchange( ); temp = ; p->left = ; p->right = ; 17