Download presentation

Presentation is loading. Please wait.

Published byAlbert Melton Modified about 1 year ago

1
Binary Trees Chapter 6

2
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

3
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.

4
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.

5
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.

6
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.

7
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.

8
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.

9
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

10
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

11
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

12
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

13
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

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

15
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

16
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

17
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

18
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

Similar presentations

© 2017 SlidePlayer.com Inc.

All rights reserved.

Ads by Google