Top Banner
Trees A tree is a set of nodes which are connected by branches to other nodes in a 'tree-like' structure. There is a special node called the root from which all other nodes can be accessed. Other nodes are grouped into subtrees which in turn form other subtrees and so on. The tree shown in figure 1 has a variable number of branches from each node and may not be a practical structure to implement. A special tree with more than two branches can be implemented as a B Tree. A tree with a maximum of two branches is known as a Binary Tree. A Strict Binary Tree has either
21

Trees A tree is a set of nodes which are connected by branches to other nodes in a 'tree-like' structure. There is a special node called the root from.

Jan 04, 2016

Download

Documents

Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Trees A tree is a set of nodes which are connected by branches to other nodes in a 'tree-like' structure. There is a special node called the root from.

Trees

A tree is a set of nodes which are connected by branches to other nodes in a 'tree-like' structure. There is a special node called the root from which all other nodes can be accessed. Other nodes are grouped into subtrees which in turn form other subtrees and so on. The tree shown in figure 1 has a variable number of branches from each node and may not be a practical structure to implement. A special tree with more than two branches can be implemented as a B Tree. A tree with a maximum of two branches is known as a Binary Tree. A Strict Binary Tree has either none or two branches.

Page 2: Trees A tree is a set of nodes which are connected by branches to other nodes in a 'tree-like' structure. There is a special node called the root from.

Tree diagram figure 1

Page 3: Trees A tree is a set of nodes which are connected by branches to other nodes in a 'tree-like' structure. There is a special node called the root from.

Knuth Binary Trees

A Knuth Binary Tree has none, one or two branches. Figure 2 shows a Knuth Binary Tree. Each node contains two pointers, one pointing to the left subtree and one pointing to the right subtree. A Knuth Tree is easier to implement than a strict binary tree as recursive algorithms can be used. In a Binary Search Tree the values contained in the nodes in the left subtree, child nodes, will all be less than that in the root node. Similarly the values contained in the nodes in the right subtree will all be greater than that in the root node. Trees can be reversed if required by making the left subtrees contain larger values than the root.

Page 4: Trees A tree is a set of nodes which are connected by branches to other nodes in a 'tree-like' structure. There is a special node called the root from.

Knuth binary tree figure 2

Page 5: Trees A tree is a set of nodes which are connected by branches to other nodes in a 'tree-like' structure. There is a special node called the root from.

Binary search tree

Figure 3 shows the order of the nodes in a example of a binary search tree. The letters symbolise the order of the nodes. In a balanced tree the root node would be approximately midway in an ordered list of the contents of the nodes. Nodes with lower values than the root are found in the left subtree and higher values in the right subtree. Each subtree follows the same rule.

Page 6: Trees A tree is a set of nodes which are connected by branches to other nodes in a 'tree-like' structure. There is a special node called the root from.

Binary search tree figure 3

Page 7: Trees A tree is a set of nodes which are connected by branches to other nodes in a 'tree-like' structure. There is a special node called the root from.

Traversing a Tree

A traverse is an ordered walk through a tree such that each node is visited only once. There are various ways

of traversing a tree. Using figure 3 as an example:

Page 8: Trees A tree is a set of nodes which are connected by branches to other nodes in a 'tree-like' structure. There is a special node called the root from.

Algorithm for constructing a tree (adding a unique node)

If the tree is empty then make this item the treeelse compare the item to the root if the item comes before the root add item to the left branch end if if the item comes after the root (if item is not unique need to use an else to allow for equality) add item to the right branch end ifend if

Page 9: Trees A tree is a set of nodes which are connected by branches to other nodes in a 'tree-like' structure. There is a special node called the root from.

Algorithm for searching a tree

if the tree is not empty and not found if the key sought equals the root's key then found is true else if the key sought is less than the root's key then search down the left tree else search down the right tree end if end ifend if

Page 10: Trees A tree is a set of nodes which are connected by branches to other nodes in a 'tree-like' structure. There is a special node called the root from.

Deleting an item from a tree

There are 4 cases:

1. The node is a leaf2. The node has only one branch3. The node has 2 branches and the Rightmost

Node of the Left Subtree (RNLS) is a leaf4. The node has 2 branches and the RNLS has a left

subtree

Page 11: Trees A tree is a set of nodes which are connected by branches to other nodes in a 'tree-like' structure. There is a special node called the root from.

Case 1 - the node is a leaf

Free the space occupied by the leaf node.

Make pointer to the node from the parent node ==NULL.

Page 12: Trees A tree is a set of nodes which are connected by branches to other nodes in a 'tree-like' structure. There is a special node called the root from.

Case 2: The node has 1 branch

Graft the child branch of the node onto the parent node.

Free the space occupied by the deleted node.

Page 13: Trees A tree is a set of nodes which are connected by branches to other nodes in a 'tree-like' structure. There is a special node called the root from.

Case 3: The node has 2 branches and the Rightmost Node of the Left Subtree (RNLS) is a leaf

Copy data part of the RNLS to data part of node to be deleted

Free the space occupied by the RNLS

Make pointer to the RNLS from it's parent == NULL

Page 14: Trees A tree is a set of nodes which are connected by branches to other nodes in a 'tree-like' structure. There is a special node called the root from.

Case 4: The node has 2 branches and the RNLS has a left subtree

Copy data part of RNLS to data part of node to be deleted

Graft the child branch of the RNLS (can only be a left branch)

onto the RNLS's parent node.

Free the space occupied by the RNLS

Page 15: Trees A tree is a set of nodes which are connected by branches to other nodes in a 'tree-like' structure. There is a special node called the root from.

tree.csource1

Global declarations

Page 16: Trees A tree is a set of nodes which are connected by branches to other nodes in a 'tree-like' structure. There is a special node called the root from.

tree.csource2

main()

Page 17: Trees A tree is a set of nodes which are connected by branches to other nodes in a 'tree-like' structure. There is a special node called the root from.

tree.csource3

chop()pause()menu()

Page 18: Trees A tree is a set of nodes which are connected by branches to other nodes in a 'tree-like' structure. There is a special node called the root from.

tree.csource4

display,getentryfunctions

Page 19: Trees A tree is a set of nodes which are connected by branches to other nodes in a 'tree-like' structure. There is a special node called the root from.

tree.csource5

insert,find_delfunctions

Page 20: Trees A tree is a set of nodes which are connected by branches to other nodes in a 'tree-like' structure. There is a special node called the root from.

tree.c source 6 delet() function

Page 21: Trees A tree is a set of nodes which are connected by branches to other nodes in a 'tree-like' structure. There is a special node called the root from.

tree.csource7

whichcase() function