Binary Tree Implementation

Slides:



Advertisements
Similar presentations
CS Fall 2012, Lab 08 Haohan Zhu. Boston University Slideshow Title Goes Here CS Fall 2012, Lab /17/2015 Tree - Data Structure  Basic.
Advertisements

S. Sudarshan Based partly on material from Fawzi Emad & Chau-Wen Tseng
SUNY Oneonta Data Structures and Algorithms Visualization Teaching Materials Generation Group Binary Search Tree A running demonstration of binary search.
© 2006 Pearson Addison-Wesley. All rights reserved11 A-1 Chapter 11 Trees.
1.1 Data Structure and Algorithm Lecture 12 Binary Search Trees Topics Reference: Introduction to Algorithm by Cormen Chapter 13: Binary Search Trees.
Binary Trees. Linear data structures Here are some of the data structures we have studied so far: –Arrays –Singly-linked lists and doubly-linked lists.
CS 261 – Winter 2010 Trees. Ubiquitous – they are everywhere in CS Probably ranks third among the most used data structure: 1.Vectors and Arrays 2.Lists.
Data Structures Arrays both single and multiple dimensions Stacks Queues Trees Linked Lists.
Recursion and Binary Tree ICS 51 – Introductory Computer Organization.
Computer Science 112 Fundamentals of Programming II Expression Trees.
Trees Chapter 8. 2 Tree Terminology A tree consists of a collection of elements or nodes, organized hierarchically. The node at the top of a tree is called.
Chapter 19 Implementing Trees and Priority Queues Fundamentals of Java.
Chapter 19 Implementing Trees and Priority Queues Fundamentals of Java.
Tree (new ADT) Terminology:  A tree is a collection of elements (nodes)  Each node may have 0 or more successors (called children)  How many does a.
Tree Data Structures.
Compiled by: Dr. Mohammad Omar Alhawarat
Trees Chapter 8. 2 Tree Terminology A tree consists of a collection of elements or nodes, organized hierarchically. The node at the top of a tree is called.
Binary Search Tree Traversal Methods. How are they different from Binary Trees?  In computer science, a binary tree is a tree data structure in which.
 Trees Data Structures Trees Data Structures  Trees Trees  Binary Search Trees Binary Search Trees  Binary Tree Implementation Binary Tree Implementation.
CS-2852 Data Structures LECTURE 11 Andrew J. Wozniewicz Image copyright © 2010 andyjphoto.com.
Week 7 - Friday.  What did we talk about last time?  Trees in general  Binary search trees.
Computer Science 112 Fundamentals of Programming II Introduction to Trees.
Binary Search Trees Data Structures Ananda Gunawardena
© Copyright 2012 by Pearson Education, Inc. All Rights Reserved. 1 Chapter 19 Binary Search Trees.
Binary Trees In computer science, a binary tree is a tree data structure in which each node has at most two children, which are referred to as the left.
Min Chen School of Computer Science and Engineering Seoul National University Data Structure: Chapter 6.
Binary Tree. Some Terminologies Short review on binary tree Tree traversals Binary Search Tree (BST)‏ Questions.
Binary Search Trees (BST)
DATA STRUCTURE BS(IT)3rd. Tree An Introduction By Yasir Mustafa Roll No. BS(IT) Bahauddin Zakariya University, Multan.
CMSC 202, Version 5/02 1 Trees. CMSC 202, Version 5/02 2 Tree Basics 1.A tree is a set of nodes. 2.A tree may be empty (i.e., contain no nodes). 3.If.
Lecture 7: Searching a Tree Neil Ghani University of Strathclyde.
More Binary Search Trees, Project 2 Bryce Boe 2013/08/06 CS24, Summer 2013 C.
Chapter 7 Trees_ Part2 TREES. Depth and Height 2  Let v be a node of a tree T. The depth of v is the number of ancestors of v, excluding v itself. 
Trees. Trees: – A trunk from the roots – Divides into branches – Ends in leaves.
Planning & System installation
Chapter 10 Trees © 2006 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved.
Trees Chapter 15.
CSCE 210 Data Structures and Algorithms
Lecture 1 (UNIT -4) TREE SUNIL KUMAR CIT-UPES.
Fundamentals of Programming II Introduction to Trees
Recursive Objects (Part 4)
Data Structures Binary Trees 1.
Planning & System installation
Week 6 - Wednesday CS221.
Binary Trees "The best time to plant a tree is twenty years ago. The second best time is now." -Chinese proverb Real programmmers always confuse Christmas.
Trees Another Abstract Data Type (ADT)
Tree.
Section 8.1 Trees.
Trees.
Binary Tree Application Expression Tree
Revised based on textbook author’s notes.
CS223 Advanced Data Structures and Algorithms
Trees Another Abstract Data Type (ADT)
Abstract Data Structures
B-Tree Insertions, Intro to Heaps
Topic 18 Binary Trees "A tree may grow a thousand feet tall, but its leaves will return to its roots." -Chinese Proverb.
Trees Definitions Implementation Traversals K-ary Trees
Trees Another Abstract Data Type (ADT)
Trees Chapter 10.
BINARY TREE CSC248 – Data Structure.
Trees.
Chapter 20: Binary Trees.
Chapter 11 Trees © 2011 Pearson Addison-Wesley. All rights reserved.
Binary Trees.
Trees.
Binary Tree Implementation And Applications
Binary Search Trees CS 580U Fall 17.
Chapter 11 Trees © 2011 Pearson Addison-Wesley. All rights reserved.
A Binary Tree is a tree in which each node has at most 2 children
Binary Tree Iterators Tree Traversals: preorder, inorder, postorder
Presentation transcript:

