0% found this document useful (0 votes)
12 views3 pages

23 BSTs

This document discusses trees and binary search trees. It defines what a tree is as a set of nodes connected by edges, with exactly one path between any two nodes. It describes properties of rooted trees including leaves, siblings, ancestors, descendants, depth, height, and subtrees. It then defines binary trees as trees where each node has at most two children, and binary search trees where all nodes in the left subtree of a node have keys less than or equal to the node's key, and all nodes in the right subtree have keys greater than or equal. It describes common tree traversal orders and how operations like search, minimum, maximum, insertion and deletion can be performed on binary search trees in O(h) time where h

Uploaded by

Nam Nguyễn
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views3 pages

23 BSTs

This document discusses trees and binary search trees. It defines what a tree is as a set of nodes connected by edges, with exactly one path between any two nodes. It describes properties of rooted trees including leaves, siblings, ancestors, descendants, depth, height, and subtrees. It then defines binary trees as trees where each node has at most two children, and binary search trees where all nodes in the left subtree of a node have keys less than or equal to the node's key, and all nodes in the right subtree have keys greater than or equal. It describes common tree traversal orders and how operations like search, minimum, maximum, insertion and deletion can be performed on binary search trees in O(h) time where h

Uploaded by

Nam Nguyễn
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

Tree

• A tree consists of a set of nodes and a set of edges that


connect pairs of nodes.
• Property: there is exactly one path (no more, no less)
between any two nodes of the tree.
• A path is a connected sequence of zero or more edges.
Trees, Binary Search Tree • In a rooted tree, one distinguished node is called the
root. Every node c, except the root, has exactly one
parent node p, which is the first node traversed on the
path from c to the root. c is p's child.
Bryn Mawr College • The root has no parent.
CS246 Programming Paradigm • A node can have any number of children.

Rooted Tree Terminology Rooted Tree Terminology (cont.)


• A leaf is a node with no children. • The height of a node n is the length of the path
• Siblings are nodes with the same parent. from n to its deepest descendant. (The height of a
• The ancestors of a node d are the nodes on the path leaf is zero.)
from d to the root. These include d's parent, d's parent's • The height of a tree is the depth of its deepest node
parent, d's parent's parent's parent, and so forth up to the = height of the root.
root. Note that d's ancestors include d itself. The root is
• The subtree rooted at node n is the tree formed by n
an ancestor of every node in the tree.
and its descendants.
• If a is an ancestor of d, then d is a descendant of a.
• A binary tree is a tree in which no node has more
• The length of a path is the number of edges in the path.
than two children, and every child is either a left child
• The depth of a node n is the length of the path from n or a right child, even if it is the only child its parent
to the root. (The depth of the root is zero.) has.

Binary Trees Representing Rooted Trees


Rooted trees can also be defined recursively. Here is the • A direct way to represent a tree is to use a data structure
where every node has three references:
definition of a binary tree: o one reference to the object stored at that node,
• A binary tree T is a structure defined on a finite set o one reference to the node's parent, and
o one reference to the node's children.
of nodes that either
• The child-sibling (CS) representation is another popular tree
o Contains no nodes, or representation. It spurns separately encapsulated linked lists
so that siblings are directly linked.
o Is composed of three disjoint sets of nodes: o It retains the item and parent references, but instead of
• a root node, referencing a list of children, each node references just its
leftmost child.
• a binary tree called the left subtree of T, and o Each node also references its next sibling to the right.
• a binary tree called the right subtree of T. o These nextSibling references are used to join the children of a
node in a singly-linked list, whose head is the node's firstChild.

1  
Binary Search Trees Invalid BSTs
• The binary-search-tree property
o If node y in left subtree of node x, then key[y] ≤ key[x].
o If node y in right subtree of node x, then key[y] ≥ key[x].
• Binary search trees are an important data
structure that supports dynamic set
operations:
o Search, Minimum, Maximum, Predecessor, Successor,
Insert, and Delete.
o Basic operations take time proportional to the height of
the tree – O(h).
• Q: Where is the minimum/maximum key?

