1 / 25

# ADT Tree - PowerPoint PPT Presentation

ADT Tree. Tree : nonempty collection of vertices and edges that satisfies certain requirements. Vertex ( node ): Have a name and carry other associated information Edge : A connection between two vertices.

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

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

Tree: nonempty collection of vertices and edges that satisfies certain requirements.

Vertex (node): Have a name and carry other associated information

Edge: A connection between two vertices.

We usually draw trees with the root (node A)at the top and leaves at the bottom!

• Any node (except the root) has exactly one edge running upward to another node. The node above it is called theparent of the node. For example, D is the parent of H and I. G is the parent of J and K.

• The nodes below a given node are called its children. For example, B, C and D are children of A. L is a child of H.

• Nodes with no children are known as leaves. For example, E,F,J,L,M and N are leaves.

• A node that has children is an internal node. For example, C is an internal node.

• The height of a tree is the length (i.e. number of edges) of a longest path from the root to a leaf. For example, the height of the above tree is 3.

Subtree

Subtree

Binary tree : no node can have more than two children (left child and right child).

+

C

D

_

A

B

E

### Expression Tree

• Expression tree for (A + B)  ((C / D) – E).

• Leaves are operands (constants or variable names). Internal nodes are operators

• If we traverse the above tree in postorder we will get

AB+ CD/E -  which is the postfix of (A + B)  ((C / D) – E).

### Complete Binary Tree

• All internal nodes have (exactly) two children

• All leaves are at the same level

• Height of an n-node complete binary tree is log n

### Implementation : ADT Binary Tree

Data Structure:

typedef struct Node *Node_ptr;

struct Node

{

ElementType element;

Node_ptr left;

Node_ptr right;

} ;

typedef Node_ptr tree;

### ADT Binary Tree - Operations

• create anempty tree

• add a new node to the tree

• remove a node from the tree.

• tree traversal :preorder, inorder and post order

### Tree traversals

Inorder:

• Traverse the left subtree in inorder

• Visit the root (and print the value )

• Traverse the right subtree in inorder

### Tree traversals

Preorder:

• Visit the root (and print the value )

• Traverse the left subtree in preorder

• Traverse the right subtree in preorder

### Tree traversals

Postorder:

• Traverse the left subtree in postorder

• Traverse the right subtree in postorder

• Visit the root (and print the value )

### Tree traversals

Inorder : B A C

Preorder : A B C

Postorder : B C A

A

B

C

### Tree traversals

Inorder: D B E A F C G

Preorder: A B D EC F G

Postorder: D E BF G C A

A

C

B

G

F

D

E

### Tree traversals

Inorder :

G D H B A E I C J F K

Preorder:

A B D G HC E I F J K

Postorder:

G H D BI E J K F C A

A

C

B

E

F

D

I

J

K

G

H

### ADT Binary Search Tree (BST)

• Binary search tree is a binary tree such that for every node, X, in the tree, the values of the all the nodes in the left subtree are less the value stored at X, and the values of all the nodes in the right subtree are equal or larger than the value stored at X ( BST Property).

• That is, a node’s left child must have a value less than its parent, and a node’s right child must have a value equal or greater than its parent .

### ADT Binary Search Tree (BST)

Operations:

• create a BST

• insert an element

• delete an element

• find an element (search)

• display all elements in a sorted order

### ADT Binary Search Tree (BST)

Insert:

• Start at the root

• If value we want to insert is less than value of current node, we have to go to the left subtree

• Otherwise we have to go to the right subtree

• If the current node is empty (not existing) create a node with the value we are inserting and place it here.

87

87

27

67

54

54

### Building a BST

54, 17, 87, 11, 19, 18, 93

54

54

54

54

87

17

17

87

17

11

54

54

17

87

17

87

11

93

19

11

19

18

### ADT Binary Search Tree (BST)

Delete:

Similar to the insert function, after deletion of a node, the BST property must be maintained.

There are 3 possible cases:

• Node to be deleted has no children

We just delete the node.

• Node to be deleted has only one child

 The node can be deleted by replacing the node by that child.

• Node to be deleted has two children

### ADT Binary Search Tree (BST) – Delete (contd.)

Node to be deleted has two children

### ADT Binary Search Tree (BST) – Delete (contd.)

Node to be deleted has two children

Steps - A:

• Find minimum value of right subtree

• Delete minimum node of right subtree but keep its value

• Replace the value of the node to be deleted by this minimum value

OR

Steps - B:

• Find maximum value of left subtree

• Delete maximum node of left subtree but keep its value

• Replace the value of the node to be deleted by this maximum value

### ADT Binary Search Tree (BST)

Search-

for element X in Tree T (r is the value stored at root)

if T is empty

"X is not in the tree";

else {

if X = r

"X is found"

else {

if X < r

search the left tree

else

search the right tree

}

}

### ADT Binary Search Tree (BST)

Using the data structure described in page 7, the above algorithm can be implemented in C as follows:

tree Binarysearch (ElementType X, tree T)

{if (T = NULL)

ERROR("X is not in the tree") ;

else {

if (X = T->element)

return T;

else {

if (X < T->element)

return Binarysearch (X, T->left);

else

return Binarysearch (X, T->right);

}

}

}

An AVL (Adelson-Velskii and Landis) tree is a balanced BST with the height O(log n)