Binary Trees Chapter 6. Linked Lists Suck By now you realize that the title to this slide is true… By now you realize that the title to this slide is.

Slides:



Advertisements
Similar presentations
Binary Trees, Binary Search Trees CMPS 2133 Spring 2008.
Advertisements

Binary Trees, Binary Search Trees COMP171 Fall 2006.
CS 171: Introduction to Computer Science II
Fall 2007CS 2251 Trees Chapter 8. Fall 2007CS 2252 Chapter Objectives To learn how to use a tree to represent a hierarchical organization of information.
1 Trees. 2 Outline –Tree Structures –Tree Node Level and Path Length –Binary Tree Definition –Binary Tree Nodes –Binary Search Trees.
Lists A list is a finite, ordered sequence of data items. Two Implementations –Arrays –Linked Lists.
Binary Trees A binary tree is made up of a finite set of nodes that is either empty or consists of a node called the root together with two binary trees,
Introduction to Data Structure, Fall 2006 Slide- 1 California State University, Fresno Introduction to Data Structure Chapter 10 Ming Li Department of.
© 2006 Pearson Addison-Wesley. All rights reserved11 A-1 Chapter 11 Trees.
Starting Out with C++: Early Objects 5/e © 2006 Pearson Education. All Rights Reserved Starting Out with C++: Early Objects 5 th Edition Chapter 19 Binary.
C o n f i d e n t i a l HOME NEXT Subject Name: Data Structure Using C Unit Title: Trees.
Binary Trees Chapter 6.
Joseph Lindo Trees Sir Joseph Lindo University of the Cordilleras.
Data Structures Arrays both single and multiple dimensions Stacks Queues Trees Linked Lists.
Binary Tree. Binary Trees – An Informal Definition A binary tree is a tree in which no node can have more than two children Each node has 0, 1, or 2 children.
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: Binary Trees. Objectives In this chapter, you will: – Learn about binary trees – Explore various binary tree traversal algorithms – Organize.
Lecture 10 Trees –Definiton of trees –Uses of trees –Operations on a tree.
Spring 2010CS 2251 Trees Chapter 6. Spring 2010CS 2252 Chapter Objectives Learn to use a tree to represent a hierarchical organization of information.
1 Trees A tree is a data structure used to represent different kinds of data and help solve a number of algorithmic problems Game trees (i.e., chess ),
CMSC 341 Introduction to Trees. 8/3/2007 UMBC CMSC 341 TreeIntro 2 Tree ADT Tree definition  A tree is a set of nodes which may be empty  If not empty,
Chapter 6 Binary Trees. 6.1 Trees, Binary Trees, and Binary Search Trees Linked lists usually are more flexible than arrays, but it is difficult to use.
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.
Binary Trees, Binary Search Trees RIZWAN REHMAN CENTRE FOR COMPUTER STUDIES DIBRUGARH UNIVERSITY.
Tree Data Structures.
Binary Search Trees Binary Search Trees (BST)  the tree from the previous slide is a special kind of binary tree called a binary.
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.
Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy Walters, and Godfrey Muganda Modified for use at Midwestern State University Chapter.
Topics Definition and Application of Binary Trees Binary Search Tree Operations.
Tree Traversals, TreeSort 20 February Expression Tree Leaves are operands Interior nodes are operators A binary tree to represent (A - B) + C.
Trees By P.Naga Srinivasu M.tech,(MBA). Basic Tree Concepts A tree consists of finite set of elements, called nodes, and a finite set of directed lines.
1 Chapter 7 Objectives Upon completion you will be able to: Create and implement binary search trees Understand the operation of the binary search tree.
Binary Search Trees (BSTs) 18 February Binary Search Tree (BST) An important special kind of binary tree is the BST Each node stores some information.
CMSC 341 Introduction to Trees. 2/21/20062 Tree ADT Tree definition –A tree is a set of nodes which may be empty –If not empty, then there is a distinguished.
Binary Tree. Some Terminologies Short review on binary tree Tree traversals Binary Search Tree (BST)‏ Questions.
Binary Search Trees (BST)
Rooted Tree a b d ef i j g h c k root parent node (self) child descendent leaf (no children) e, i, k, g, h are leaves internal node (not a leaf) sibling.
TREES K. Birman’s and G. Bebis’s Slides. Tree Overview 2  Tree: recursive data structure (similar to list)  Each cell may have zero or more successors.
Copyright © 2012 Pearson Education, Inc. Chapter 20: Binary Trees.
1 Trees General Trees  Nonrecursive definition: a tree consists of a set of nodes and a set of directed edges that connect pairs of nodes.
1 Joe Meehean. A A B B D D I I C C E E X X A A B B D D I I C C E E X X  Terminology each circle is a node pointers are edges topmost node is the root.
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.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 20: Binary Trees.
Question 4 Tutorial 8. Part A Insert 20, 10, 15, 5,7, 30, 25, 18, 37, 12 and 40 in sequence into an empty binary tree
Fundamentals of Algorithms MCS - 2 Lecture # 17. Binary Search Trees.
1 Trees. 2 Trees Trees. Binary Trees Tree Traversal.
Tree Representation and Terminology Binary Trees Binary Search Trees Pointer-Based Representation of a Binary Tree Array-Based Representation of a Binary.
DS.T.1 Trees Chapter 4 Overview Tree Concepts Traversals Binary Trees Binary Search Trees AVL Trees Splay Trees B-Trees.
Week 6 - Wednesday CS221.
Binary Search Tree (BST)
Section 8.1 Trees.
Trees.
ITEC 2620M Introduction to Data Structures
Binary Trees, Binary Search Trees
Chapter 20: Binary Trees.
Chapter 21: Binary Trees.
Find in a linked list? first last 7  4  3  8 NULL
CSE 373, Copyright S. Tanimoto, 2002 Binary Trees -
CMSC 202 Trees.
Binary Trees, Binary Search Trees
Trees.
CSE 373, Copyright S. Tanimoto, 2001 Binary Trees -
CSC 143 Java Trees.
Chapter 20: Binary Trees.
Binary Trees.
Trees.
Binary Trees, Binary Search Trees
Data Structures Using C++ 2E
Chapter 11 Trees © 2011 Pearson Addison-Wesley. All rights reserved.
NATURE VIEW OF A TREE leaves branches root. NATURE VIEW OF A TREE leaves branches root.
Presentation transcript:

