Presentation is loading. Please wait.

Presentation is loading. Please wait.

TCSS 342 BST v1.01 BST addElement To addElement(), we essentially do a find( ). When we reach a null pointer, we create a new node there. void addElement(Object.

Similar presentations


Presentation on theme: "TCSS 342 BST v1.01 BST addElement To addElement(), we essentially do a find( ). When we reach a null pointer, we create a new node there. void addElement(Object."— Presentation transcript:

1 TCSS 342 BST v1.01 BST addElement To addElement(), we essentially do a find( ). When we reach a null pointer, we create a new node there. void addElement(Object el, BinaryTreeNode t) { if (t == null) t = new BinaryTreeNode(el, null, null); else if (el < t.element) addElement(el, t.left); else if (x > t.element) addElement(x, t.right); else ; // duplicate; do appropriate thing } Can be implemented iteratively.

2 TCSS 342 BST v1.02 findMin, findMax To find the maximum element in the BST, we follow right children until we reach NULL. To find the minimum element in the BST, we follow left children until we reach NULL. 3 117 1 84 5

3 TCSS 342 BST v1.03 BST remove Removing an item disrupts the tree structure. Basic idea: find the node that is to be removed. Then “fix” the tree so that it is still a binary search tree. Three cases: –node has no children –node has one child –node has two children

4 TCSS 342 BST v1.04 No children, one child 3 117 1 84 5 3 7 1 84 5

5 TCSS 342 BST v1.05 Two children Replace the node with its successor. Then remove the successor from the tree. 3 117 1 84 5 3 7 1 84 7

6 TCSS 342 BST v1.06 Height of BSTs n-node BST: Worst case depth: n-1. Claim: The maximum number of nodes in a binary tree of height h is 2 h+1 – 1. Proof: The proof is by induction on h. For h = 0, the tree has one node, which is equal to 2 0+1 – 1. Suppose the claim is true for any tree of height h. Any tree of height h+1 has at most two subtrees of height h. By the induction hypothesis, this tree has at most 2 (2 h+1 – 1)+1 = 2 h+2 – 1.

7 TCSS 342 BST v1.07 Height of BSTs, cont’d If we have a BST of n nodes and height h, then by the Claim, n  2 h+1 – 1. So, h  log (n+1) – 1. Average depth of nodes in a tree. Assumptions: insert items randomly (with equal likelihood); each item is equally likely to be looked up. Internal path length: the sum of the depths of all nodes.

8 TCSS 342 BST v1.08 Average Depth Let D(n) be the internal path length of some tree with n nodes. The left subtree has i nodes, and the right subtree has n–i–1 nodes. D(1) = 0 D(n) = D(i) + D(n – i – 1) + n – 1 r TLTL TRTR

9 TCSS 342 BST v1.09 D(n)  1.442 n log n How long does it take to insert the items 1, 2, 3, …, n (in that order) into a BST? What if they were inserted so that the resulting BST were perfectly balanced?


Download ppt "TCSS 342 BST v1.01 BST addElement To addElement(), we essentially do a find( ). When we reach a null pointer, we create a new node there. void addElement(Object."

Similar presentations


Ads by Google