Binary Tree Traversals Inorder Traversal of BST


50

30 55

25 53 60
35
10
31 62
37
20 Inorder-Tree-Walk (x)
1. if x ≠ NIL
Prints out keys in sorted order: 2. then Inorder-Tree-Walk(left[x])
10, 20, 25, 30, 31, 35, 37, 50, 53, 55, 60, 62 3. print key[x]
4. Inorder-Tree-Walk(right[x])

Querying a Binary Search Tree Tree Search


• All dynamic-set search operations can be supported Search for 37 < 50 ≥ Running time O(h)
in O(h) time. where h is tree height
30 55
• h = Θ(lg n) for a balanced binary tree (and for an <

≥ <
average tree built by adding nodes in random
order.) < 25 53 60
35 ≥
• h = Θ(n) for an unbalanced tree that resembles a < ≥
10
62
linear chain of n nodes in the worst case. ≥ 31 37
Tree-Search(x, k)
20 1. if x = NIL or k = key[x]
2. then return x
3. if k < key[x]
4. then return Tree-Search(left[x], k)
5. else return Tree-Search(right[x], k)

2  
Iterative Tree Search Finding Min & Max
Search for 37 < 50 • The binary-search-tree property guarantees that:
≥ Running time O(h) o The minimum is located at the left-most node.
where h is tree height
< 30 55 o The maximum is located at the right-most node.
< ≥

Tree-Minimum(x) Tree-Maximum(x)
< 25 53 60 1. while left[x] ≠ NIL 1. while right[x] ≠ NIL
35 ≥
< 2. do x ← left[x] 2. do x ← right[x]
≥ 3. return x 3. return x
10
≥ 62
31 37
Iterative-Tree-Search(x, k) o Question: how long do they take?
20 1. while x ≠ NIL and k ≠ key[x]
2. do if k < key[x]
3. then x ← left[x]
4. else x ← right[x]
5. return x

Predecessor & Successor Pseudo-code for Successor


10, 20, 23, 25, 30, 31, 35, 37, 50, 53, 55, 60, 62 Tree-Successor(x)
x 1. if right[x] ≠ NIL
50
The successor of y is lowest 2. then return Tree-Minimum(right[x])
ancestor whose left child 30 3. y ← p[x]
55
is y or an ancestor of y 4. while y ≠ NIL and x = right[y]
5. do x ← y
25 53 60
35 6. y ← p[y]
The successor of x 7. return y
10
31 is the leftmost node 62
37
in its right subtree
• Code for predecessor is symmetric.
20 The predecessor of x is The successor of the • Running time: O(h)
the rightmost node in largest key is NIL
no right subtree
23 its left subtree
y

Insertion Example BST Insertion : Pseudo-code


Example: insert z = 32 Tree-Insert(T, z) • Beginning at root of the tree,
trace a downward path,
Compare 32 and 25 1. y ← NIL maintaining two pointers.
x 25
traverse the right subtree 2. x ← root[T] o Pointer x: traces the downward path.
o Pointer y: “trailing pointer” to keep track
20 3. while x ≠ NIL of parent of x.
35 • Traverse the tree downward by
4. do y ← x
5. if key[z] < key[x] comparing the value of node at x
Compare 32 and 12 40 with key[z], and move to the left
6. then x ← left[x] or right child accordingly.
35, traverse the
left subtree
insert 32 as 7. else x ← right[x] • When x is NIL, it is at the
y 25 left child correct position for node z.
25 8. p[z] ← y
y 9. if y = NIL • Compare z’s value with y’s
x 20 35 value, and insert z at either y’s
20 35 10. then root[t] ← z left or right, appropriately.
11. else if key[z] < key[y] • Complexity: O(h)
12 40 12 32 40 12. then left[y] ← z o Initialization: O(1)
z 13. else right[y] ← z o While loop (3-7) : O(h) time
o Insert the value (8-13) : O(1)

3  

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy