Presentation is loading. Please wait.

Presentation is loading. Please wait.

IIT Bombay Data Structures and Algorithms Prof. Ajit A. Diwan Prof. Ganesh Ramakrishnan Prof. Deepak B. Phatak Department of Computer Science and Engineering.

Similar presentations


Presentation on theme: "IIT Bombay Data Structures and Algorithms Prof. Ajit A. Diwan Prof. Ganesh Ramakrishnan Prof. Deepak B. Phatak Department of Computer Science and Engineering."— Presentation transcript:

1 IIT Bombay Data Structures and Algorithms Prof. Ajit A. Diwan Prof. Ganesh Ramakrishnan Prof. Deepak B. Phatak Department of Computer Science and Engineering IIT Bombay Session: Binary Trees 1Ajit A. Diwan, Ganesh Ramakrishnan, and Deepak B. Phatak, IIT Bombay

2 IIT Bombay Trees Trees are the simplest example of a non-sequential data type. In a sequence, every element (except the first and the last), has a unique next and previous element. In a tree, an element can have more than one ``next’’ element. Every element (except one), has exactly one previous element. Trees can give more compact representations of other types. Operations are more efficient compared to sequences. Ajit A. Diwan, Ganesh Ramakrishnan, and Deepak B. Phatak, IIT Bombay2

3 IIT Bombay Binary Trees Binary trees are the simplest kind of trees. Defined using a single operation, starting from an empty binary tree. Ф is a binary tree. If L, R are binary trees, then plant(n, L, R) is a binary tree. Here n denotes a node, a data type that can store values of any type. All binary trees can be obtained from Ф using the plant operation. Ajit A. Diwan, Ganesh Ramakrishnan, and Deepak B. Phatak, IIT Bombay3

4 IIT Bombay Definitions In the binary tree T obtained using the plant operation T = plant(n, L, R) n is called the root node of the tree T. L is the left subtree of T (also of node n) and R is the right subtree of T ( and node n). If L (or R) is not empty, but is of the form plant(m, L 1, L 2 ), then m is called the left (right) child of n, and n is the parent of m. T is said to contain the node n, and all nodes contained in L and R. Note that we assume that the node n is not contained in L or R. Ajit A. Diwan, Ganesh Ramakrishnan, and Deepak B. Phatak, IIT Bombay4

5 IIT Bombay Operations on Binary Trees Operations on trees defined in a way similar to numbers and sequences. Define for the empty tree. Assuming it is defined for trees L and R, define it for the tree plant(n, L, R). This defines the operation for all binary trees. Ajit A. Diwan, Ganesh Ramakrishnan, and Deepak B. Phatak, IIT Bombay5

6 IIT Bombay Height and Size height(Ф) = 0 height(plant(n, L, R)) = next(max(height(L), height(R))) Here max is the maximum function on numbers defined by max(0, n) = n for all n max(next(m), n) = next(m) if max(m, n) = m = n otherwise size(Ф) = 0 size(plant(n, L, R)) = next(add(size(L), size(R))) Ajit A. Diwan, Ganesh Ramakrishnan, and Deepak B. Phatak, IIT Bombay6

7 IIT Bombay Equality When are two binary trees equal? equal(Ф, Ф) = true equal(Ф, plant(m, L 1, R1 )) = false equal(plant(n, L, R), Ф) = false equal(plant(n, L, R), plant(m, L 1, R 1 )) = equal(L, L 1 ) and equal(R, R 1 ) Note that the nodes play no role in the definition. Ajit A. Diwan, Ganesh Ramakrishnan, and Deepak B. Phatak, IIT Bombay7

8 IIT Bombay Special Binary Trees Some special types of binary trees are particularly useful. Full binary trees full(Ф) = true full(plant(n, L, R)) = false if height(L) ≠ height(R) = full(L) and full(R) otherwise A full binary tree of height h has size For every node, the left and right subtree have the same size. Ajit A. Diwan, Ganesh Ramakrishnan, and Deepak B. Phatak, IIT Bombay8

9 IIT Bombay Complete Binary Trees complete(Ф) = true complete(plant(n, L, R)) = full(L) and complete(R) if height(L) = height(R) = complete(L) and full(R) if height(L) = next(height(R)) = false otherwise Ajit A. Diwan, Ganesh Ramakrishnan, and Deepak B. Phatak, IIT Bombay9

10 IIT Bombay Tree Traversals Many times we need a list of all the nodes in a tree. Tree traversal is a way of constructing a sequence of nodes in the tree. Different ways of traversing give sequences with different properties. These sequences give an alternative way of representing the tree. Ajit A. Diwan, Ganesh Ramakrishnan, and Deepak B. Phatak, IIT Bombay10

11 IIT Bombay Inorder, Preorder, Postorder Traversal inorder(Ф) = Ф, preorder(Ф) = Ф, postorder(Ф) = Ф inorder(plant(n, L, R)) = concatenate(concatenate(inorder(L), push(n,Ф)), inorder(R)) preoder(plant(n, L, R)) = concatenate(concatenate(push(n,Ф), preorder(L)), preorder(R)) postorder(plant(n, L, R)) = concatenate(concatenate(postorder(L), postorder(R)), push(n,Ф)) Ajit A. Diwan, Ganesh Ramakrishnan, and Deepak B. Phatak, IIT Bombay11

12 IIT Bombay Exercises Let T(n) denote the number of binary trees of size n. Show how you can compute T(n) using a recurrence relation. Suppose you are given the sequence of nodes in a binary tree obtained by inorder and preorder traversal. Show that these two sequences uniquely represent the binary tree. Two nodes in a binary tree are said to be siblings if they have the same parent. Write a function to count the number of pairs of siblings in a binary tree. Ajit A. Diwan, Ganesh Ramakrishnan, and Deepak B. Phatak, IIT Bombay12


Download ppt "IIT Bombay Data Structures and Algorithms Prof. Ajit A. Diwan Prof. Ganesh Ramakrishnan Prof. Deepak B. Phatak Department of Computer Science and Engineering."

Similar presentations


Ads by Google