bst addelement n.
Download
Skip this Video
Loading SlideShow in 5 Seconds..
BST addElement PowerPoint Presentation
Download Presentation
BST addElement

Loading in 2 Seconds...

play fullscreen
1 / 9

BST addElement - PowerPoint PPT Presentation


  • 94 Views
  • Uploaded on

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

loader
I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.
capcha
Download Presentation

PowerPoint Slideshow about 'BST addElement' - moke


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.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.


- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript
bst addelement
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.

TCSS 342 BST v1.0

findmin findmax

5

4

8

1

7

11

3

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.

TCSS 342 BST v1.0

bst remove
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

TCSS 342 BST v1.0

no children one child

5

4

8

1

7

11

3

5

4

8

1

7

11

3

No children, one child

TCSS 342 BST v1.0

two children

5

4

8

1

7

11

3

7

4

8

1

7

11

3

Two children
  • Replace the node with its successor. Then remove the successor from the tree.

TCSS 342 BST v1.0

height of bsts
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 2h+1 – 1.

Proof: The proof is by induction on h. For h = 0, the tree has one node, which is equal to 20+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(2h+1 – 1)+1 = 2h+2 – 1.

TCSS 342 BST v1.0

height of bsts cont d
Height of BSTs, cont’d
  • If we have a BST of n nodes and height h, then by the Claim,

n 2h+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.

TCSS 342 BST v1.0

average depth

r

TR

TL

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

TCSS 342 BST v1.0

slide9
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?

TCSS 342 BST v1.0