Docsity

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

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