Binary Search Trees: Understanding Tree Structure and Implementation, Study notes of Data Structures and Algorithms

An introduction to binary search trees, explaining their terminology, implementation of the TreeSort algorithm, and the relationship between nodes and subtrees. It also covers the concept of a binary tree, the height of a tree, and the difference between complete and balanced trees. The document concludes with an overview of traversal methods and example implementations.

Typology: Study notes

2021/2022

Uploaded on 09/27/2022

palumi
palumi 🇺🇸

4.2

(14)

245 documents

1 / 68

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Binary Search Trees
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b
pf1c
pf1d
pf1e
pf1f
pf20
pf21
pf22
pf23
pf24
pf25
pf26
pf27
pf28
pf29
pf2a
pf2b
pf2c
pf2d
pf2e
pf2f
pf30
pf31
pf32
pf33
pf34
pf35
pf36
pf37
pf38
pf39
pf3a
pf3b
pf3c
pf3d
pf3e
pf3f
pf40
pf41
pf42
pf43
pf44

Partial preview of the text

Download Binary Search Trees: Understanding Tree Structure and Implementation and more Study notes Data Structures and Algorithms in PDF only on Docsity!

Binary Search Trees

 Understand tree terminology

 Understand and implement tree traversals

 Define the binary search tree property

 Implement binary search trees

 Implement the TreeSort algorithm

 A set of nodes (or vertices) with a single starting point ▪ called the root  Each node is connected by an edge to another node  A tree is a connected graph ▪ There is a path to every node in the tree ▪ A tree has one less edge than the number of nodes root node edge

yes! NO! All the nodes are not connected NO! There is an extra edge (5 nodes and 5 edges) yes! (but not a binary tree) yes! (it’s actually a similar graph to the blue one)

 A leaf is a node with no children  A path is a sequence of nodes v

… v

n

▪ where v i is a parent of v i+ (1  in )  A subtree is any node in the tree along with all of its descendants  A binary tree is a tree with at most two children per node ▪ The children are referred to as left and right ▪ We can also refer to left and right subtrees

D

E F GG

C

B D

E F G

C, E, F and G are leaves path from A to D to G subtree rooted at B

AA

C

 The height of a node v is the length of the longest path from v to a leaf ▪ The height of the tree is the height of the root  The depth of a node v is the length of the path from v to the root ▪ This is also referred to as the level of a node  Note that there is a slightly different formulation of the height of a tree ▪ Where the height of a tree is said to be the number of different levels of nodes in the tree (including the root)

A

B

E

I

F

A

B

E

height of node B is 2 height of the tree is 3 depth of node E is 2 level 1 level 2 level 3

B

A

J

D G

H

C

 Each level doubles the number of nodes

▪ Level 1 has 2 nodes (

1

▪ Level 2 has 4 nodes (

2

) or 2 times the number in Level 1

 Therefore a tree with h levels has 2 h +

  • 1 nodes

▪ The root level has 1 node

the bottom level has 2 h nodes, that is, just over ½ the nodes are leaves

 A binary tree is complete if ▪ The leaves are on at most two different levels, ▪ The second to bottom level is completely filled in and ▪ The leaves on the bottom level are as far to the left as possible

A

B C

D E F

A

B C

D E F

A

B C

D E F

G

A

B

C D

A

B C

D E

F

 A traversal algorithm for a binary tree visits each node in the tree ▪ Typically, it will do something while visiting each node!  Traversal algorithms are naturally recursive  There are three traversal methods ▪ Inorder ▪ Preorder ▪ Postorder

inOrder(Node* nd) {

if (nd != NULL) {

inOrder(nd->leftChild);

visit(nd);

inOrder(nd->rightChild);

The visit function would do whatever the purpose of the traversal is, for example print the data in the node