Chapter 5. Binary Trees

Similar documents
Binary Trees

Binary Trees, Binary Search Trees

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

Chapter 20: Binary Trees

Trees, Binary Trees, and Binary Search Trees

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

Programming II (CS300)

Trees. Truong Tuan Anh CSE-HCMUT

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

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

! 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

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

CMSC 341 Lecture 15 Leftist Heaps

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

Binary Trees and Binary Search Trees

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

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

CMSC 341 Lecture 15 Leftist Heaps

TREES. Trees - Introduction

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

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

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

Algorithms and Data Structures

CS102 Binary Search Trees

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

CE 221 Data Structures and Algorithms

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

Trees. CSE 373 Data Structures

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

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

CSI33 Data Structures

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

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

MID TERM MEGA FILE SOLVED BY VU HELPER Which one of the following statement is NOT correct.

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

Binary Trees. Examples:

Lecture 6: Analysis of Algorithms (CS )

Binary Trees. Height 1

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

March 20/2003 Jayakanth Srinivasan,

CMSC 341 Leftist Heaps

Computational Optimization ISE 407. Lecture 16. Dr. Ted Ralphs

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

Principles of Computer Science

CSCI Trees. Mark Redekopp David Kempe

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

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

Trees. A tree is a directed graph with the property

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

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

Advanced Java Concepts Unit 5: Trees. Notes and Exercises

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

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

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

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

Binary Search Trees. What is a Binary Search Tree?

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

FINALTERM EXAMINATION Fall 2009 CS301- Data Structures Question No: 1 ( Marks: 1 ) - Please choose one The data of the problem is of 2GB and the hard

Data Structures and Algorithms

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

Traversing Trees with Iterators

Traversing Trees with Iterators

Sorted Arrays. Operation Access Search Selection Predecessor Successor Output (print) Insert Delete Extract-Min

Data Structure Advanced

Stacks, Queues and Hierarchical Collections

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

Garbage Collection: recycling unused memory

SFU CMPT Lecture: Week 9

Why Do We Need Trees?

CS24 Week 8 Lecture 1

Data Structure - Binary Tree 1 -

BBM 201 Data structures

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

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

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

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

Associate Professor Dr. Raed Ibraheem Hamed

CS302 - Data Structures using C++

Data Structures and Algorithms

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

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

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

CMSC 341 Lecture 10 Binary Search Trees

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

Stacks, Queues and Hierarchical Collections. 2501ICT Logan

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

CS 206 Introduction to Computer Science II

Algorithms in Systems Engineering ISE 172. Lecture 16. Dr. Ted Ralphs

8. Write an example for expression tree. [A/M 10] (A+B)*((C-D)/(E^F))

Programming II (CS300)

Algorithms. Deleting from Red-Black Trees B-Trees

Trees. Carlos Moreno uwaterloo.ca EIT

Trees. Tree Structure Binary Tree Tree Traversals

Programming II (CS300)

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

MULTIMEDIA COLLEGE JALAN GURNEY KIRI KUALA LUMPUR

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

Topic 14. The BinaryTree ADT

CSI33 Data Structures

Transcription:

Chapter 5 Binary Trees

Definitions and Properties A binary tree is made up of a finite set of elements called nodes It consists of a root and two subtrees There is an edge from the root to its children If n 1, n 2, n k is a sequence of nodes such that n i is the parent of n i+1, then that sequence is a path The length of the path is k-1k The depth of node M is the length of the path from the root to M

More definitions The height of the tree is one more than the depth of the deepest node A leaf node is any node with no children An internal node is a node with at least one child A full binary tree has each node as either an internal node with two children or as a leaf A complete binary tree has each level filled and the last level may not be filled, but it is filled from left to right

Binary Tree Theorems The number of leaves in a non-empty full binary tree is one more than the number of internal nodes. The number of empty subtrees in a non-empty binary tree is one more than the number of nodes in the tree These are useful when you need to compute the amount of overhead used by a tree