Binary Tree Implementation Revised based on textbook author’s notes.

Binary Tree Properties There are several properties associated with binary trees that depend on the node organization. depth – the distance of a node from the root. level – all nodes at a given depth share a level. height – number of levels in the tree. width – number of nodes on the level containing the most nodes. size – number of nodes in the tree.

Binary Tree Properties

Binary Tree Properties Given a tree of size n: max height = n min height =

Binary Tree Structure Height of a tree will be important in analyzing the efficiency of binary tree algorithms. Structural properties can play a role in the efficiency of an algorithm.

Full Binary Tree A binary tree in which each interior node contains two children.

Perfect Binary Tree A full binary tree in which all leaf nodes are at the same level.

Complete Binary Tree A binary tree of height h, is a perfect binary tree down to height h – 1 and the nodes at the lowest level are filled from left to right (no gaps).

Binary Tree Implementation Many different implementations. We’ll discuss two. Linked node based Array based

Linked node based # The storage class for creating binary tree nodes. class BinTreeNode : def __init__( self, data ): self.data = data self.left = None self.right = None def set_left(self, leftnode): ”””Set the incoming node as the left child””” self.left = leftnode ”””similar functions follow””” def set_right(self, rightnode): def set_data(self, new_data): def get_data(self): def get_left(self): def get_right(self): bintreenode.py

Physical Implementation testbintree.py

Tree Traversals Iterates through the nodes of a tree, one node at a time in order to visit every node. With a linear structure this was simple. How is this done with a hierarchical structure? Must begin at the root node. Every node must be visited. Results in a recursive solution.

Preorder Traversal After visiting the root, traverse the nodes in the left subtree then traverse the nodes in the right subtree.

Preorder Traversal

Preorder Traversal The implementation is rather simple. Given a binary tree of size n, a complete traversal requires O(n) to visit every node. def preorderTrav( subtree ): if subtree is not None : print( subtree.data ) preorderTrav( subtree.left ) preorderTrav( subtree.right )

Inorder Traversal Similar to the preorder traversal, but we traverse the left subtree before visiting the node.

Inorder Traversal The implementation swaps the order of the visit operation and the recursive calls. def inorderTrav( subtree ): if subtree is not None : inorderTrav( subtree.left ) print( subtree.data ) inorderTrav( subtree.right )

Postorder Traversal Is the opposite of the preorder traversal. Traverse both the left and right subtrees before visiting the node.

Postorder Traversal The implementation swaps the order of the visit operation and the recursive calls. def postorderTrav( subtree ): if subtree is not None : postorderTrav( subtree.left ) postorderTrav( subtree.right ) print( subtree.data )

Breadth-First (level order) Traversal The nodes are visited by level, from left to right. The previous traversals are all depth-first traversals.

Breadth-First Traversal Recursion can not be used with this traversal. We can use a queue and an iterative loop. def breadthFirstTrav( bintree ): Queue q q.enqueue( bintree ) while not q.isEmpty() : # Remove the next node from the queue and visit it. node = q.dequeue() print( node.data ) # Add the two children to the queue. if node.left is not None : q.enqueue( node.left ) if node.right is not None : q.enqueue( node.right )

Array based binary trees It is very nature to implement binary trees using linked nodes. For binary tree that has “many” nodes, it may be more effective and efficient to implement it using an array!