1 / 7

Binary Trees Terminology

Binary Trees Terminology. A graph G = <V,E> is a collection of nodes and edges . An edge (v 1 ,v 2 ) is a pair of vertices that are directly connected.

lelia
Download Presentation

Binary Trees Terminology

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 TreesTerminology • A graph G = <V,E> is a collection of nodes and edges. An edge (v1,v2) is a pair of vertices that are directly connected. • A path, P, is a sequence of vertices <v1,v2,…,vn> whereand (vi,vi+1) is an edge of G. A path <v1,v2,…,vn,v1> is a cycle. • A tree is a graph without cycles. The Root is a distinguished node of the tree. The Parent is the adjacent node that is closer to the root. A Leaf is a node without children. A child is an adjacent node that is further from the root. The level of a vertex is the length of the path from the root. • A binary tree is a tree with at most two children

  2. Advantage of TreesAssuming Deletions exclude time for Find • Ordered Array • Insert O(N), Delete O(N), Find O(Lg N). • Unordered Array • Insert O(1), Delete O(1), Find O(N). • Linked List • Insert O(1), Delete O(1), Find O(N). • Binary Trees • Insert O(1), Delete O(1), Find O(lg N) in most cases. • Question: When would Find not take place in O(Lg N)? Note: the above insert and removed complexities don’t include the find

  3. Tree StructureQuestion: Can a Tree be implemented using an array? typedef struct TreeNode { Key key; Data data; struct TreeNode left; struct TreeNode right; } Tree Top of the Tree: Tree root; Basic Tree Abstract Data Type Methods Node find(Key key) {} Item insert(Key key, Data data ) {} Item delete(Key key) {}

  4. Binary Tree Find Node find( Key key) { Node *current = root, *previous; previous = null; if (current == null) return null; while(!equal(current->key, key)) { previous = current if (greater(current->key, key)) current = current->left; else current = current->right; if (current == null) return previous; } return previous } Notes -> in C is equivalent to “.” in Java Java has no equivalent to “.” in C

  5. Binary Tree Insertion/Deletionpseudo code • Insert node Perform Find If node was found, Return false If tree was empty, Setroot = node ElseIfnode < leaf node returned by Find Link node to left child of leaf, Return true Else Link node to right child of leaf, Return true • Delete node Perform Find Ifnode was not found, Return false If node was a leaf, Unlink parent left or right pointer, Return true If node had one child, Link child to appropriate parent pointer, Return true Find and Removesuccessor Replacenode with successor in the tree, Returntrue

  6. Other Search Tree Operations • Find Minimum: • Go left until the leaf is found • Find Maximum: • Go right until the leaf is found • Predecessor: • Go left, and the proceed right until the leaf is found • Successor: • Go right, and then proceed left until the leaf is found

  7. Binary Tree TraversalSee Tree Example in Text • Preorder Traversal • Recursively Call until leaf is found • Recursive step • Visit, Traversal(Left), Traversal(Right) • Inorder (Traversal(left), Visit, Traversal(right)) • Postorder (Traversal(left), Traversal(right), Visit)

More Related