//反转二叉树
//反转二叉树 遍历树 交换左右树
class Node{
constructor(element,parent){ //parent是树中比较重要的属性
this.element = element;
this.parent = parent;
this.left = null;
this.right = null;
}
}
class BST{ //不能通过索引取数据
constructor(compare){
this.root = null;
this.size= 0; //包含着的节点个数
this.compare = compare||this.compare;
}
compare(e1,e2)
{
return e1 - e2;
}
add(element){
if(this.root == null)
{
this.root = new Node(element,null);
this.size++;
return;
}
else
{
//增加的不是根节点,第一种方式是根据递归 写循环
let currentNode = this.root;//当前的根节点
let compare= 0;//当前比较的结果
let parent = null;//当前父节点
while(currentNode){
compare= this.compare(element,currentNode.element);
parent = currentNode;
if(compare > 0) //如果大于0就找右树,小于0找左树
{
currentNode= currentNode.right;
}
else if(compare < 0)
{
currentNode = currentNode.left;
}
else{
//break;不处理
currentNode.element = element; //如果比较后结果一样,就有最新的去覆盖
return;
}
};
let newNode = new Node(element,parent);
if(compare > 0)
{
parent.right = newNode;
}
else
{
parent.left = newNode;
}
}
}
preorderTraversal(visitor){ //访问DOM元素一般采用的就是这种方式
if(visitor == null)return;
const travelsal = (node)=>{
if(node == null)return;
visitor.visit(node);
travelsal(node.left);
travelsal(node.right);
}
travelsal(this.root);
}
inorderTraversal(visitor){ //按照由小到大排列
if(visitor == null)return;
const travelsal=(node)=>{
if(node == null)return;
travelsal(node.left);
visitor.visit(node);
travelsal(node.right);
}
travelsal(this.root);
}
postorderTraversal(visitor){ //先操作子节点再操作父节点
if(visitor == null)return;
const travelsal=(node)=>{
if(node == null)return;
travelsal(node.left);
travelsal(node.right);
visitor.visit(node);
}
travelsal(this.root);
}
levelOrderTraversal(visitor){
if(this.root == null || visitor == null)
{
return; //一个节点都没有 直接return即可
}
let stack = [this.root];//根节点放入到队列中
let index = 0;
let currentNode = null;
while(currentNode=stack[index++]){
//层序遍历 优化递归可以使用栈的方式 队列的方式
visitor.visit(currentNode);
if(currentNode.left)
{
stack.push(currentNode.left);
}
if(currentNode.right){
stack.push(currentNode.right);
}
}
stack = null;
}
invertTree(){
if(this.root== null)return;
let stack = [this.root];
let index = 0;
let currentNode = null;
while(currentNode = stack[index++]){
let tmp = currentNode.left;
currentNode.left = currentNode.right;
currentNode.right = tmp;
if(currentNode.left){
stack.push(currentNode.left);
}
if(currentNode.right)
{
stack.push(currentNode.right);
}
}
stack = null;
return this.root;
}
}
let bst = new BST((e1,e2)=>{ //arr.sort默认调用sort就是简单a-b 自定义比较器
return e1.age-e2.age;
});
let arr=[{age:10,name:'zs'},{age:8},{age:19},{age:6},{age:15},{age:22},{age:20},{age:10,name:'lisi'}];
arr.forEach((element)=>{
bst.add(element);
});
//console.dir(bst.root,{depth:200});
//bst.preorderTraversal();
/*
bst.inorderTraversal({
visit(node){ //ast语法树 babel-transform
console.log(node.element,'*****');
}
})
bst.levelOrderTraversal({
visit(node){
console.log(node.element,"----");
}
})
D:\important_file\笔记>node test.js
{ age: 10, name: 'lisi' } ----
{ age: 8 } ----
{ age: 19 } ----
{ age: 6 } ----
{ age: 15 } ----
{ age: 22 } ----
{ age: 20 } ----
*/
console.dir(bst.invertTree(),{depth:2000});
/*
D:\important_file\笔记>node test.js
<ref *1> Node {
element: { age: 10, name: 'lisi' },
parent: null,
left: <ref *2> Node {
element: { age: 19 },
parent: [Circular *1],
left: <ref *3> Node {
element: { age: 22 },
parent: [Circular *2],
left: null,
right: Node {
element: { age: 20 },
parent: [Circular *3],
left: null,
right: null
}
},
right: Node {
element: { age: 15 },
parent: [Circular *2],
left: null,
right: null
}
},
right: <ref *4> Node {
element: { age: 8 },
parent: [Circular *1],
left: null,
right: Node {
element: { age: 6 },
parent: [Circular *4],
left: null,
right: null
}
}
}
*/