topics definition and application of binary trees binary search tree operations
TRANSCRIPT
![Page 1: Topics Definition and Application of Binary Trees Binary Search Tree Operations](https://reader035.vdocuments.us/reader035/viewer/2022072006/56649f575503460f94c7bb7a/html5/thumbnails/1.jpg)
Topics
Definition and Application of Binary Trees
Binary Search Tree Operations
![Page 2: Topics Definition and Application of Binary Trees Binary Search Tree Operations](https://reader035.vdocuments.us/reader035/viewer/2022072006/56649f575503460f94c7bb7a/html5/thumbnails/2.jpg)
Definition and Application of Binary Trees
• Binary tree: a nonlinear data structure in which each node may point to 0, 1, or two other nodes
• The nodes that a nodeN points to are
the childrenof N NULL NULL
NULL NULL NULL NULL
![Page 3: Topics Definition and Application of Binary Trees Binary Search Tree Operations](https://reader035.vdocuments.us/reader035/viewer/2022072006/56649f575503460f94c7bb7a/html5/thumbnails/3.jpg)
Terminology
• If a node N is a child of another node P, then P is called the parent of N
• A node that has no children is called a leaf
• In a binary tree there is a unique node with no parent, it is called the root of the tree
![Page 4: Topics Definition and Application of Binary Trees Binary Search Tree Operations](https://reader035.vdocuments.us/reader035/viewer/2022072006/56649f575503460f94c7bb7a/html5/thumbnails/4.jpg)
Binary Tree Terminology
• Tree pointer: like a head pointer for a linked list, it points to the first node in the binary tree
• Root node: the node with no parent NULL NULL
NULL NULL NULL NULL
![Page 5: Topics Definition and Application of Binary Trees Binary Search Tree Operations](https://reader035.vdocuments.us/reader035/viewer/2022072006/56649f575503460f94c7bb7a/html5/thumbnails/5.jpg)
Binary Tree Terminology
• Leaf nodes: nodes that have no children
The nodes containing 7 and 43 are leaf nodes
NULL NULL7
19
31
43
59
NULL NULL NULL NULL
![Page 6: Topics Definition and Application of Binary Trees Binary Search Tree Operations](https://reader035.vdocuments.us/reader035/viewer/2022072006/56649f575503460f94c7bb7a/html5/thumbnails/6.jpg)
Binary Tree Terminology
• Child nodes, children: The children of the node containing 31 are the nodes containing 19 and 59
NULL NULL7
19
31
43
59
NULL NULL NULL NULL
![Page 7: Topics Definition and Application of Binary Trees Binary Search Tree Operations](https://reader035.vdocuments.us/reader035/viewer/2022072006/56649f575503460f94c7bb7a/html5/thumbnails/7.jpg)
Binary Tree Terminology
The parent of the node containing 43 is the node containing 59
NULL NULL7
19
31
43
59
NULL NULL NULL NULL
![Page 8: Topics Definition and Application of Binary Trees Binary Search Tree Operations](https://reader035.vdocuments.us/reader035/viewer/2022072006/56649f575503460f94c7bb7a/html5/thumbnails/8.jpg)
Binary Tree Terminology
• A descendant of a node is defined as follows:
(1) A node is considered its own descendant
(2) A child of a descendant of a node N is a descendant of N
So the set of all descendants of a nodeincludes the node itself, its children, thechildren of the children, etc.
![Page 9: Topics Definition and Application of Binary Trees Binary Search Tree Operations](https://reader035.vdocuments.us/reader035/viewer/2022072006/56649f575503460f94c7bb7a/html5/thumbnails/9.jpg)
Binary Tree Terminology
• A subtree of a binary tree is a part of the tree consisting of all descendants of a given node N
• Such a subtree is said to be rooted at N, and N is called the root of the subtree
![Page 10: Topics Definition and Application of Binary Trees Binary Search Tree Operations](https://reader035.vdocuments.us/reader035/viewer/2022072006/56649f575503460f94c7bb7a/html5/thumbnails/10.jpg)
Subtrees of Binary Trees
• A subtree of a binary tree is itself a binary tree
• A nonempty binary tree consists of a root node, with the rest of its nodes forming two subtrees, called the left and right subtree
![Page 11: Topics Definition and Application of Binary Trees Binary Search Tree Operations](https://reader035.vdocuments.us/reader035/viewer/2022072006/56649f575503460f94c7bb7a/html5/thumbnails/11.jpg)
Binary Tree Terminology
• The node containing 31 is the root
• The nodes containing 19 and 7 form the left subtree
• The nodes containing 59 and 43 form the right subtree
NULL NULL7
19
31
43
59
NULL NULL NULL NULL
![Page 12: Topics Definition and Application of Binary Trees Binary Search Tree Operations](https://reader035.vdocuments.us/reader035/viewer/2022072006/56649f575503460f94c7bb7a/html5/thumbnails/12.jpg)
Binary Tree Node• A node in a binary tree is like a node in a
linked list, except it has two node pointer fields:class TreeNode{ public: int value;
TreeNode *left; TreeNode *right;
};It is convenient to use a constructor to assistin creation of nodes
![Page 13: Topics Definition and Application of Binary Trees Binary Search Tree Operations](https://reader035.vdocuments.us/reader035/viewer/2022072006/56649f575503460f94c7bb7a/html5/thumbnails/13.jpg)
Binary Tree Operations
• Create a binary tree
• Insert a node into a binary tree at some position
• Delete a node from a binary tree – remove a node and adjust links to preserve the binary tree
• Update a node in a binary tree
• Retrieve value of a node in a binary tree
![Page 14: Topics Definition and Application of Binary Trees Binary Search Tree Operations](https://reader035.vdocuments.us/reader035/viewer/2022072006/56649f575503460f94c7bb7a/html5/thumbnails/14.jpg)
Inserting an item into a Binary Tree
1) If tree is empty, create a new node as root, with empty left and right subtrees
2) Otherwise, insert the item as a leaf (as either the right or left child), update the links
![Page 15: Topics Definition and Application of Binary Trees Binary Search Tree Operations](https://reader035.vdocuments.us/reader035/viewer/2022072006/56649f575503460f94c7bb7a/html5/thumbnails/15.jpg)
Inserting an item into a Binary Tree
NULL NULL7
87
65
43
59
root
Since the right subtree is NULL, insert 23 here
NULL NULL NULL NULL
![Page 16: Topics Definition and Application of Binary Trees Binary Search Tree Operations](https://reader035.vdocuments.us/reader035/viewer/2022072006/56649f575503460f94c7bb7a/html5/thumbnails/16.jpg)
Traversing a Binary Tree
Three traversal methods:1) Inorder:
a) Traverse left subtree of nodeb) Process data in nodec) Traverse right subtree of node
2) Preorder: a) Process data in nodeb) Traverse left subtree of nodec) Traverse right subtree of node
3) Postorder: a) Traverse left subtree of nodeb) Traverse right subtree of nodec) Process data in node
![Page 17: Topics Definition and Application of Binary Trees Binary Search Tree Operations](https://reader035.vdocuments.us/reader035/viewer/2022072006/56649f575503460f94c7bb7a/html5/thumbnails/17.jpg)
Traversing a Binary Tree
NULL NULL7
87
65
43
59
TRAVERSAL METHOD
NODES VISITED IN ORDER
Inorder 7, 87, 65, 43, 59
Preorder 65, 87, 7, 59, 43
Postorder 7, 87, 43, 59, 65
NULL NULL NULL NULL
![Page 18: Topics Definition and Application of Binary Trees Binary Search Tree Operations](https://reader035.vdocuments.us/reader035/viewer/2022072006/56649f575503460f94c7bb7a/html5/thumbnails/18.jpg)
Uses of Binary Trees
• Binary search tree: a binary tree whose data is organized to simplify searches
• Left subtree at each node contains data values less than the data in the node
• Right subtree at each node contains values greater than the data in the node
NULL NULL7
19
31
43
59
NULL NULL NULL NULL
![Page 19: Topics Definition and Application of Binary Trees Binary Search Tree Operations](https://reader035.vdocuments.us/reader035/viewer/2022072006/56649f575503460f94c7bb7a/html5/thumbnails/19.jpg)
Uses of Binary Trees
• Decision Tree: a binary tree whose data is organized to answer yes/no question
• Left subtree at each node contains yes responses
• Right subtree at each node contains no responses
NULLBird
Does it fly?
Is it an animal?
Flower
NULL NULL NULL
Dog
NULL NULL
NULL