Binary Trees Chapter 6

Linked Lists Suck By now you realize that the title to this slide is true… By now you realize that the title to this slide is true… When we are talking about searching or representing data structures that need a hierarchical structures. When we are talking about searching or representing data structures that need a hierarchical structures. We need a better structure… We need a better structure… So we get binary trees So we get binary trees

Tree definition Here is a (recursive, of course) definition for a tree: Here is a (recursive, of course) definition for a tree: 1. An empty structure is an empty tree 2. If t1,…,tk are disjointed trees, then the structure whose root has as its children the roots of t1,…,tk is also a tree 3. Only structures generate by rules 1 and 2 are trees.

More terminology Each node has to be reachable from the roots through a unique sequence of arcs called a path. Each node has to be reachable from the roots through a unique sequence of arcs called a path. The number of arcs in a path is called the length of the path. The number of arcs in a path is called the length of the path. The level of a node is the length of the path from the root to the node plus 1. The level of a node is the length of the path from the root to the node plus 1. The height of a non-empty tree is the maximum level of a node in the tree. The height of a non-empty tree is the maximum level of a node in the tree.

Special Trees An empty tree has a height of zero. An empty tree has a height of zero. A single node tree is a tree of height 1. A single node tree is a tree of height 1. This is the only case where a node is both a root and a leaf. This is the only case where a node is both a root and a leaf.

Binary Trees According to the definition of trees, a node can have any number of children. According to the definition of trees, a node can have any number of children. A binary tree is restricted to only having 0, 1, or 2 children. A binary tree is restricted to only having 0, 1, or 2 children. A complete binary tree is one where all the levels are full with exception to the last level and it is filled from left to right. A complete binary tree is one where all the levels are full with exception to the last level and it is filled from left to right. A full binary tree is one where if a node has a child, then it has two children. A full binary tree is one where if a node has a child, then it has two children.

