Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "Binary Trees Terminology A graph G = is a collection of nodes and edges. An edge (v 1,v 2 ) is a pair of vertices that are directly connected. A path,"— Presentation transcript:

1 Binary Trees Terminology A graph G = is a collection of nodes and edges. An edge (v 1,v 2 ) is a pair of vertices that are directly connected. A path, P, is a sequence of vertices where and (v i,v i+1 ) is an edge of G. A path 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 Trees Assuming 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 Structure Question: 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/Deletion pseudo code Insert node Perform Find If node was found, Return false If tree was empty, Set root = node Else If node < 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 If node 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 Remove successor Replace node with successor in the tree, Return true

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 Traversal See 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)


Download ppt "Binary Trees Terminology A graph G = is a collection of nodes and edges. An edge (v 1,v 2 ) is a pair of vertices that are directly connected. A path,"

Similar presentations


Ads by Google