1 / 18

Binary Trees

Binary Trees. Chapter 6. Linked Lists Suck. 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. We need a better structure… So we get binary trees. Tree definition.

tristana
Télécharger la présentation

Binary Trees

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Binary Trees Chapter 6

  2. Linked Lists Suck • 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. • We need a better structure… • So we get binary trees

  3. Tree definition • Here is a (recursive, of course) definition for a tree: • An empty structure is an empty tree • If t1,…,tk are disjointed trees, then the structure whose root has as its children the roots of t1,…,tk is also a tree • 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. • 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 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. • 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.

  6. Binary Trees • 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 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.

  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.

  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. • 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 • For a tree of n nodes there are n! traversals • Of course most of those are hard to program • There are two very common traversals • Breadth 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. • Usually in a left to right fashion • 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 • There are three common depth first traversals • Inorder • Preorder • Postorder • 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 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 • There are two common ways to do this • 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 • It simplifies some of the tests • 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() < data ) setLeft( InsertionHelper( n->getLeft(), data); else setRight( InsertionHelper( n->getRight(), data); }

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

  16. Deletion by Merging • Deletion by merging takes two subtrees and merges them together into one tree • The idea is you have a node n to delete • N can have two children • 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 • 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 • If the node n to be deleted has no children, • easy blow it away • 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 • If n has two child, • Now we have deletion by copying

  18. Details • 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 then blow away the node that had the smallest value in it

More Related