Full Binary Tree Theorem For all the nonempty binary trees whose nonterminal node have exactly two nonempty children, the number of leaves m is greater than the number of nonterminal node k and m = k + 1. For all the nonempty binary trees whose nonterminal node have exactly two nonempty children, the number of leaves m is greater than the number of nonterminal node k and m = k + 1.

Binary Search Trees A binary search tree (BST) is a binary tree that has the following property: For each node n of the tree, all values stored in its left subtree are less than value v stored in n, and all values stored in the right subtree are greater than v. A binary search tree (BST) is a binary tree that has the following property: For each node n of the tree, all values stored in its left subtree are less than value v stored in n, and all values stored in the right subtree are greater than v. This definition excludes the case of duplicates. They can be include and would be put in the right subtree. This definition excludes the case of duplicates. They can be include and would be put in the right subtree.

Binary Tree Traversals A traversal is where each node in a tree is visited and visited once A traversal is where each node in a tree is visited and visited once For a tree of n nodes there are n! traversals For a tree of n nodes there are n! traversals Of course most of those are hard to program Of course most of those are hard to program There are two very common traversals There are two very common traversals Breadth First Breadth First Depth First Depth First

Breadth First In a breadth first traversal all of the nodes on a given level are visited and then all of the nodes on the next level are visited. In a breadth first traversal all of the nodes on a given level are visited and then all of the nodes on the next level are visited. Usually in a left to right fashion Usually in a left to right fashion This is implemented with a queue This is implemented with a queue

Depth First In a depth first traversal all the nodes on a branch are visited before any others are visited In a depth first traversal all the nodes on a branch are visited before any others are visited There are three common depth first traversals There are three common depth first traversals Inorder Inorder Preorder Preorder Postorder Postorder Each type has its use and specific application Each type has its use and specific application

Insertion In order to build a tree you must be able to insert into the tree In order to build a tree you must be able to insert into the tree In order to do this you need to know where the nodes goes In order to do this you need to know where the nodes goes Typically the tree is searched looking for a null pointer to hang the new element from Typically the tree is searched looking for a null pointer to hang the new element from There are two common ways to do this There are two common ways to do this Use a look ahead or check for null as the first line in the code Use a look ahead or check for null as the first line in the code

More insertion I prefer to check for null as the first thing I do in my code I prefer to check for null as the first thing I do in my code It simplifies some of the tests It simplifies some of the tests And makes for a really easy to check for base case And makes for a really easy to check for base case

Code InsertionHelper( Node *n, T data ) { if ( node == 0 ) return new Node( data ); if ( n->getData() getData() < data ) setLeft( InsertionHelper( n->getLeft(), data); else setRight( InsertionHelper( n->getRight(), data); }

Deletion Deletion poses a bigger problem Deletion poses a bigger problem When we delete we normally have two choices When we delete we normally have two choices Deletion by merging Deletion by merging Deletion by copying Deletion by copying

Deletion by Merging Deletion by merging takes two subtrees and merges them together into one tree Deletion by merging takes two subtrees and merges them together into one tree The idea is you have a node n to delete The idea is you have a node n to delete N can have two children N can have two children So you find the smallest element in n’s left subtree So you find the smallest element in n’s left subtree You then take n’s right subtree and merge it to the bottom of the left subtree You then take n’s right subtree and merge it to the bottom of the left subtree The root of the left subtree replaces n The root of the left subtree replaces n

Deletion by copying This will simply swap values and reduce a difficult case to an easier one This will simply swap values and reduce a difficult case to an easier one If the node n to be deleted has no children, If the node n to be deleted has no children, easy blow it away easy blow it away If it has one child If it has one child Easy simply pass n’s child pointer up, make n’s parent point to n’s child and blow n away Easy simply pass n’s child pointer up, make n’s parent point to n’s child and blow n away If n has two child, If n has two child, Now we have deletion by copying Now we have deletion by copying

Details We find the smallest value in n’s right subtree We find the smallest value in n’s right subtree We will take the value from that node and put it in place of the value in n We will take the value from that node and put it in place of the value in n We will then blow away the node that had the smallest value in it We will then blow away the node that had the smallest value in it