Binary Tree ADT There are activities that relate to nodes Reach a child Get a value There are activities that relate to trees Initialization Clearing Due to these activities the Node class should be separate from the Tree class

Binary Tree Traversals Any process for visiting all the nodes in some order is a traversal Any traversal that visits each node only one time is an enumeration There are three main kinds of enumerations on trees Preorder Postorder Inorder

Traversal Function The traversal function is easily written as a recursive function An important design decision is when to check for empty subtrees You can check at the beginning of the function, returning if you are a null pointer You can check before you call the recursive function The first choice is better

Why? We would have to use two checks for a null tree versus one. We make twice as many function calls. We are required that an empty tree is never passed in or make an additional check for an empty tree In any case, it gets complicated and error prone

Binary Tree Node Implementations Normally nodes contain a data field value and two pointers to children Some programmers find it convenient to add a pointer to the parent This is somewhat analogous to a pointer to a previous node in a Linked List However, it is almost always unnecessary and add unneeded overhead

More Binary Nodes An important decision is whether the same class can be used for internal and leaf nodes Some applications only store data in the leaf nodes. Other applications require one type of data in an internal node and another in the leaves. There are some choices in what to do

Union Construct enum Nodetype {leaf, internal}; class VarBinNode { // Generic node class public: Nodetype mytype; // Store type for node union { struct { // internal node VarBinNode* left; // Left child VarBinNode* right; // Right child Operator opx; // Value } intl; Operand var; // Leaf: Value only };

Huh? In the union construct, you get one or the other, but not both So we either get the pointers to the children and the operator (internal node) Or we get the value (leaf node) Problem is the data structure store enough space for the biggest subtype

Better Approach Use inheritance Have a base class that implements a general node Inherit from the base class one for each type of node The method isleaf() returns true for leaves and false otherwise In this option the tree handles traversing the nodes. This method uses polymorphism to handle which member functions get called

Third Approach This approach also uses inheritance. The major difference is that the nodes know how to traverse themselves. Meaning that each subclass implements a traverse function that is polymorphically called. This is known as a Composite design pattern

Pros and Cons When comparing the two inheritance methods The first does not require that the node classes explicitly support a traverse function This fact makes it easy to add new node types The second approach relieves the tree class from needing to know anything about the internals of the node classes. Secondly, the traverse function does not need to know all the node types

Space Requirements Assuming every node has two pointers and a data spot For n nodes we get n(2p + d) The total overhead required is 2pn for the entire tree The fraction of overhead is 2p/(2p+d) If p = d, then about 2/3 of the tree is overhead

Space for Full Trees If only the leaves store data, then we can get great savings in space. (n/2(2p))/(n/2(2p)+d) = p/(p+d) When p = d we get about ½ of the space is overhead This is much better.

Array Implementation for Complete Binary Trees We begin by assigning numbers to the node position in the complete binary tree, level by level. An array can store the tree s s data value efficiently, placing each value in the array position corresponding to the nodes position Simple formulas can be determined to calculate siblings, parents, and children.

10 10 8 6 4 2 Right Sibling Right Sibling 9 7 5 3 1 Left Sibling Left Sibling 10 10 8 6 4 2 Right Child Right Child 11 11 9 7 5 3 1 Left Child Left Child 5 4 4 3 3 2 2 1 1 0 0 Parent Parent 11 11 10 10 9 8 7 6 5 4 3 2 1 0 Position Position

Formulas Parent(r) = (r-1)/2 if r 0 Left Child(r) = 2r+1 if 2r+1 < n Right Child(r) = 2r+2 if 2r+2 < n Left sibling(r) = r-1 r 1 if r is even Right sibling(r) = r+1 is odd and r+1 < n

Binary Search Tree Binary Search Tree (BST) Property: All nodes stored in the left subtree of a node whose key is K have key values less than K. All nodes stored in the right subtree of a node whose key value is K have key values greater than or equal to K.

Method Example bool find(const Key& k) const { return findhelp(root, K); } bool findhelp(binnode* subroot, const Key& k) const { if(subroot == NULL) return false; else if( k < subroot->val() ) return findhelp(subroot->left(), k); else if( k > subroot->val() ) return findhelp(subroot->right(), k); else return true; }

Insertion Insertion requires that the location for the record is first located The recursion ends when the subroot is null Either the node is an internal node with an open child on the correct side or the node is a leaf A new node is created and the node is inserted in the tree Question: how does the parent know who their new child is?

Insertion Ideas Insert uses an inserthelper function to actually perform the insertion. inserthelper returns a pointer to itself This allows the parent to set the correct pointer to point to the new child

Removing Nodes This is a bit trickier than insertion. Let's look at the smaller problem of removing the smallest item from a given subtree We will traverse down the left child pointers until we find a node that does not have a left child We will call that node min and return a reference to it We will also return a pointer to its right subchild

More Deletions So to delete a general key value R, we first find it and check its children If it is a leaf, we blow it away If it has one subchild, we have the node's parent point to it and we are done If it has two children, well we have to do more work What can we do?

More Deletions We can find a value to substitute for this value that will keep the BST property Two choices, The greatest value from the left subtree The least value from the right subtree Well, we find the smallest value in the nodes right subtree. Why?

Threaded Trees All previously discussed tree traversals required a stack Either the stack is created and maintained explicitly by the user or created by the system as the run-time stack. So there is there another way to traverse a tree without a stack? Of course there is otherwise why would we be talking about it.

Stackless Depth-First Traversal Threaded trees allow you traverse the tree by following pointers stored within the tree Each node would store pointers to its predecessor and successor This would create a lot of overhead with the additional two pointers for a total of 4 pointers per node So what can we do?

It s s all in the context You ve already got two pointers in the nodes, why not use them? Of course you need to know what pointers are currently referring to, their context You can use a bit to indicate what the pointer is being used for. How does this work?

Inorder example void ThreadedTree<T>:: <T>::inorder(){ ThreadedNode<T> *prev* prev,, *p = root; if( p!= 0 ) { while ( p->left p!= 0 ) p = p->left; p while( p!= 0 ) { visit( p ); prev = p; p = p->right; p if ( p!= 0 && prev->successor == 0) while( p->left p!= 0 ) p = p->left; p } } }

Other Methods You can also reorganize the tree to traverse the tree without stacks and without threads It is easy to traverse a tree with no left child One such algorithm is: MorrisInorder() While not finished If node has no left descendant Visit it; Go to the right Else make this node the right child of the rightmost node in its left descendants, go to this left descendant

Heaps and Priority Queues There are many cases where we want to choose the next most important item from a list This occurs in hospitals, OSs, etc. Normal queues are not efficient enough to implement a priority queue. We could use a binary tree to help with this problem

Heaps A heap is a complete binary tree, and so it is almost always implemented in an array. The values stored in a heap are partially ordered This means that there is a relationship between the value stored at any node and the values of its children. There are two variations

Min Heaps and Max Heaps The min heap stores at every node a value that is less than or equal to that of its children. The root stores the smallest value The max heap stores at every node a value that is greater than or equal to that if its children The root stores the greatest value

Building a Heap (a) (4-2) (4-1) (2-1) (5-2) (5-4) (6-3) (6-5) (7-5) (7-6) (b) (5-2), (7-3), (7-1), (6-1)

Details Assume you have a root and its children are already heaps. You siftdown the root to the correct location. If the root is greater than its children you are done If not exchange the root with its bigger child and repeat at the next level Where do you start this process? At the last internal node.

Example

Removing Removing requires that we replace the root with a value so that we maintain the shape of the tree. Take the last node and swap it with the root. Call siftdown on the root.

Cost to Build a Heap Cost for heap construction: log n (i - 1) n/2 i n. i=1