简体   繁体   English

二进制搜索树中的插入vs二进制树中的插入

[英]Insertion in Binary Search Tree vs Insertion in Binary Tree

What is the difference between insertion in Binary Search Tree(BST) and in Binary Tree(BT)? 在二叉搜索树(BST)和二叉树(BT)中插入有什么区别? I know in BST, you compare the value of the new node with root, if smaller, you added to its left, if bigger you add it to the right of the root. 我知道在BST中,您将新节点的值与根进行比较,如果较小,则将其添加到左侧,如果较大,则将其添加到根的右侧。 Is it the same procedure for BT? BT的程序是否相同? If not, what procedure does it follow for insertion and removal? 如果没有,插入和移除将遵循什么程序?

Looks like you have misunderstanding of BT and BST defenitions. 看起来您对BT和BST的防御方式有误解。 First you need to know difference between BT and BST. 首先,您需要了解BT和BST之间的区别。

  • Binary Tree is a tree, which node has at most 2 children. 二叉树是一棵树,该节点最多有2个子节点。 Storing children to the left or to the right branch doesn't depend on children value. 将子级存储到左侧或右侧分支与子级值无关。
  • Binary Search Tree is a Binary Tree, in which childrens of each node are stored in a specific order. 二进制搜索树是一个二进制树,其中每个节点的子级以特定顺序存储。 Children smaller than parent node usually stored on the left branch, greater or equal on the right. 小于父节点的子级通常存储在左侧分支上,大于或等于右侧。

Answering your question: 回答您的问题:

  • inserting in Binary Tree you need to keep track that each node has no more than 2 children. 在二叉树中插入,您需要跟踪每个节点的子节点不超过2个。 Other words, to add element to binary tree you just add it as a child to any node with less than 2 children. 换句话说,要将元素添加到二叉树中,只需将其作为子元素添加到少于2个子元素的任何节点。
  • insertng in Search Binary Tree you need to keep track that children are stored in the specific order( child smaller than parent on the left and greater or equal on the right) and parent has at most 2 children. 在搜索二叉树中插入时,您需要跟踪子项以特定顺序存储(子项小于父项,左边大于或等于右边),并且父项最多有2个子项。

You are not restricted to have children <= or >= to the parent node depending on left/right. 根据左/右,您不可以让父节点的子节点<=或> =。

Just put them anywhere as long as each node has at most 2 children. 只要将它们放置在任何位置,只要每个节点最多有2个子节点即可。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM